#include<bits/stdc++.h> #define int long long using namespace std; map<pair<int,int>,int> usuniecie,dodanie,kraw,zbior; struct wi{ vector<int> Q,Q2; int odw=0; int odw2=0; }*w; vector<pair<int,int> > dodaj,usun,usunn; void dfs(int x) { w[x].odw=1; for(int i=0;i<w[x].Q.size();i++) { int pom=w[x].Q[i]; if(w[pom].odw) continue; if(kraw.find({1,pom})==kraw.end()) dodaj.push_back({1,pom}); dfs(pom); } } void dfs2(int x) { w[x].odw2=1; for(int i=0;i<w[x].Q2.size();i++) { int pom=w[x].Q2[i]; if(w[pom].odw2) continue; if(zbior.find({1,pom})==zbior.end()) usunn.push_back({1,pom}); dfs2(pom); } } int32_t main() { cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(0); int n,m1,m2,x,y; cin>>n; w=new wi[n+3]; cin>>m1; for(int i=1;i<=m1;i++) { int x,y; cin>>x>>y; if(x>y) swap(x,y); w[x].Q.push_back(y); w[y].Q.push_back(x); kraw[{x,y}]=1; usuniecie[{x,y}]=1; } cin>>m2; for(int i=1;i<=m2;i++) { int x,y; cin>>x>>y; if(x>y) swap(x,y); zbior[{x,y}]=1; zbior[{y,x}]=1; if(kraw.find({x,y})==kraw.end()) dodanie[{x,y}]=1; else usuniecie.erase({x,y}); w[x].Q2.push_back(y); w[y].Q2.push_back(x); } dfs(1); dfs2(1); for(auto it=dodanie.begin();it!=dodanie.end();it++) if(it->first.first!=1) dodaj.push_back({it->first.first,it->first.second}); for(auto it=usuniecie.begin();it!=usuniecie.end();it++) if(it->first.first!=1) usun.push_back({it->first.first,it->first.second}); for(int i=usunn.size()-1;i>=0;i--) usun.push_back(usunn[i]); cout<<dodaj.size()+usun.size()<<"\n"; for(int i=0;i<dodaj.size();i++) cout<<"+ "<<dodaj[i].first<<" "<<dodaj[i].second<<"\n"; for(int i=0;i<usun.size();i++) cout<<"- "<<usun[i].first<<" "<<usun[i].second<<"\n"; 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 | #include<bits/stdc++.h> #define int long long using namespace std; map<pair<int,int>,int> usuniecie,dodanie,kraw,zbior; struct wi{ vector<int> Q,Q2; int odw=0; int odw2=0; }*w; vector<pair<int,int> > dodaj,usun,usunn; void dfs(int x) { w[x].odw=1; for(int i=0;i<w[x].Q.size();i++) { int pom=w[x].Q[i]; if(w[pom].odw) continue; if(kraw.find({1,pom})==kraw.end()) dodaj.push_back({1,pom}); dfs(pom); } } void dfs2(int x) { w[x].odw2=1; for(int i=0;i<w[x].Q2.size();i++) { int pom=w[x].Q2[i]; if(w[pom].odw2) continue; if(zbior.find({1,pom})==zbior.end()) usunn.push_back({1,pom}); dfs2(pom); } } int32_t main() { cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(0); int n,m1,m2,x,y; cin>>n; w=new wi[n+3]; cin>>m1; for(int i=1;i<=m1;i++) { int x,y; cin>>x>>y; if(x>y) swap(x,y); w[x].Q.push_back(y); w[y].Q.push_back(x); kraw[{x,y}]=1; usuniecie[{x,y}]=1; } cin>>m2; for(int i=1;i<=m2;i++) { int x,y; cin>>x>>y; if(x>y) swap(x,y); zbior[{x,y}]=1; zbior[{y,x}]=1; if(kraw.find({x,y})==kraw.end()) dodanie[{x,y}]=1; else usuniecie.erase({x,y}); w[x].Q2.push_back(y); w[y].Q2.push_back(x); } dfs(1); dfs2(1); for(auto it=dodanie.begin();it!=dodanie.end();it++) if(it->first.first!=1) dodaj.push_back({it->first.first,it->first.second}); for(auto it=usuniecie.begin();it!=usuniecie.end();it++) if(it->first.first!=1) usun.push_back({it->first.first,it->first.second}); for(int i=usunn.size()-1;i>=0;i--) usun.push_back(usunn[i]); cout<<dodaj.size()+usun.size()<<"\n"; for(int i=0;i<dodaj.size();i++) cout<<"+ "<<dodaj[i].first<<" "<<dodaj[i].second<<"\n"; for(int i=0;i<usun.size();i++) cout<<"- "<<usun[i].first<<" "<<usun[i].second<<"\n"; return 0; } |