#include<bits/stdc++.h> using namespace std; #ifdef LOCAL template <class T, class U> auto &operator<<(ostream &out, pair<T, U> a) { return out << "(" << a.first << ", " << a.second << ")"; } template <class T, class = class enable_if<!is_same<T, string>(), class T::iterator>::type> auto &operator<<(ostream &out, T a) { out << "{"; bool fi = 1; for(auto b : a) { if(fi) {out<<b; fi=0;} else out<<", "<<b; } return out << "}"; } template <class T, class X, class Y> auto &operator<<(ostream &out, const priority_queue<T,X,Y>& a) {auto b = a; vector<T> v; while(!b.empty()) {v.push_back(b.top()); b.pop();} return out<<v; } void dump(){ cerr << "\e[39m"<<"\n"; } template <class T, class... Ts> void dump(T a, Ts... x) { cerr << a << ", "; dump(x...); } #define debug(...) cerr << "\e[91m"<<__func__<<":"<<__LINE__<<"\t"<<"[" #__VA_ARGS__ "]: ", dump(__VA_ARGS__) #else #define debug(...) ; #endif template<class C> C reversed(C c) {reverse(c.begin(),c.end()); return c;} #define rep(i, n) for(int i=0;i<(int)(n);i++) #define all(X) (X).begin(), (X).end() #define mp make_pair #define st first #define nd second typedef long long ll; typedef pair<int,int> pii; struct Operation { char type; int a, b; }; int32_t main(){ ios::sync_with_stdio(false); cin.tie(nullptr); int n, ms, md; cin >> n >> ms; vector<pair<int, int>> start_edges(ms); vector<vector<int>> g(n+1); vector<vector<int>> g2(n+1); queue<int> bfs_q; vector<bool> visited(n+1); visited[1] = true; rep(i, ms) { int a, b; cin >> a >> b; if (a > b) swap(a, b); start_edges[i] = mp(a, b); g[a].push_back(b); g[b].push_back(a); if (a == 1) { bfs_q.push(b); visited[b] = true; } } vector<Operation> res; vector<pair<int, int>> to_delete; while (!bfs_q.empty()) { int s = bfs_q.front(); bfs_q.pop(); for (int v: g[s]) { if (!visited[v]) { visited[v] = true; bfs_q.push(v); res.push_back({'+', 1, v}); start_edges.push_back({1, v}); } } } sort(all(start_edges)); cin >> md; vector<pair<int, int>> dest_edges(md); rep(i, md) { int a, b; cin >> a >> b; if (a > b) swap(a, b); dest_edges[i] = mp(a, b); if (!binary_search(all(start_edges), mp(a, b))) { res.push_back({'+', a, b}); } g2[a].push_back(b); g2[b].push_back(a); } sort(all(dest_edges)); for (auto [a, b]: start_edges) { if (a != 1 && !binary_search(all(dest_edges), mp(a, b))) { res.push_back({'-', a, b}); } } visited.assign(n+1, false); visited[1] = true; for (int v: g2[1]) { if (!visited[v]) { visited[v] = true; bfs_q.push(v); } } while (!bfs_q.empty()) { int s = bfs_q.front(); bfs_q.pop(); for (int v: g2[s]) { if (!visited[v]) { visited[v] = true; bfs_q.push(v); to_delete.push_back({1, v}); } } } while (!to_delete.empty()) { auto [a, b] = to_delete.back(); to_delete.pop_back(); if (!binary_search(all(dest_edges), mp(a, b))) { res.push_back({'-', a, b}); } } cout << res.size() << "\n"; for (auto op: res) { cout << op.type << " " << op.a << " " << op.b << "\n"; } 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 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 | #include<bits/stdc++.h> using namespace std; #ifdef LOCAL template <class T, class U> auto &operator<<(ostream &out, pair<T, U> a) { return out << "(" << a.first << ", " << a.second << ")"; } template <class T, class = class enable_if<!is_same<T, string>(), class T::iterator>::type> auto &operator<<(ostream &out, T a) { out << "{"; bool fi = 1; for(auto b : a) { if(fi) {out<<b; fi=0;} else out<<", "<<b; } return out << "}"; } template <class T, class X, class Y> auto &operator<<(ostream &out, const priority_queue<T,X,Y>& a) {auto b = a; vector<T> v; while(!b.empty()) {v.push_back(b.top()); b.pop();} return out<<v; } void dump(){ cerr << "\e[39m"<<"\n"; } template <class T, class... Ts> void dump(T a, Ts... x) { cerr << a << ", "; dump(x...); } #define debug(...) cerr << "\e[91m"<<__func__<<":"<<__LINE__<<"\t"<<"[" #__VA_ARGS__ "]: ", dump(__VA_ARGS__) #else #define debug(...) ; #endif template<class C> C reversed(C c) {reverse(c.begin(),c.end()); return c;} #define rep(i, n) for(int i=0;i<(int)(n);i++) #define all(X) (X).begin(), (X).end() #define mp make_pair #define st first #define nd second typedef long long ll; typedef pair<int,int> pii; struct Operation { char type; int a, b; }; int32_t main(){ ios::sync_with_stdio(false); cin.tie(nullptr); int n, ms, md; cin >> n >> ms; vector<pair<int, int>> start_edges(ms); vector<vector<int>> g(n+1); vector<vector<int>> g2(n+1); queue<int> bfs_q; vector<bool> visited(n+1); visited[1] = true; rep(i, ms) { int a, b; cin >> a >> b; if (a > b) swap(a, b); start_edges[i] = mp(a, b); g[a].push_back(b); g[b].push_back(a); if (a == 1) { bfs_q.push(b); visited[b] = true; } } vector<Operation> res; vector<pair<int, int>> to_delete; while (!bfs_q.empty()) { int s = bfs_q.front(); bfs_q.pop(); for (int v: g[s]) { if (!visited[v]) { visited[v] = true; bfs_q.push(v); res.push_back({'+', 1, v}); start_edges.push_back({1, v}); } } } sort(all(start_edges)); cin >> md; vector<pair<int, int>> dest_edges(md); rep(i, md) { int a, b; cin >> a >> b; if (a > b) swap(a, b); dest_edges[i] = mp(a, b); if (!binary_search(all(start_edges), mp(a, b))) { res.push_back({'+', a, b}); } g2[a].push_back(b); g2[b].push_back(a); } sort(all(dest_edges)); for (auto [a, b]: start_edges) { if (a != 1 && !binary_search(all(dest_edges), mp(a, b))) { res.push_back({'-', a, b}); } } visited.assign(n+1, false); visited[1] = true; for (int v: g2[1]) { if (!visited[v]) { visited[v] = true; bfs_q.push(v); } } while (!bfs_q.empty()) { int s = bfs_q.front(); bfs_q.pop(); for (int v: g2[s]) { if (!visited[v]) { visited[v] = true; bfs_q.push(v); to_delete.push_back({1, v}); } } } while (!to_delete.empty()) { auto [a, b] = to_delete.back(); to_delete.pop_back(); if (!binary_search(all(dest_edges), mp(a, b))) { res.push_back({'-', a, b}); } } cout << res.size() << "\n"; for (auto op: res) { cout << op.type << " " << op.a << " " << op.b << "\n"; } return 0; } |