#include <bits/stdc++.h> using namespace std; constexpr int MAXN = 30'005; constexpr int MAXM = 50'005; struct prompt { char c; int a, b; }; vector<int> adj[MAXN]; map<pair<int, int>, int> edges; // 0 - nie ma, 1 - jest, 2 - powinien być vector<int> order; vector<prompt> prompts; vector<bool> vis; int parents[MAXN]; int cnt = 0; void bfs(int v, int b, bool del) { queue<int> q; q.push(v); vis[v] = 1; while (q.size()) { int a = q.front(); q.pop(); if (a == b) { while (parents[b] != v) { order.push_back(b); b = parents[b]; } return; } for (auto x : adj[a]) { if (vis[x] || (del && a == v && x == b) || edges[{min(a, x), max(a, x)}] == 0) continue; vis[x] = 1; parents[x] = a; q.push(x); } } } void add(int a, int b) { vis.assign(MAXN, 0); bfs(a, b, 0); // for (auto x : order) // cout << x << ' '; // cout << '\n'; for (int i = order.size() - 1; i >= 0; i--) { adj[a].push_back(order[i]); adj[order[i]].push_back(a); prompts.push_back({'+', a, order[i]}); edges[{min(a, order[i]), max(a, order[i])}] = 1; cnt++; } order.clear(); } void remove(int a, int b) { vis.assign(MAXN, 0); bfs(a, b, 1); for (int i = order.size() - 1; i > 0; i--) { prompts.push_back({'+', a, order[i]}); cnt++; } for (int i = 0; i <= order.size() - 1; i++) { edges[{min(a, order[i]), max(a, order[i])}] = 0; prompts.push_back({'-', a, order[i]}); cnt++; } order.clear(); } int main() { ios_base::sync_with_stdio(0); cin.tie(0), cout.tie(0); int n, m; cin >> n >> m; for (int i = 0; i < m; i++) { int a, b; cin >> a >> b; adj[a].push_back(b); adj[b].push_back(a); edges[{min(a, b), max(a, b)}] = 1; } cin >> m; for (int i = 0; i < m; i++) { int a, b; cin >> a >> b; if (edges[{min(a, b), max(a, b)}] == 0) add(a, b); edges[{min(a, b), max(a, b)}] = 2; } for (int i = 1; i <= n; i++) { for (auto x : adj[i]) { if (edges[{min(i, x), max(i, x)}] == 1) remove(i, x); } } cout << cnt << '\n'; for (auto x : prompts) cout << x.c << ' ' << x.a << ' ' << x.b << '\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 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 | #include <bits/stdc++.h> using namespace std; constexpr int MAXN = 30'005; constexpr int MAXM = 50'005; struct prompt { char c; int a, b; }; vector<int> adj[MAXN]; map<pair<int, int>, int> edges; // 0 - nie ma, 1 - jest, 2 - powinien być vector<int> order; vector<prompt> prompts; vector<bool> vis; int parents[MAXN]; int cnt = 0; void bfs(int v, int b, bool del) { queue<int> q; q.push(v); vis[v] = 1; while (q.size()) { int a = q.front(); q.pop(); if (a == b) { while (parents[b] != v) { order.push_back(b); b = parents[b]; } return; } for (auto x : adj[a]) { if (vis[x] || (del && a == v && x == b) || edges[{min(a, x), max(a, x)}] == 0) continue; vis[x] = 1; parents[x] = a; q.push(x); } } } void add(int a, int b) { vis.assign(MAXN, 0); bfs(a, b, 0); // for (auto x : order) // cout << x << ' '; // cout << '\n'; for (int i = order.size() - 1; i >= 0; i--) { adj[a].push_back(order[i]); adj[order[i]].push_back(a); prompts.push_back({'+', a, order[i]}); edges[{min(a, order[i]), max(a, order[i])}] = 1; cnt++; } order.clear(); } void remove(int a, int b) { vis.assign(MAXN, 0); bfs(a, b, 1); for (int i = order.size() - 1; i > 0; i--) { prompts.push_back({'+', a, order[i]}); cnt++; } for (int i = 0; i <= order.size() - 1; i++) { edges[{min(a, order[i]), max(a, order[i])}] = 0; prompts.push_back({'-', a, order[i]}); cnt++; } order.clear(); } int main() { ios_base::sync_with_stdio(0); cin.tie(0), cout.tie(0); int n, m; cin >> n >> m; for (int i = 0; i < m; i++) { int a, b; cin >> a >> b; adj[a].push_back(b); adj[b].push_back(a); edges[{min(a, b), max(a, b)}] = 1; } cin >> m; for (int i = 0; i < m; i++) { int a, b; cin >> a >> b; if (edges[{min(a, b), max(a, b)}] == 0) add(a, b); edges[{min(a, b), max(a, b)}] = 2; } for (int i = 1; i <= n; i++) { for (auto x : adj[i]) { if (edges[{min(i, x), max(i, x)}] == 1) remove(i, x); } } cout << cnt << '\n'; for (auto x : prompts) cout << x.c << ' ' << x.a << ' ' << x.b << '\n'; return 0; } |