#include<bits/stdc++.h> using namespace std; #ifdef DEBUG auto&operator <<(auto& o, pair<auto, auto> p) {return o<<"("<<p.first<<", "<<p.second<<")";} auto operator <<(auto& o, auto x)->decltype(x.end(), o) {o<<"{"; for(auto v : x) o<<v<<", "; return o<<"}";} #define debug(X) cout<<"["#X"]"<<X<<endl; #else #define debug(X) {} #endif #define int long long struct manager { set<pair<int, int> > allEdges; set<pair<int, int> > goodEdges; set<pair<int, int> > orgEdges; vector<pair<char, pair<int, int> > > storage; void add(int a, int b, bool f = true) { if(a > b) swap(a, b); if(a == b) return; if(allEdges.find({a, b}) != allEdges.end()) return; allEdges.insert({a, b}); if(f) storage.push_back({'+', {a, b}}); return; } void sub(int a, int b) { if(a > b) swap(a, b); if(a == b) return; if(allEdges.find({a, b}) == allEdges.end()) return; if(goodEdges.find({a, b}) != goodEdges.end()) return; allEdges.erase({a, b}); storage.push_back({'-', {a, b}}); return; } void addGood(int a, int b, bool f = 0) { if(a > b) swap(a, b); if(a == b) return; goodEdges.insert({a, b}); if(f) orgEdges.insert({a, b}); return; } void subGood(int a, int b) { if(a > b) swap(a, b); if(a == b) return; if(orgEdges.find({a, b}) != orgEdges.end()) return; if(goodEdges.find({a, b}) != goodEdges.end()) goodEdges.erase({a, b}); return; } void Print() { cout<<storage.size()<<"\n"; for(auto v : storage) cout<<v.first<<" "<<v.second.first<<" "<<v.second.second<<"\n"; } }; int32_t main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n; cin>>n; vector<vector<int> > graph(n+1); vector<vector<int> > ograph(n+1); manager M; auto getGraph = [&](vector<vector<int> >& g, bool f = 1) { int m; cin>>m; for(int i=0;i<m;i++) { int a, b; cin>>a>>b; g[a].push_back(b); g[b].push_back(a); if(f == 1) M.add(a, b, 0); if(f == 0) M.addGood(a, b, true); } }; getGraph(graph); getGraph(ograph, 0); int root, minn = 1e9+7; for(int i=1;i<=n;i++) if(graph[i].size() < minn) minn = graph[i].size(), root = i; debug(root); vector<bool> vis(n+1, false); function<void(int)> addScaff = [&](int a) { vis[a] = true; M.add(root, a); for(auto v : graph[a]) if(!vis[v]) addScaff(v); }; addScaff(root); for(auto v : M.goodEdges) M.add(v.first, v.second); fill(vis.begin(), vis.end(), false); function<void(int)> OaddScaff = [&](int a) { vis[a] = true; M.add(root, a); M.addGood(root, a); for(auto v : ograph[a]) if(!vis[v]) OaddScaff(v); }; OaddScaff(root); auto copy = M.allEdges; for(auto v : copy) M.sub(v.first, v.second); fill(vis.begin(), vis.end(), false); function<void(int)> subScaff = [&](int a) { vis[a] = true; for(auto v : ograph[a]) if(!vis[v]) subScaff(v); M.subGood(root, a); M.sub(root, a); }; subScaff(root); M.Print(); }
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 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 | #include<bits/stdc++.h> using namespace std; #ifdef DEBUG auto&operator <<(auto& o, pair<auto, auto> p) {return o<<"("<<p.first<<", "<<p.second<<")";} auto operator <<(auto& o, auto x)->decltype(x.end(), o) {o<<"{"; for(auto v : x) o<<v<<", "; return o<<"}";} #define debug(X) cout<<"["#X"]"<<X<<endl; #else #define debug(X) {} #endif #define int long long struct manager { set<pair<int, int> > allEdges; set<pair<int, int> > goodEdges; set<pair<int, int> > orgEdges; vector<pair<char, pair<int, int> > > storage; void add(int a, int b, bool f = true) { if(a > b) swap(a, b); if(a == b) return; if(allEdges.find({a, b}) != allEdges.end()) return; allEdges.insert({a, b}); if(f) storage.push_back({'+', {a, b}}); return; } void sub(int a, int b) { if(a > b) swap(a, b); if(a == b) return; if(allEdges.find({a, b}) == allEdges.end()) return; if(goodEdges.find({a, b}) != goodEdges.end()) return; allEdges.erase({a, b}); storage.push_back({'-', {a, b}}); return; } void addGood(int a, int b, bool f = 0) { if(a > b) swap(a, b); if(a == b) return; goodEdges.insert({a, b}); if(f) orgEdges.insert({a, b}); return; } void subGood(int a, int b) { if(a > b) swap(a, b); if(a == b) return; if(orgEdges.find({a, b}) != orgEdges.end()) return; if(goodEdges.find({a, b}) != goodEdges.end()) goodEdges.erase({a, b}); return; } void Print() { cout<<storage.size()<<"\n"; for(auto v : storage) cout<<v.first<<" "<<v.second.first<<" "<<v.second.second<<"\n"; } }; int32_t main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n; cin>>n; vector<vector<int> > graph(n+1); vector<vector<int> > ograph(n+1); manager M; auto getGraph = [&](vector<vector<int> >& g, bool f = 1) { int m; cin>>m; for(int i=0;i<m;i++) { int a, b; cin>>a>>b; g[a].push_back(b); g[b].push_back(a); if(f == 1) M.add(a, b, 0); if(f == 0) M.addGood(a, b, true); } }; getGraph(graph); getGraph(ograph, 0); int root, minn = 1e9+7; for(int i=1;i<=n;i++) if(graph[i].size() < minn) minn = graph[i].size(), root = i; debug(root); vector<bool> vis(n+1, false); function<void(int)> addScaff = [&](int a) { vis[a] = true; M.add(root, a); for(auto v : graph[a]) if(!vis[v]) addScaff(v); }; addScaff(root); for(auto v : M.goodEdges) M.add(v.first, v.second); fill(vis.begin(), vis.end(), false); function<void(int)> OaddScaff = [&](int a) { vis[a] = true; M.add(root, a); M.addGood(root, a); for(auto v : ograph[a]) if(!vis[v]) OaddScaff(v); }; OaddScaff(root); auto copy = M.allEdges; for(auto v : copy) M.sub(v.first, v.second); fill(vis.begin(), vis.end(), false); function<void(int)> subScaff = [&](int a) { vis[a] = true; for(auto v : ograph[a]) if(!vis[v]) subScaff(v); M.subGood(root, a); M.sub(root, a); }; subScaff(root); M.Print(); } |