#include<bits/stdc++.h> using namespace std; using LL=long long; #define FOR(i,l,r)for(int i=(l);i<=(r);++i) #define REP(i,n)FOR(i,0,(n)-1) #define ssize(x)int(x.size()) #ifdef DEBUG auto operator<<(auto&o,auto x)->decltype(x.end(),o); auto&operator<<(auto&o,pair<auto,auto>p){return o<<"("<<p.first<<", "<<p.second<<")";} auto&operator<<(auto&o,tuple<auto,auto,auto>t){return o<<"("<<get<0>(t)<<", "<<get<1>(t)<<", "<<get<2>(t)<<")";} auto&operator<<(auto&o,tuple<auto,auto,auto,auto>t){return o<<"("<<get<0>(t)<<", "<<get<1>(t)<<", "<<get<2>(t)<<", "<<get<3>(t)<<")";} auto operator<<(auto&o,auto x)->decltype(x.end(),o){o<<"{";int i=0;for(auto e:x)o<<","+!i++<<e;return o<<"}";} #define debug(X...)cerr<<"["#X"]: ",[](auto...$){((cerr<<$<<"; "),...)<<endl;}(X) #else #define debug(...){} #endif int N; using Operation = tuple<bool, int, int>; Operation neg(Operation x) { auto [a, b, c] = x; return {a ^ 1, b, c}; } struct Graph { int n, m; vector<pair<int, int>> edges; vector<vector<int>> adj; bool connected(int a, int b) { if (a == b) return true; auto it = lower_bound(adj[a].begin(), adj[a].end(), b); return it != adj[a].end() and *it == b; } vector<Operation> get_canonical() { vector<int> depth(n); vector<bool> visited(n); vector<Operation> ret; function<void(int, int)> dfs = [&](int v, int d) { visited[v] = true; depth[v] = d; if (not connected(0, v)) { ret.emplace_back(true, 0, v); } for (auto x : adj[v]) { if (not visited[x]) { dfs(x, d + 1); } else if (depth[x] < depth[v] and x != 0) { ret.emplace_back(false, x, v); } } }; dfs(0, 0); return ret; } }; auto&operator>>(auto&i,Graph& g) { g.n = N; cin >> g.m; g.edges.resize(g.m); g.adj.resize(g.n); for (auto& [a, b] : g.edges) { cin >> a >> b; --a, --b; if (a > b) swap(a, b); g.adj[a].emplace_back(b); g.adj[b].emplace_back(a); } for (auto& v : g.adj) sort(v.begin(), v.end()); return i; } auto&operator<<(auto&o,const Graph& g) { return o << make_tuple(g.n, g.m, g.edges, g.adj); } int main() { cin.tie(0)->sync_with_stdio(0); cin >> N; Graph one, two; cin >> one >> two; debug(one, two); auto left = one.get_canonical(); auto right = two.get_canonical(); reverse(right.begin(), right.end()); for (auto& x : right) x = neg(x); debug(left, right); vector<Operation> ans; for (auto v : {left, right}) ans.insert(ans.end(), v.begin(), v.end()); for (auto& [_, a, b] : ans) if (a > b) swap(a, b); debug(ans); #ifdef LOCAL assert(ssize(ans) <= one.n + one.m + two.n + two.m); set<pair<int, int>> source(one.edges.begin(), one.edges.end()); set<pair<int, int>> target(two.edges.begin(), two.edges.end()); debug(source, target); auto edge_exists = [&](int a, int b) { if (a > b) swap(a, b); return source.contains(pair(a, b)); }; for (auto [type, a, b] : ans) { assert(min(a, b) >= 0 and max(a, b) < one.n and a != b); if (a > b) swap(a, b); auto c_exists = [&] { REP(c, one.n) { if (c == a or c == b) continue; if (edge_exists(a, c) and edge_exists(b, c)) return true; } return false; }; assert(c_exists()); if (type) { assert(not edge_exists(a, b)); source.emplace(a, b); } else { assert(edge_exists(a, b)); source.erase(pair(a, b)); } } assert(source == target); cout << "OK\n"; #else cout << ssize(ans) << '\n'; for (auto [type, a, b] : ans) { cout << (type ? '+' : '-') << ' ' << a + 1 << ' ' << b + 1 << '\n'; } #endif }
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 | #include<bits/stdc++.h> using namespace std; using LL=long long; #define FOR(i,l,r)for(int i=(l);i<=(r);++i) #define REP(i,n)FOR(i,0,(n)-1) #define ssize(x)int(x.size()) #ifdef DEBUG auto operator<<(auto&o,auto x)->decltype(x.end(),o); auto&operator<<(auto&o,pair<auto,auto>p){return o<<"("<<p.first<<", "<<p.second<<")";} auto&operator<<(auto&o,tuple<auto,auto,auto>t){return o<<"("<<get<0>(t)<<", "<<get<1>(t)<<", "<<get<2>(t)<<")";} auto&operator<<(auto&o,tuple<auto,auto,auto,auto>t){return o<<"("<<get<0>(t)<<", "<<get<1>(t)<<", "<<get<2>(t)<<", "<<get<3>(t)<<")";} auto operator<<(auto&o,auto x)->decltype(x.end(),o){o<<"{";int i=0;for(auto e:x)o<<","+!i++<<e;return o<<"}";} #define debug(X...)cerr<<"["#X"]: ",[](auto...$){((cerr<<$<<"; "),...)<<endl;}(X) #else #define debug(...){} #endif int N; using Operation = tuple<bool, int, int>; Operation neg(Operation x) { auto [a, b, c] = x; return {a ^ 1, b, c}; } struct Graph { int n, m; vector<pair<int, int>> edges; vector<vector<int>> adj; bool connected(int a, int b) { if (a == b) return true; auto it = lower_bound(adj[a].begin(), adj[a].end(), b); return it != adj[a].end() and *it == b; } vector<Operation> get_canonical() { vector<int> depth(n); vector<bool> visited(n); vector<Operation> ret; function<void(int, int)> dfs = [&](int v, int d) { visited[v] = true; depth[v] = d; if (not connected(0, v)) { ret.emplace_back(true, 0, v); } for (auto x : adj[v]) { if (not visited[x]) { dfs(x, d + 1); } else if (depth[x] < depth[v] and x != 0) { ret.emplace_back(false, x, v); } } }; dfs(0, 0); return ret; } }; auto&operator>>(auto&i,Graph& g) { g.n = N; cin >> g.m; g.edges.resize(g.m); g.adj.resize(g.n); for (auto& [a, b] : g.edges) { cin >> a >> b; --a, --b; if (a > b) swap(a, b); g.adj[a].emplace_back(b); g.adj[b].emplace_back(a); } for (auto& v : g.adj) sort(v.begin(), v.end()); return i; } auto&operator<<(auto&o,const Graph& g) { return o << make_tuple(g.n, g.m, g.edges, g.adj); } int main() { cin.tie(0)->sync_with_stdio(0); cin >> N; Graph one, two; cin >> one >> two; debug(one, two); auto left = one.get_canonical(); auto right = two.get_canonical(); reverse(right.begin(), right.end()); for (auto& x : right) x = neg(x); debug(left, right); vector<Operation> ans; for (auto v : {left, right}) ans.insert(ans.end(), v.begin(), v.end()); for (auto& [_, a, b] : ans) if (a > b) swap(a, b); debug(ans); #ifdef LOCAL assert(ssize(ans) <= one.n + one.m + two.n + two.m); set<pair<int, int>> source(one.edges.begin(), one.edges.end()); set<pair<int, int>> target(two.edges.begin(), two.edges.end()); debug(source, target); auto edge_exists = [&](int a, int b) { if (a > b) swap(a, b); return source.contains(pair(a, b)); }; for (auto [type, a, b] : ans) { assert(min(a, b) >= 0 and max(a, b) < one.n and a != b); if (a > b) swap(a, b); auto c_exists = [&] { REP(c, one.n) { if (c == a or c == b) continue; if (edge_exists(a, c) and edge_exists(b, c)) return true; } return false; }; assert(c_exists()); if (type) { assert(not edge_exists(a, b)); source.emplace(a, b); } else { assert(edge_exists(a, b)); source.erase(pair(a, b)); } } assert(source == target); cout << "OK\n"; #else cout << ssize(ans) << '\n'; for (auto [type, a, b] : ans) { cout << (type ? '+' : '-') << ' ' << a + 1 << ' ' << b + 1 << '\n'; } #endif } |