#include <bits/stdc++.h> using namespace std; #define mp make_pair #define pb push_back vector <pair <int, int> > resa, resr; set <pair <int, int> > S, Sf; vector <vector <int> > G; vector <bool> vis; void DFSa(int src) { vis[src] = 1; for (int i = 0; i < G[src].size(); i++) { int u = G[src][i]; if (!vis[u]) { if (S.find(mp(1, u)) == S.end() && S.find(mp(u, 1)) == S.end()) { S.insert(mp(1, u)); resa.pb(mp(1, u)); G[1].pb(u); G[u].pb(1); } DFSa(u); } } return; } void DFSr(int src) { vis[src] = 1; for (int i = 0; i < G[src].size(); i++) { int u = G[src][i]; if (!vis[u]) DFSr(u); } for (int i = 0; i < G[src].size(); i++) { int u = G[src][i]; if (Sf.find(mp(src, u)) == Sf.end() && Sf.find(mp(u, src)) == Sf.end() && (S.find(mp(src, u)) != S.end() || S.find(mp(u, src)) != S.end()) && u != 1 && src != 1) { resr.pb(mp(src, u)); if (S.find(mp(src, u)) != S.end()) S.erase(S.find(mp(src, u))); if (S.find(mp(u, src)) != S.end()) S.erase(S.find(mp(u, src))); } } if (Sf.find(mp(src, 1)) == Sf.end() && Sf.find(mp(1, src)) == Sf.end() && (S.find(mp(src, 1)) != S.end() || S.find(mp(1, src)) != S.end())) { resr.pb(mp(src, 1)); if (S.find(mp(src, 1)) != S.end()) S.erase(S.find(mp(src, 1))); if (S.find(mp(1, src)) != S.end()) S.erase(S.find(mp(1, src))); } return; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, m; cin >> n >> m; vector <int> v; G.assign(n + 1, v); vis.assign(n + 1, 0); for (int i = 0; i < m; i++) { int a, b; cin >> a >> b; S.insert(mp(a, b)); G[a].pb(b); G[b].pb(a); } DFSa(1); cin >> m; for (int i = 0; i < m; i++) { int a, b; cin >> a >> b; Sf.insert(mp(a, b)); if (S.find(mp(a, b)) == S.end() && S.find(mp(b, a)) == S.end()) { S.insert(mp(a, b)); resa.pb(mp(a, b)); G[a].pb(b); G[b].pb(a); } } vis.clear(); vis.assign(n + 1, 0); DFSr(1); cout << resa.size() + resr.size() << '\n'; for (int i = 0; i < resa.size(); i++) cout << "+ " << resa[i].first << ' ' << resa[i].second << '\n'; for (int i = 0; i < resr.size(); i++) cout << "- " << resr[i].first << ' ' << resr[i].second << '\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 | #include <bits/stdc++.h> using namespace std; #define mp make_pair #define pb push_back vector <pair <int, int> > resa, resr; set <pair <int, int> > S, Sf; vector <vector <int> > G; vector <bool> vis; void DFSa(int src) { vis[src] = 1; for (int i = 0; i < G[src].size(); i++) { int u = G[src][i]; if (!vis[u]) { if (S.find(mp(1, u)) == S.end() && S.find(mp(u, 1)) == S.end()) { S.insert(mp(1, u)); resa.pb(mp(1, u)); G[1].pb(u); G[u].pb(1); } DFSa(u); } } return; } void DFSr(int src) { vis[src] = 1; for (int i = 0; i < G[src].size(); i++) { int u = G[src][i]; if (!vis[u]) DFSr(u); } for (int i = 0; i < G[src].size(); i++) { int u = G[src][i]; if (Sf.find(mp(src, u)) == Sf.end() && Sf.find(mp(u, src)) == Sf.end() && (S.find(mp(src, u)) != S.end() || S.find(mp(u, src)) != S.end()) && u != 1 && src != 1) { resr.pb(mp(src, u)); if (S.find(mp(src, u)) != S.end()) S.erase(S.find(mp(src, u))); if (S.find(mp(u, src)) != S.end()) S.erase(S.find(mp(u, src))); } } if (Sf.find(mp(src, 1)) == Sf.end() && Sf.find(mp(1, src)) == Sf.end() && (S.find(mp(src, 1)) != S.end() || S.find(mp(1, src)) != S.end())) { resr.pb(mp(src, 1)); if (S.find(mp(src, 1)) != S.end()) S.erase(S.find(mp(src, 1))); if (S.find(mp(1, src)) != S.end()) S.erase(S.find(mp(1, src))); } return; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, m; cin >> n >> m; vector <int> v; G.assign(n + 1, v); vis.assign(n + 1, 0); for (int i = 0; i < m; i++) { int a, b; cin >> a >> b; S.insert(mp(a, b)); G[a].pb(b); G[b].pb(a); } DFSa(1); cin >> m; for (int i = 0; i < m; i++) { int a, b; cin >> a >> b; Sf.insert(mp(a, b)); if (S.find(mp(a, b)) == S.end() && S.find(mp(b, a)) == S.end()) { S.insert(mp(a, b)); resa.pb(mp(a, b)); G[a].pb(b); G[b].pb(a); } } vis.clear(); vis.assign(n + 1, 0); DFSr(1); cout << resa.size() + resr.size() << '\n'; for (int i = 0; i < resa.size(); i++) cout << "+ " << resa[i].first << ' ' << resa[i].second << '\n'; for (int i = 0; i < resr.size(); i++) cout << "- " << resr[i].first << ' ' << resr[i].second << '\n'; return 0; } |