#include<iostream> #include<vector> #include<algorithm> #include<map> #include<set> using namespace std; #define MAXS 50010 set<int> firstGraph[MAXS]; set<int> targetGraph[MAXS]; int processed[MAXS]; vector<pair<char,pair<int,int> > > result; void firstDfs(int v){ if(processed[v]){ return; } processed[v]=1; if(v!=1 && firstGraph[v].count(1)==0){ result.push_back({'+',{1,v}}); } for(auto x:firstGraph[v]){ firstDfs(x); } } void secondDfs(int v){ if(processed[v]){ return; } processed[v]=1; for(auto x:targetGraph[v]){ secondDfs(x); } if(v!=1 && targetGraph[v].count(1)==0){ result.push_back({'-',{1,v}}); } } int main(){ ios::sync_with_stdio(0); cin.tie(0); int n; cin>>n; int ms; cin>>ms; for(int i=0;i<ms;i++){ int a1,b1; cin>>a1>>b1; firstGraph[a1].insert(b1); firstGraph[b1].insert(a1); } int md; cin>>md; for(int i=0;i<md;i++){ int a1,b1; cin>>a1>>b1; targetGraph[a1].insert(b1); targetGraph[b1].insert(a1); } //first step -> connect everything to 1 firstDfs(1); for(int j=2;j<=n;j++){ for(auto x:firstGraph[j]){ if(x!=1 && targetGraph[j].count(x)==0){ //need to remove edge x<->j in the first graph result.push_back({'-',{j,x}}); } } for(auto x:targetGraph[j]){ if(x!=1 && firstGraph[j].count(x)==0){ //need to add edge x<->j in the first graph result.push_back({'+',{j,x}}); } } } for(int i=1;i<=n;i++){ processed[i]=0; } //last step -> remove unnecesary edges connecting to 1 secondDfs(1); cout<<result.size()<<"\n"; for(auto x: result){ cout<<x.first<<" "<<x.second.first<<" "<<x.second.second<<"\n"; } }
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 91 92 93 94 95 96 97 98 99 100 101 | #include<iostream> #include<vector> #include<algorithm> #include<map> #include<set> using namespace std; #define MAXS 50010 set<int> firstGraph[MAXS]; set<int> targetGraph[MAXS]; int processed[MAXS]; vector<pair<char,pair<int,int> > > result; void firstDfs(int v){ if(processed[v]){ return; } processed[v]=1; if(v!=1 && firstGraph[v].count(1)==0){ result.push_back({'+',{1,v}}); } for(auto x:firstGraph[v]){ firstDfs(x); } } void secondDfs(int v){ if(processed[v]){ return; } processed[v]=1; for(auto x:targetGraph[v]){ secondDfs(x); } if(v!=1 && targetGraph[v].count(1)==0){ result.push_back({'-',{1,v}}); } } int main(){ ios::sync_with_stdio(0); cin.tie(0); int n; cin>>n; int ms; cin>>ms; for(int i=0;i<ms;i++){ int a1,b1; cin>>a1>>b1; firstGraph[a1].insert(b1); firstGraph[b1].insert(a1); } int md; cin>>md; for(int i=0;i<md;i++){ int a1,b1; cin>>a1>>b1; targetGraph[a1].insert(b1); targetGraph[b1].insert(a1); } //first step -> connect everything to 1 firstDfs(1); for(int j=2;j<=n;j++){ for(auto x:firstGraph[j]){ if(x!=1 && targetGraph[j].count(x)==0){ //need to remove edge x<->j in the first graph result.push_back({'-',{j,x}}); } } for(auto x:targetGraph[j]){ if(x!=1 && firstGraph[j].count(x)==0){ //need to add edge x<->j in the first graph result.push_back({'+',{j,x}}); } } } for(int i=1;i<=n;i++){ processed[i]=0; } //last step -> remove unnecesary edges connecting to 1 secondDfs(1); cout<<result.size()<<"\n"; for(auto x: result){ cout<<x.first<<" "<<x.second.first<<" "<<x.second.second<<"\n"; } } |