#include <bits/stdc++.h> #include <vector> using namespace std; //vector<array<int,2>> wrong; vector<map<int,array<int,2>>> G; vector<array<int,4>> res; void add_edge(int k1, int k2, int proof){ if(G[k1][k2][0]) return; res.push_back({'+', k1, k2, proof}); G[k1][k2][0]=1; G[k2][k1][0]=1; } void rem_edge(int k1, int k2, int proof){ if(G[k1][k2][0]==0) return; res.push_back({'-', k1, k2, proof}); G[k1][k2][0]=0; G[k2][k1][0]=0; } void dfs_add(vector<int>&vis, int v){ vis[v] = 1; for(auto edge:G[v]) if(edge.second[0]){ int u = edge.first; if(vis[u]==1) continue; add_edge(0, u, v); dfs_add(vis, u); } } void dfs_rem(vector<int>&vis, int v, int p){ vis[v] = 1; for(auto edge:G[v]) if(edge.second[0]){ int u = edge.first; if(vis[u]==1) continue; dfs_rem(vis, u, v); } if(G[v][0][1]==0 && v!=0) rem_edge(v, 0, p); } int main(){ vector<int> in; int n, m; cin>>n; in.push_back(n); G.resize(n); cin>>m; in.push_back(m); for(int i=0;i<m;i++){ int k1, k2; cin>>k1>>k2; in.push_back(k1), in.push_back(k2); k1--, k2--; G[k1][k2][0] = 1; G[k2][k1][0] = 1; } cin>>m; in.push_back(m); for(int i=0;i<m;i++){ int k1, k2; cin>>k1>>k2; in.push_back(k1), in.push_back(k2); k1--, k2--; G[k1][k2][1] = 1; G[k2][k1][1] = 1; } vector<int> vis(n,0), vis2(n,0); dfs_add(vis, 0); for(int v=0;v<n;v++) for(auto edge:G[v]) if(edge.second[1]){ add_edge(v, edge.first, 0); } for(int v=0;v<n;v++) for(auto edge:G[v]) if(edge.second[1]==0){ if(v==0 || edge.first==0) continue; rem_edge(v, edge.first, 0); } dfs_rem(vis2, 0, -1); //for(auto x:res) //cout << (char)x[0]<<" "<<x[1]+1<<" "<<x[2]+1<<endl; //for(auto x: in) cout<<x<<" "; //cout<<endl; cout << res.size()<<endl; for(auto x:res) //cout << (char)x[0]<<" "<<x[1]+1<<" "<<x[2]+1<<" "<<x[3]+1<<endl; cout << (char)x[0]<<" "<<x[1]+1<<" "<<x[2]+1<<" "<<endl; }
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 112 113 114 | #include <bits/stdc++.h> #include <vector> using namespace std; //vector<array<int,2>> wrong; vector<map<int,array<int,2>>> G; vector<array<int,4>> res; void add_edge(int k1, int k2, int proof){ if(G[k1][k2][0]) return; res.push_back({'+', k1, k2, proof}); G[k1][k2][0]=1; G[k2][k1][0]=1; } void rem_edge(int k1, int k2, int proof){ if(G[k1][k2][0]==0) return; res.push_back({'-', k1, k2, proof}); G[k1][k2][0]=0; G[k2][k1][0]=0; } void dfs_add(vector<int>&vis, int v){ vis[v] = 1; for(auto edge:G[v]) if(edge.second[0]){ int u = edge.first; if(vis[u]==1) continue; add_edge(0, u, v); dfs_add(vis, u); } } void dfs_rem(vector<int>&vis, int v, int p){ vis[v] = 1; for(auto edge:G[v]) if(edge.second[0]){ int u = edge.first; if(vis[u]==1) continue; dfs_rem(vis, u, v); } if(G[v][0][1]==0 && v!=0) rem_edge(v, 0, p); } int main(){ vector<int> in; int n, m; cin>>n; in.push_back(n); G.resize(n); cin>>m; in.push_back(m); for(int i=0;i<m;i++){ int k1, k2; cin>>k1>>k2; in.push_back(k1), in.push_back(k2); k1--, k2--; G[k1][k2][0] = 1; G[k2][k1][0] = 1; } cin>>m; in.push_back(m); for(int i=0;i<m;i++){ int k1, k2; cin>>k1>>k2; in.push_back(k1), in.push_back(k2); k1--, k2--; G[k1][k2][1] = 1; G[k2][k1][1] = 1; } vector<int> vis(n,0), vis2(n,0); dfs_add(vis, 0); for(int v=0;v<n;v++) for(auto edge:G[v]) if(edge.second[1]){ add_edge(v, edge.first, 0); } for(int v=0;v<n;v++) for(auto edge:G[v]) if(edge.second[1]==0){ if(v==0 || edge.first==0) continue; rem_edge(v, edge.first, 0); } dfs_rem(vis2, 0, -1); //for(auto x:res) //cout << (char)x[0]<<" "<<x[1]+1<<" "<<x[2]+1<<endl; //for(auto x: in) cout<<x<<" "; //cout<<endl; cout << res.size()<<endl; for(auto x:res) //cout << (char)x[0]<<" "<<x[1]+1<<" "<<x[2]+1<<" "<<x[3]+1<<endl; cout << (char)x[0]<<" "<<x[1]+1<<" "<<x[2]+1<<" "<<endl; } |