#include <bits/stdc++.h> using namespace std; struct graph { int n, m; vector<vector<int>> adj; vector<bool> visited, done; graph(int _n) : n(_n), adj(n + 1), visited(n + 1), done(n + 1) {} void read() { cin >> m; for (int i = 0; i < m; i++) { int u, v; cin >> u >> v; adj[u].emplace_back(v); adj[v].emplace_back(u); } } void dfs(int u, vector<tuple<int, int, int>> &result) { visited[u] = true; for (auto v : adj[u]) { if (!visited[v]) { if (!done[v]) { result.emplace_back(1, 1, v); done[v] = true; } dfs(v, result); } } } vector<tuple<int, int, int>> to_star() { done[1] = true; for (auto v : adj[1]) { done[v] = 1; } vector<tuple<int, int, int>> result; dfs(1, result); for (int u = 2; u <= n; u++) { for (auto v : adj[u]) { if (u < v) { result.emplace_back(0, u, v); } } } return result; } }; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; graph g1(n), g2(n); g1.read(); g2.read(); auto result = g1.to_star(); auto tmp = g2.to_star(); reverse(tmp.begin(), tmp.end()); for (auto &[type, u, v] : tmp) { type = 1 - type; } result.insert(result.end(), tmp.begin(), tmp.end()); cout << result.size() << "\n"; for (const auto &[type, u, v] : result) { cout << (type == 1 ? "+" : "-") << " " << u << " " << v << "\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 | #include <bits/stdc++.h> using namespace std; struct graph { int n, m; vector<vector<int>> adj; vector<bool> visited, done; graph(int _n) : n(_n), adj(n + 1), visited(n + 1), done(n + 1) {} void read() { cin >> m; for (int i = 0; i < m; i++) { int u, v; cin >> u >> v; adj[u].emplace_back(v); adj[v].emplace_back(u); } } void dfs(int u, vector<tuple<int, int, int>> &result) { visited[u] = true; for (auto v : adj[u]) { if (!visited[v]) { if (!done[v]) { result.emplace_back(1, 1, v); done[v] = true; } dfs(v, result); } } } vector<tuple<int, int, int>> to_star() { done[1] = true; for (auto v : adj[1]) { done[v] = 1; } vector<tuple<int, int, int>> result; dfs(1, result); for (int u = 2; u <= n; u++) { for (auto v : adj[u]) { if (u < v) { result.emplace_back(0, u, v); } } } return result; } }; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; graph g1(n), g2(n); g1.read(); g2.read(); auto result = g1.to_star(); auto tmp = g2.to_star(); reverse(tmp.begin(), tmp.end()); for (auto &[type, u, v] : tmp) { type = 1 - type; } result.insert(result.end(), tmp.begin(), tmp.end()); cout << result.size() << "\n"; for (const auto &[type, u, v] : result) { cout << (type == 1 ? "+" : "-") << " " << u << " " << v << "\n"; } return 0; } |