#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cout.tie(NULL); cin.tie(NULL); srand(time(NULL)); int n; cin >> n; vector<vector<int>> start(n), goal(n); int m; cin >> m; for(int i = 0; i < m; i++) { int a, b; cin >> a >> b; a--; b--; start[a].push_back(b); start[b].push_back(a); } cin >> m; for(int i = 0; i < m; i++) { int a, b; cin >> a >> b; a--; b--; goal[a].push_back(b); goal[b].push_back(a); } for(int i = 0; i < n; i++) { sort(start[i].begin(), start[i].end()); sort(goal[i].begin(), goal[i].end()); } set<pair<int, int>> add, remove; for(int i = 0; i < n; i++) { int it1 = 0, it2 = 0; while(it1 < start[i].size() && it2 < goal[i].size()) { int u = start[i][it1], v = goal[i][it2]; if(i > u) it1++; else if (i > v) it2++; else if(u == v) { it1++; it2++; } else if (u < v) { remove.insert({i, u}); it1++; } else { add.insert({i, v}); it2++; } } while(it1 < start[i].size()) { int u = start[i][it1]; if(i < u) remove.insert({i, u}); it1++; } while(it2 < goal[i].size()){ int v = goal[i][it2]; if(i < v) add.insert({i, goal[i][it2]}); it2++; } } vector<pair<int, int>> changes1, changes2; vector<bool> visited(n, false); queue<int> q; visited[0] = true; for(auto e : start[0]) { q.push(e); visited[e] = true; } while(q.size()) { int v = q.front(); q.pop(); for(auto u : start[v]) { if(!visited[u]) { visited[u] = true; changes1.push_back({0, u}); add.erase({0, u}); q.push(u); } } } fill(visited.begin(), visited.end(), false); visited[0] = true; for(auto e : goal[0]) { q.push(e); visited[e] = true; } while(q.size()) { int v = q.front(); q.pop(); for(auto u : goal[v]) { if(!visited[u]) { visited[u] = true; changes2.push_back({0, u}); remove.erase({0, u}); q.push(u); } } } cout << (changes1.size() + changes2.size() + add.size() + remove.size()) << '\n'; for(auto change : changes1) { cout << "+ " << change.first + 1 << ' ' << change.second + 1 << '\n'; } for(auto change : add) { cout << "+ " << change.first + 1 << ' ' << change.second + 1 << '\n'; } for(auto change : remove) { cout << "- " << change.first + 1 << ' ' << change.second + 1 << '\n'; } reverse(changes2.begin(), changes2.end()); for(auto change : changes2) { cout << "- " << change.first + 1 << ' ' << change.second + 1 << '\n'; } }
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 130 131 | #include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cout.tie(NULL); cin.tie(NULL); srand(time(NULL)); int n; cin >> n; vector<vector<int>> start(n), goal(n); int m; cin >> m; for(int i = 0; i < m; i++) { int a, b; cin >> a >> b; a--; b--; start[a].push_back(b); start[b].push_back(a); } cin >> m; for(int i = 0; i < m; i++) { int a, b; cin >> a >> b; a--; b--; goal[a].push_back(b); goal[b].push_back(a); } for(int i = 0; i < n; i++) { sort(start[i].begin(), start[i].end()); sort(goal[i].begin(), goal[i].end()); } set<pair<int, int>> add, remove; for(int i = 0; i < n; i++) { int it1 = 0, it2 = 0; while(it1 < start[i].size() && it2 < goal[i].size()) { int u = start[i][it1], v = goal[i][it2]; if(i > u) it1++; else if (i > v) it2++; else if(u == v) { it1++; it2++; } else if (u < v) { remove.insert({i, u}); it1++; } else { add.insert({i, v}); it2++; } } while(it1 < start[i].size()) { int u = start[i][it1]; if(i < u) remove.insert({i, u}); it1++; } while(it2 < goal[i].size()){ int v = goal[i][it2]; if(i < v) add.insert({i, goal[i][it2]}); it2++; } } vector<pair<int, int>> changes1, changes2; vector<bool> visited(n, false); queue<int> q; visited[0] = true; for(auto e : start[0]) { q.push(e); visited[e] = true; } while(q.size()) { int v = q.front(); q.pop(); for(auto u : start[v]) { if(!visited[u]) { visited[u] = true; changes1.push_back({0, u}); add.erase({0, u}); q.push(u); } } } fill(visited.begin(), visited.end(), false); visited[0] = true; for(auto e : goal[0]) { q.push(e); visited[e] = true; } while(q.size()) { int v = q.front(); q.pop(); for(auto u : goal[v]) { if(!visited[u]) { visited[u] = true; changes2.push_back({0, u}); remove.erase({0, u}); q.push(u); } } } cout << (changes1.size() + changes2.size() + add.size() + remove.size()) << '\n'; for(auto change : changes1) { cout << "+ " << change.first + 1 << ' ' << change.second + 1 << '\n'; } for(auto change : add) { cout << "+ " << change.first + 1 << ' ' << change.second + 1 << '\n'; } for(auto change : remove) { cout << "- " << change.first + 1 << ' ' << change.second + 1 << '\n'; } reverse(changes2.begin(), changes2.end()); for(auto change : changes2) { cout << "- " << change.first + 1 << ' ' << change.second + 1 << '\n'; } } |