#include <bits/stdc++.h> using namespace std; int n; int s1, s2; vector <int> graf1[30100]; vector <int> graf2[30100]; vector <int> result; int jed1[30100]; int jed2[30100]; map<pair<int, int>, int> M; int odw[30100]; void dfs1(int v){ odw[v] = 1; result.push_back(v); for(int i: graf1[v]){ if(odw[i] == 0) dfs1(i); } } void dfs2(int v){ odw[v] = 1; result.push_back(v); for(int i: graf2[v]){ if(odw[i] == 0) dfs2(i); } } vector<pair<int, pair<int, int>>> wynik; int main(){ cin>>n; cin>>s1; for(int i=1;i<=s1;i++){ int x,y; cin>>x>>y; if(y<x) swap(x,y); graf1[x].push_back(y); graf1[y].push_back(x); if(x==1){ jed1[y] = 1; } else{ M[{x,y}]--; } } cin>>s2; for(int i=1;i<=s2;i++){ int x,y; cin>>x>>y; if(y<x) swap(x,y); graf2[x].push_back(y); graf2[y].push_back(x); if(x==1){ jed2[y] = 1; } else{ M[{x,y}]++; } } dfs1(1); for(int i: result){ if(i != 1 && jed1[i] == 0) wynik.push_back({1,{1,i}}); //cout<<"+ 1 "<<i<<endl; } result.clear(); for(int i=1;i<=n;i++)odw[i] = 0; for(auto v: M){ if(v.second == 1){ //cout<<"+ "<<v.first.first<<" "<<v.first.second<<endl; wynik.push_back({1,v.first}); } if(v.second == -1){ //cout<<"- "<<v.first.first<<" "<<v.first.second<<endl; wynik.push_back({-1,v.first}); } } dfs2(1); for(int i=result.size()-1; i>=0; i--){ if(result[i] != 1 && jed2[result[i]] == 0) wynik.push_back({-1,{1,result[i]}}); //cout<<"- 1 "<<result[i]<<endl; } cout<<wynik.size()<<endl; for(auto v: wynik){ if(v.first == 1){ cout<<"+ "<<v.second.first<<" "<<v.second.second<<endl; } else{ cout<<"- "<<v.second.first<<" "<<v.second.second<<endl; } } //for(auto v: M){ // cout<<v.first.first<<" "<<v.first.second<<" "<<v.second //} }
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 <bits/stdc++.h> using namespace std; int n; int s1, s2; vector <int> graf1[30100]; vector <int> graf2[30100]; vector <int> result; int jed1[30100]; int jed2[30100]; map<pair<int, int>, int> M; int odw[30100]; void dfs1(int v){ odw[v] = 1; result.push_back(v); for(int i: graf1[v]){ if(odw[i] == 0) dfs1(i); } } void dfs2(int v){ odw[v] = 1; result.push_back(v); for(int i: graf2[v]){ if(odw[i] == 0) dfs2(i); } } vector<pair<int, pair<int, int>>> wynik; int main(){ cin>>n; cin>>s1; for(int i=1;i<=s1;i++){ int x,y; cin>>x>>y; if(y<x) swap(x,y); graf1[x].push_back(y); graf1[y].push_back(x); if(x==1){ jed1[y] = 1; } else{ M[{x,y}]--; } } cin>>s2; for(int i=1;i<=s2;i++){ int x,y; cin>>x>>y; if(y<x) swap(x,y); graf2[x].push_back(y); graf2[y].push_back(x); if(x==1){ jed2[y] = 1; } else{ M[{x,y}]++; } } dfs1(1); for(int i: result){ if(i != 1 && jed1[i] == 0) wynik.push_back({1,{1,i}}); //cout<<"+ 1 "<<i<<endl; } result.clear(); for(int i=1;i<=n;i++)odw[i] = 0; for(auto v: M){ if(v.second == 1){ //cout<<"+ "<<v.first.first<<" "<<v.first.second<<endl; wynik.push_back({1,v.first}); } if(v.second == -1){ //cout<<"- "<<v.first.first<<" "<<v.first.second<<endl; wynik.push_back({-1,v.first}); } } dfs2(1); for(int i=result.size()-1; i>=0; i--){ if(result[i] != 1 && jed2[result[i]] == 0) wynik.push_back({-1,{1,result[i]}}); //cout<<"- 1 "<<result[i]<<endl; } cout<<wynik.size()<<endl; for(auto v: wynik){ if(v.first == 1){ cout<<"+ "<<v.second.first<<" "<<v.second.second<<endl; } else{ cout<<"- "<<v.second.first<<" "<<v.second.second<<endl; } } //for(auto v: M){ // cout<<v.first.first<<" "<<v.first.second<<" "<<v.second //} } |