#include<bits/stdc++.h> #define vi vector<int> #define pii pair<int, int> #define se second #define fi first #define ALL(x) begin(x), end(x) using namespace std; vector <string> res; int n, m1, m2; void get_graph(int &m, vector<vi> &graph, map <pii, bool> &map) { cin >> m; for (int i = 0; i < m; i++) { int a, b; cin >> a >> b; if (a > b) swap(a, b); graph[a].push_back(b); graph[b].push_back(a); map[{a, b}] = true; } } void dfs1(int w, const vector <vi> &g, vector <bool> &vis, map <pii, bool> &map) { vis[w] = true; if (w != 1 && !map[{1, w}]) { res.push_back("+ 1 " + to_string(w)); map[{1, w}] = true; } for (int s : g[w]) { if (vis[s]) continue; dfs1(s, g, vis, map); } } void dfs2(int w, const vector <vi> &g, vector <bool> &vis, map <pii, bool> &edge_map) { vis[w] = true; for (int s : g[w]) { if (vis[s]) continue; dfs2(s, g, vis, edge_map); } if (w != 1 && !edge_map[{1, w}]) res.push_back("- 1 " + to_string(w)); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n; vector<vi> g1(n + 1), g2(n + 1); map <pii, bool> edge_map1, edge_map2; get_graph(m1, g1, edge_map1); get_graph(m2, g2, edge_map2); vector<bool> vis(n + 1, false); dfs1(1, g1, vis, edge_map1); for (auto elem : edge_map2) { pii edge = elem.fi; if (edge.fi == 1) continue; if (edge_map1[edge]) continue; res.push_back("+ " + to_string(edge.fi) + " " + to_string(edge.se)); edge_map1[edge] = true; } for (auto elem : edge_map1) { pii edge = elem.fi; if (edge.fi == 1) continue; if (!elem.se) continue; if (edge_map2[edge]) continue; res.push_back("- " + to_string(edge.fi) + " " + to_string(edge.se)); edge_map1[edge] = false; } vector <bool> vis2(n + 1, false); dfs2(1, g2, vis2, edge_map2); cout << res.size() << "\n"; for (string s : res) cout << s << "\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 | #include<bits/stdc++.h> #define vi vector<int> #define pii pair<int, int> #define se second #define fi first #define ALL(x) begin(x), end(x) using namespace std; vector <string> res; int n, m1, m2; void get_graph(int &m, vector<vi> &graph, map <pii, bool> &map) { cin >> m; for (int i = 0; i < m; i++) { int a, b; cin >> a >> b; if (a > b) swap(a, b); graph[a].push_back(b); graph[b].push_back(a); map[{a, b}] = true; } } void dfs1(int w, const vector <vi> &g, vector <bool> &vis, map <pii, bool> &map) { vis[w] = true; if (w != 1 && !map[{1, w}]) { res.push_back("+ 1 " + to_string(w)); map[{1, w}] = true; } for (int s : g[w]) { if (vis[s]) continue; dfs1(s, g, vis, map); } } void dfs2(int w, const vector <vi> &g, vector <bool> &vis, map <pii, bool> &edge_map) { vis[w] = true; for (int s : g[w]) { if (vis[s]) continue; dfs2(s, g, vis, edge_map); } if (w != 1 && !edge_map[{1, w}]) res.push_back("- 1 " + to_string(w)); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n; vector<vi> g1(n + 1), g2(n + 1); map <pii, bool> edge_map1, edge_map2; get_graph(m1, g1, edge_map1); get_graph(m2, g2, edge_map2); vector<bool> vis(n + 1, false); dfs1(1, g1, vis, edge_map1); for (auto elem : edge_map2) { pii edge = elem.fi; if (edge.fi == 1) continue; if (edge_map1[edge]) continue; res.push_back("+ " + to_string(edge.fi) + " " + to_string(edge.se)); edge_map1[edge] = true; } for (auto elem : edge_map1) { pii edge = elem.fi; if (edge.fi == 1) continue; if (!elem.se) continue; if (edge_map2[edge]) continue; res.push_back("- " + to_string(edge.fi) + " " + to_string(edge.se)); edge_map1[edge] = false; } vector <bool> vis2(n + 1, false); dfs2(1, g2, vis2, edge_map2); cout << res.size() << "\n"; for (string s : res) cout << s << "\n"; return 0; } |