#include <bits/stdc++.h> using namespace std; const int q=3*1e4+1,p=2*1e5+1; vector <int> t[q],t1[q]; vector<pair<int,int> >res,res1,res2; set<int> s; int vis[q]; void dfs(int v){ for(auto it=t[v].begin(); it!=t[v].end(); it++){ if(!vis[*it]){ int check=1; for(auto it1=t[1].begin(); it1!=t[1].end(); it1++) if(*it1==*it) check=0; if(check) res.push_back({1,*it}); vis[*it]=1; dfs(*it); } } } int main(){ int n,m; cin>>n>>m; for(int i=1; i<=m; i++){ int a,b; cin>>a>>b; t[a].push_back(b); t[b].push_back(a); } vis[1]=1; dfs(1); int x; cin>>x; for(int i=1; i<=x; i++){ int a,b; cin>>a>>b; t1[a].push_back(b); t1[b].push_back(a); int check=1; for(auto it=t[a].begin(); it!=t[a].end(); it++){ if(*it==b){ check=0; } } if(check){ if(a==1 || b==1) s.insert(max(a,b)); else res2.push_back({a,b}); } } for(int i=1; i<=n; i++){ for(auto it=t[i].begin(); it!=t[i].end(); it++){ int check=1; for(auto it1=t1[i].begin(); it1!=t1[i].end(); it1++) if(*it==*it1) check=0; if(check && i<*it) res1.push_back({i,*it}); } } // for(auto it=res1.begin(); it!=res1.end(); it++){ // cout<<"spr "<<it->first<<" "<<it->second<<endl; // } ///wyswietlenie powtarzajcych sie int result=2*res.size()+res2.size()-s.size()+res1.size(); cout<<result<<endl; ///wynik for(auto it=res.begin(); it!=res.end(); it++){ cout<<"+ "<<it->first<<" "<<it->second<<endl; ///wiazania z 1 } for(auto it=res2.begin(); it!=res2.end(); it++){ cout<<"+ "<<it->first<<" "<<it->second<<endl; ///wiazania docelowe } for(auto it=res1.begin(); it!=res1.end(); it++){ cout<<"- "<<it->first<<" "<<it->second<<endl; ///usuniecie wiazan pocz } for(auto it=res.begin(); it!=res.end(); it++){ if(it->first!=1 && it->second!=1) break; auto it1=s.lower_bound(max(it->first,it->second)); if(*it1!=max(it->first,it->second)){ cout<<"- "<<it->first<<" "<<it->second<<endl; } else s.erase(it1); } ///usuniecie zbednych wiazan z 1 return 0; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 | #include <bits/stdc++.h> using namespace std; const int q=3*1e4+1,p=2*1e5+1; vector <int> t[q],t1[q]; vector<pair<int,int> >res,res1,res2; set<int> s; int vis[q]; void dfs(int v){ for(auto it=t[v].begin(); it!=t[v].end(); it++){ if(!vis[*it]){ int check=1; for(auto it1=t[1].begin(); it1!=t[1].end(); it1++) if(*it1==*it) check=0; if(check) res.push_back({1,*it}); vis[*it]=1; dfs(*it); } } } int main(){ int n,m; cin>>n>>m; for(int i=1; i<=m; i++){ int a,b; cin>>a>>b; t[a].push_back(b); t[b].push_back(a); } vis[1]=1; dfs(1); int x; cin>>x; for(int i=1; i<=x; i++){ int a,b; cin>>a>>b; t1[a].push_back(b); t1[b].push_back(a); int check=1; for(auto it=t[a].begin(); it!=t[a].end(); it++){ if(*it==b){ check=0; } } if(check){ if(a==1 || b==1) s.insert(max(a,b)); else res2.push_back({a,b}); } } for(int i=1; i<=n; i++){ for(auto it=t[i].begin(); it!=t[i].end(); it++){ int check=1; for(auto it1=t1[i].begin(); it1!=t1[i].end(); it1++) if(*it==*it1) check=0; if(check && i<*it) res1.push_back({i,*it}); } } // for(auto it=res1.begin(); it!=res1.end(); it++){ // cout<<"spr "<<it->first<<" "<<it->second<<endl; // } ///wyswietlenie powtarzajcych sie int result=2*res.size()+res2.size()-s.size()+res1.size(); cout<<result<<endl; ///wynik for(auto it=res.begin(); it!=res.end(); it++){ cout<<"+ "<<it->first<<" "<<it->second<<endl; ///wiazania z 1 } for(auto it=res2.begin(); it!=res2.end(); it++){ cout<<"+ "<<it->first<<" "<<it->second<<endl; ///wiazania docelowe } for(auto it=res1.begin(); it!=res1.end(); it++){ cout<<"- "<<it->first<<" "<<it->second<<endl; ///usuniecie wiazan pocz } for(auto it=res.begin(); it!=res.end(); it++){ if(it->first!=1 && it->second!=1) break; auto it1=s.lower_bound(max(it->first,it->second)); if(*it1!=max(it->first,it->second)){ cout<<"- "<<it->first<<" "<<it->second<<endl; } else s.erase(it1); } ///usuniecie zbednych wiazan z 1 return 0; } |