#include <bits/stdc++.h> using namespace std; constexpr int MAXN = 1e5 +7; bool visited[MAXN][2]; vector<int> G[MAXN][2]; int n; int m1, m2; vector<pair<int, int>> G2; set<pair<int, int>> S; vector<tuple<int, int, int>> anserw; set<pair<int, int>> act; bool wyp; void bfs(int a){ visited[a][0] = 1; queue<int> Q; Q.push(a); while(Q.size()){ int v = Q.front(); Q.pop(); for(auto u : G[v][0]){ if(visited[u][0]) continue; visited[u][0] = 1; Q.push(u); if(S.find({a, u}) != S.end()) S.erase({a, u}); else anserw.push_back({1, a, u}); //cout << "+ 1" << " " << i << "\n"; act.insert({a, u}); } } } void usun(int v){ visited[v][1] = 1; for(auto u : G[v][1]){ if(visited[u][1]) continue; usun(u); } if(act.find({1, v}) != act.end()){ act.erase({1, v}); anserw.push_back({-1, 1, v}); } } void wypisz(int a, int b = -1){ if(!wyp) return; cout << a << " "; if(b != -1) cout << b; cout << "\n"; } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); wyp = 0; cin >> n; wypisz(n); cin >> m1; wypisz(m1); for(int i = 1; i <= m1; i++){ int a, b; cin >> a >> b; wypisz(a, b); if(b < a) swap(a, b); S.insert({a, b}); G[a][0].push_back(b); G[b][0].push_back(a); } cin >> m2; wypisz(m2); for(int i = 1; i <= m2; i++){ int a, b; cin >> a >> b; wypisz(a, b); if(b < a) swap(a, b); G2.push_back({a, b}); G[a][1].push_back(b); G[b][1].push_back(a); } //to jest bfs bfs(1); for(auto [u, v] : S) anserw.push_back({-1, u, v}); //cout << "- " << u << " " << v << "\n"; for(auto [u, v] : G2){ if(act.find({u, v}) != act.end()){ act.erase({u, v}); } else anserw.push_back({1, u, v}); //cout << "+ " << u << " " << v << "\n"; } usun(1); //for(auto [u, v] : act) //anserw.push_back({-1, u, v}); //cout << "- " << u << " " << v << "\n"; cout << anserw.size() << "\n"; for(auto [z, u, v] : anserw){ if(z == -1) cout << "- "; else cout << "+ "; cout << u << " " << v << "\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 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 | #include <bits/stdc++.h> using namespace std; constexpr int MAXN = 1e5 +7; bool visited[MAXN][2]; vector<int> G[MAXN][2]; int n; int m1, m2; vector<pair<int, int>> G2; set<pair<int, int>> S; vector<tuple<int, int, int>> anserw; set<pair<int, int>> act; bool wyp; void bfs(int a){ visited[a][0] = 1; queue<int> Q; Q.push(a); while(Q.size()){ int v = Q.front(); Q.pop(); for(auto u : G[v][0]){ if(visited[u][0]) continue; visited[u][0] = 1; Q.push(u); if(S.find({a, u}) != S.end()) S.erase({a, u}); else anserw.push_back({1, a, u}); //cout << "+ 1" << " " << i << "\n"; act.insert({a, u}); } } } void usun(int v){ visited[v][1] = 1; for(auto u : G[v][1]){ if(visited[u][1]) continue; usun(u); } if(act.find({1, v}) != act.end()){ act.erase({1, v}); anserw.push_back({-1, 1, v}); } } void wypisz(int a, int b = -1){ if(!wyp) return; cout << a << " "; if(b != -1) cout << b; cout << "\n"; } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); wyp = 0; cin >> n; wypisz(n); cin >> m1; wypisz(m1); for(int i = 1; i <= m1; i++){ int a, b; cin >> a >> b; wypisz(a, b); if(b < a) swap(a, b); S.insert({a, b}); G[a][0].push_back(b); G[b][0].push_back(a); } cin >> m2; wypisz(m2); for(int i = 1; i <= m2; i++){ int a, b; cin >> a >> b; wypisz(a, b); if(b < a) swap(a, b); G2.push_back({a, b}); G[a][1].push_back(b); G[b][1].push_back(a); } //to jest bfs bfs(1); for(auto [u, v] : S) anserw.push_back({-1, u, v}); //cout << "- " << u << " " << v << "\n"; for(auto [u, v] : G2){ if(act.find({u, v}) != act.end()){ act.erase({u, v}); } else anserw.push_back({1, u, v}); //cout << "+ " << u << " " << v << "\n"; } usun(1); //for(auto [u, v] : act) //anserw.push_back({-1, u, v}); //cout << "- " << u << " " << v << "\n"; cout << anserw.size() << "\n"; for(auto [z, u, v] : anserw){ if(z == -1) cout << "- "; else cout << "+ "; cout << u << " " << v << "\n"; } } |