#include<bits/stdc++.h> using namespace std; int n, m1, m2; set<pair<int, int>> edge_set1; vector<int> graph1[30100]; set<pair<int, int>> edge_set2; vector<int> graph2[30100]; bool visited[30100]; bool visited2[30100]; queue<pair<int, int>> added; stack<pair<int, int>> deleted; void dfs(int v) { visited[v] = true; for(int w : graph1[v]) { if(!visited[w]) { if(!edge_set1.contains({1,w})) added.push({1,w}); dfs(w); } } } void dfs2(int v) { visited2[v] = true; for(int w : graph2[v]) { if(!visited2[w]) { if(!edge_set2.contains({1,w})) deleted.push({1,w}); dfs2(w); } } } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cin>>n; int x,y; cin>>m1; for(int i=0;i<m1;i++) { cin>>x>>y; edge_set1.insert({x,y}); edge_set1.insert({y,x}); graph1[x].push_back(y); graph1[y].push_back(x); } cin>>m2; for(int i=0;i<m2;i++) { cin>>x>>y; edge_set2.insert({x,y}); edge_set2.insert({y,x}); graph2[x].push_back(y); graph2[y].push_back(x); } dfs(1); dfs2(1); for(int i=2;i<=n;i++) { for(int w : graph1[i]) { if(!edge_set2.contains({i,w}) && i<w) deleted.push({i,w}); } } for(int i=2;i<=n;i++) { for(int w : graph2[i]) { if(!edge_set1.contains({i,w}) && i<w) added.push({i,w}); } } cout<<added.size() + deleted.size()<<endl; while(!added.empty()) { pair<int, int> w = added.front(); added.pop(); cout<<"+ "<<w.first<<" "<<w.second<<endl; } while(!deleted.empty()) { pair<int, int> w = deleted.top(); deleted.pop(); cout<<"- "<<w.first<<" "<<w.second<<endl; } 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 | #include<bits/stdc++.h> using namespace std; int n, m1, m2; set<pair<int, int>> edge_set1; vector<int> graph1[30100]; set<pair<int, int>> edge_set2; vector<int> graph2[30100]; bool visited[30100]; bool visited2[30100]; queue<pair<int, int>> added; stack<pair<int, int>> deleted; void dfs(int v) { visited[v] = true; for(int w : graph1[v]) { if(!visited[w]) { if(!edge_set1.contains({1,w})) added.push({1,w}); dfs(w); } } } void dfs2(int v) { visited2[v] = true; for(int w : graph2[v]) { if(!visited2[w]) { if(!edge_set2.contains({1,w})) deleted.push({1,w}); dfs2(w); } } } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cin>>n; int x,y; cin>>m1; for(int i=0;i<m1;i++) { cin>>x>>y; edge_set1.insert({x,y}); edge_set1.insert({y,x}); graph1[x].push_back(y); graph1[y].push_back(x); } cin>>m2; for(int i=0;i<m2;i++) { cin>>x>>y; edge_set2.insert({x,y}); edge_set2.insert({y,x}); graph2[x].push_back(y); graph2[y].push_back(x); } dfs(1); dfs2(1); for(int i=2;i<=n;i++) { for(int w : graph1[i]) { if(!edge_set2.contains({i,w}) && i<w) deleted.push({i,w}); } } for(int i=2;i<=n;i++) { for(int w : graph2[i]) { if(!edge_set1.contains({i,w}) && i<w) added.push({i,w}); } } cout<<added.size() + deleted.size()<<endl; while(!added.empty()) { pair<int, int> w = added.front(); added.pop(); cout<<"+ "<<w.first<<" "<<w.second<<endl; } while(!deleted.empty()) { pair<int, int> w = deleted.top(); deleted.pop(); cout<<"- "<<w.first<<" "<<w.second<<endl; } return 0; } |