/* Things to notice: 1. do not calculate useless values 2. do not use similar names Things to check: 1. submit the correct file 2. time (it is log^2 or log) 3. memory 4. prove your naive thoughts 5. long long 6. corner case like n=0,1,inf or n=m 7. check if there is a mistake in the ds or other tools you use 8. fileio in some oi-contest 9. module on time 10. the number of a same divisor in a math problem 11. multi-information and queries for dp and ds problems */ #include<bits/stdc++.h> using namespace std; #define fi first #define se second #define pii pair<int,int> #define mp make_pair #define pb push_back const int mod=998244353; const int inf=0x3f3f3f3f; int n,m1,m2; vector <int> g1[30005],g2[30005]; vector <array<int,3> > ans; map <pii,int> ma1,ma2; void rev(int u,int v) { if(u>v) swap(u,v); if(!ma1[mp(u,v)]) ma1[mp(u,v)]=1,ans.pb({1,u,v}); else ma1[mp(u,v)]=0,ans.pb({-1,u,v}); } int vis[30005]; vector <int> dfn; void dfs0(int u,int gid) { vis[u]=1,dfn.pb(u); vector <int> adj=(gid==1?g1[u]:g2[u]); for(int i=0;i<adj.size();i++) { int v=adj[i]; if(vis[v]) continue; dfs0(v,gid); } } void solve() { cin>>n>>m1; while(m1--) { int u,v; cin>>u>>v; if(u>v) swap(u,v); ma1[mp(u,v)]=1,g1[u].pb(v),g1[v].pb(u); } cin>>m2; while(m2--) { int u,v; cin>>u>>v; if(u>v) swap(u,v); ma2[mp(u,v)]=1,g2[u].pb(v),g2[v].pb(u); } dfs0(1,1); for(int i=1;i<dfn.size();i++) { int v=dfn[i]; if(!ma1[mp(1,v)]) rev(1,v); } for(auto it=ma1.begin();it!=ma1.end();it++) if(it->se) { pii e=(it->fi); if(e.fi==1) continue; rev(e.fi,e.se); } for(auto it=ma2.begin();it!=ma2.end();it++) { pii e=(it->fi); if(e.fi==1) continue; rev(e.fi,e.se); } memset(vis,0,sizeof(vis)),dfn.clear(); dfs0(1,2); for(int i=(int)(dfn.size())-1;i>=1;i--) { int v=dfn[i]; if(!ma2[mp(1,v)]) rev(1,v); } cout<<ans.size()<<"\n"; for(int i=0;i<ans.size();i++) { if(ans[i][0]==1) cout<<"+ "; else cout<<"- "; cout<<ans[i][1]<<" "<<ans[i][2]<<"\n"; } } signed main() { ios::sync_with_stdio(0); cin.tie(0); int _=1; // cin>>_; while(_--) solve(); 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 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 | /* Things to notice: 1. do not calculate useless values 2. do not use similar names Things to check: 1. submit the correct file 2. time (it is log^2 or log) 3. memory 4. prove your naive thoughts 5. long long 6. corner case like n=0,1,inf or n=m 7. check if there is a mistake in the ds or other tools you use 8. fileio in some oi-contest 9. module on time 10. the number of a same divisor in a math problem 11. multi-information and queries for dp and ds problems */ #include<bits/stdc++.h> using namespace std; #define fi first #define se second #define pii pair<int,int> #define mp make_pair #define pb push_back const int mod=998244353; const int inf=0x3f3f3f3f; int n,m1,m2; vector <int> g1[30005],g2[30005]; vector <array<int,3> > ans; map <pii,int> ma1,ma2; void rev(int u,int v) { if(u>v) swap(u,v); if(!ma1[mp(u,v)]) ma1[mp(u,v)]=1,ans.pb({1,u,v}); else ma1[mp(u,v)]=0,ans.pb({-1,u,v}); } int vis[30005]; vector <int> dfn; void dfs0(int u,int gid) { vis[u]=1,dfn.pb(u); vector <int> adj=(gid==1?g1[u]:g2[u]); for(int i=0;i<adj.size();i++) { int v=adj[i]; if(vis[v]) continue; dfs0(v,gid); } } void solve() { cin>>n>>m1; while(m1--) { int u,v; cin>>u>>v; if(u>v) swap(u,v); ma1[mp(u,v)]=1,g1[u].pb(v),g1[v].pb(u); } cin>>m2; while(m2--) { int u,v; cin>>u>>v; if(u>v) swap(u,v); ma2[mp(u,v)]=1,g2[u].pb(v),g2[v].pb(u); } dfs0(1,1); for(int i=1;i<dfn.size();i++) { int v=dfn[i]; if(!ma1[mp(1,v)]) rev(1,v); } for(auto it=ma1.begin();it!=ma1.end();it++) if(it->se) { pii e=(it->fi); if(e.fi==1) continue; rev(e.fi,e.se); } for(auto it=ma2.begin();it!=ma2.end();it++) { pii e=(it->fi); if(e.fi==1) continue; rev(e.fi,e.se); } memset(vis,0,sizeof(vis)),dfn.clear(); dfs0(1,2); for(int i=(int)(dfn.size())-1;i>=1;i--) { int v=dfn[i]; if(!ma2[mp(1,v)]) rev(1,v); } cout<<ans.size()<<"\n"; for(int i=0;i<ans.size();i++) { if(ans[i][0]==1) cout<<"+ "; else cout<<"- "; cout<<ans[i][1]<<" "<<ans[i][2]<<"\n"; } } signed main() { ios::sync_with_stdio(0); cin.tie(0); int _=1; // cin>>_; while(_--) solve(); return 0; } |