#include <bits/stdc++.h> using namespace std; const int MAXN = 3e5 + 5; vector <int> g1[MAXN], g2[MAXN]; bool connected[MAXN], vis[MAXN]; struct odp { char s; int a, b; }; vector <odp> ans; void dfs1(int n = 1) { vis[n] = true; if (!connected[n]) ans.push_back({'+', 1, n}); for (auto e: g1[n]) if (!vis[e]) dfs1(e); } void dfs2(int n = 1) { vis[n] = true; for (auto e: g2[n]) if (!vis[e]) dfs2(e); if (!connected[n]) ans.push_back({'-', 1, n}); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n; cin >> n; int m1, m2; cin >> m1; vector <pair <int, int>> edges; for (int i = 0; i < m1; i++) { int a, b; cin >> a >> b; g1[a].push_back(b); g1[b].push_back(a); if (a > b) swap(a, b); if (a != 1) edges.push_back({a, b}); else connected[b] = true; } connected[1] = true; dfs1(); for (auto [a, b] : edges) ans.push_back({'-', a, b}); for (int i = 2; i <= n; i++) connected[i] = false; cin >> m2; for (int i = 0; i < m2; i++) { int a, b; cin >> a >> b; g2[a].push_back(b); g2[b].push_back(a); if (a > b) swap(a, b); if (a != 1) ans.push_back({'+', a, b}); else connected[b] = true; } for (int i = 1; i <= n; i++) vis[i] = false; dfs2(); cout << ans.size() << '\n'; for (auto x: ans) cout << x.s << ' ' << x.a << ' ' << x.b << '\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 | #include <bits/stdc++.h> using namespace std; const int MAXN = 3e5 + 5; vector <int> g1[MAXN], g2[MAXN]; bool connected[MAXN], vis[MAXN]; struct odp { char s; int a, b; }; vector <odp> ans; void dfs1(int n = 1) { vis[n] = true; if (!connected[n]) ans.push_back({'+', 1, n}); for (auto e: g1[n]) if (!vis[e]) dfs1(e); } void dfs2(int n = 1) { vis[n] = true; for (auto e: g2[n]) if (!vis[e]) dfs2(e); if (!connected[n]) ans.push_back({'-', 1, n}); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n; cin >> n; int m1, m2; cin >> m1; vector <pair <int, int>> edges; for (int i = 0; i < m1; i++) { int a, b; cin >> a >> b; g1[a].push_back(b); g1[b].push_back(a); if (a > b) swap(a, b); if (a != 1) edges.push_back({a, b}); else connected[b] = true; } connected[1] = true; dfs1(); for (auto [a, b] : edges) ans.push_back({'-', a, b}); for (int i = 2; i <= n; i++) connected[i] = false; cin >> m2; for (int i = 0; i < m2; i++) { int a, b; cin >> a >> b; g2[a].push_back(b); g2[b].push_back(a); if (a > b) swap(a, b); if (a != 1) ans.push_back({'+', a, b}); else connected[b] = true; } for (int i = 1; i <= n; i++) vis[i] = false; dfs2(); cout << ans.size() << '\n'; for (auto x: ans) cout << x.s << ' ' << x.a << ' ' << x.b << '\n'; } |