#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace std; using namespace __gnu_pbds; template <typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; const int N = 10010; vector<bool> vis; vector< tuple<int, int, int> > res; vector<int> GA[N], GB[N]; bool edges[N][N]; void dfsA(int x, int start) { vis[x] = true; for (int i : GA[x]) { if (!vis[i]) { if (!edges[start][i]) { res.emplace_back(0, start, i); edges[start][i] = edges[i][start] = true; } dfsA(i, start); } } } void dfsB(int x, int start) { vis[x] = true; bool neigh = false; for (int i : GB[x]) { neigh |= (i == start); if (!vis[i]) { dfsB(i, start); } } if (!neigh && x != start && edges[start][x]) { res.emplace_back(1, start, x); edges[start][x] = edges[x][start] = false; } } void solve(int _id) { int n; cin >> n; int m1, m2; cin >> m1; for (int i = 0; i < m1; i++) { int a, b; cin >> a >> b; GA[a].push_back(b); GA[b].push_back(a); edges[a][b] = edges[b][a] = true; } cin >> m2; for (int i = 0; i < m2; i++) { int a, b; cin >> a >> b; GB[a].push_back(b); GB[b].push_back(a); } vis.resize(n + 1); for (int i = 1; i <= n; i++) { std::fill(vis.begin(), vis.end(), 0); dfsA(i, i); } for (int i = 1; i <= n; i++) { std::fill(vis.begin(), vis.end(), 0); dfsB(i, i); } cout << res.size() << "\n"; for (auto [c, a, b] : res) { cout << (c == 0 ? "+ " : "- ") << a << " " << b << "\n"; } } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int t = 1; // freopen("../d/1.in","r",stdin); // freopen("../wzo.out","w",stdout); // cin >> t; for (int i = 1; i <= t; i++) { solve(i); } return 0; } /* 1 cc 1 1 1 a ca cca */
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 | #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace std; using namespace __gnu_pbds; template <typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; const int N = 10010; vector<bool> vis; vector< tuple<int, int, int> > res; vector<int> GA[N], GB[N]; bool edges[N][N]; void dfsA(int x, int start) { vis[x] = true; for (int i : GA[x]) { if (!vis[i]) { if (!edges[start][i]) { res.emplace_back(0, start, i); edges[start][i] = edges[i][start] = true; } dfsA(i, start); } } } void dfsB(int x, int start) { vis[x] = true; bool neigh = false; for (int i : GB[x]) { neigh |= (i == start); if (!vis[i]) { dfsB(i, start); } } if (!neigh && x != start && edges[start][x]) { res.emplace_back(1, start, x); edges[start][x] = edges[x][start] = false; } } void solve(int _id) { int n; cin >> n; int m1, m2; cin >> m1; for (int i = 0; i < m1; i++) { int a, b; cin >> a >> b; GA[a].push_back(b); GA[b].push_back(a); edges[a][b] = edges[b][a] = true; } cin >> m2; for (int i = 0; i < m2; i++) { int a, b; cin >> a >> b; GB[a].push_back(b); GB[b].push_back(a); } vis.resize(n + 1); for (int i = 1; i <= n; i++) { std::fill(vis.begin(), vis.end(), 0); dfsA(i, i); } for (int i = 1; i <= n; i++) { std::fill(vis.begin(), vis.end(), 0); dfsB(i, i); } cout << res.size() << "\n"; for (auto [c, a, b] : res) { cout << (c == 0 ? "+ " : "- ") << a << " " << b << "\n"; } } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int t = 1; // freopen("../d/1.in","r",stdin); // freopen("../wzo.out","w",stdout); // cin >> t; for (int i = 1; i <= t; i++) { solve(i); } return 0; } /* 1 cc 1 1 1 a ca cca */ |