#include <iostream> #include <algorithm> #include <map> #include <set> #include <vector> using namespace std; const int M = 30005; int n, m1, m2; bool vis[M], vis2[M]; set <int> graph[M], g2[M]; vector <pair<int, int>> ad, de; void add(int a, int b, bool c) { if (graph[a].count(b)) return; if (c) ad.push_back({ a,b }); graph[a].insert(b); graph[b].insert(a); } void del(int a, int b) { if (!graph[a].count(b) || g2[a].count(b)) return; de.push_back({ a,b }); graph[a].erase(b); graph[b].erase(a); } void dfs(int v) { vis[v] = 1; for (auto el : graph[v]) if (!vis[el]) { add(1, el, 1); dfs(el); } } void dfs2(int v) { vis2[v] = 1; for (auto el : graph[v]) if (!vis2[el]) dfs2(el); del(1, v); } int main() { cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(0); cin >> n >> m1; for (int i = 0; i < m1; i++) { int a, b; cin >> a >> b; add(a, b, 0); } dfs(1); cin >> m2; for (int i = 0; i < m2; i++) { int a, b; cin >> a >> b; add(a, b, 1); g2[a].insert(b); g2[b].insert(a); } for (int i = 2; i <= n; i++) for (auto el : graph[i]) if (el > i) del(i, el); dfs2(1); cout << ad.size() + de.size() << '\n'; for (auto el : ad) cout << "+ " << el.first << ' ' << el.second << '\n'; for (auto el : de) cout << "- " << el.first << ' ' << el.second << '\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 68 69 70 71 72 73 74 75 76 77 | #include <iostream> #include <algorithm> #include <map> #include <set> #include <vector> using namespace std; const int M = 30005; int n, m1, m2; bool vis[M], vis2[M]; set <int> graph[M], g2[M]; vector <pair<int, int>> ad, de; void add(int a, int b, bool c) { if (graph[a].count(b)) return; if (c) ad.push_back({ a,b }); graph[a].insert(b); graph[b].insert(a); } void del(int a, int b) { if (!graph[a].count(b) || g2[a].count(b)) return; de.push_back({ a,b }); graph[a].erase(b); graph[b].erase(a); } void dfs(int v) { vis[v] = 1; for (auto el : graph[v]) if (!vis[el]) { add(1, el, 1); dfs(el); } } void dfs2(int v) { vis2[v] = 1; for (auto el : graph[v]) if (!vis2[el]) dfs2(el); del(1, v); } int main() { cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(0); cin >> n >> m1; for (int i = 0; i < m1; i++) { int a, b; cin >> a >> b; add(a, b, 0); } dfs(1); cin >> m2; for (int i = 0; i < m2; i++) { int a, b; cin >> a >> b; add(a, b, 1); g2[a].insert(b); g2[b].insert(a); } for (int i = 2; i <= n; i++) for (auto el : graph[i]) if (el > i) del(i, el); dfs2(1); cout << ad.size() + de.size() << '\n'; for (auto el : ad) cout << "+ " << el.first << ' ' << el.second << '\n'; for (auto el : de) cout << "- " << el.first << ' ' << el.second << '\n'; } |