#include <bits/stdc++.h> using namespace std; vector<int> graf1[30001]; vector<pair<int,int>> kra1; vector<pair<int,int>> kra2; //laczenie z 1 bitset<30001> odw; bitset<30001> odw2; bitset<30001> is; vector<string> zapy; void dfs(int v, int pop) { odw[v] = 1; if(v != 1 && pop != 1) { zapy.push_back("+ " + to_string(v) + " 1\n"); } for(auto& it: graf1[v]) { if(odw[it] == 0) dfs(it,v); } } void dfs2(int v, int pop) { if((is[v] == 1 && v != pop) || v == 1) return; odw2[v] = 1; for(auto& it: graf1[v]) { if(odw2[it] == 0) dfs2(it,v); } if(v != pop) zapy.push_back("- " + to_string(v) + " 1\n"); } int main() { ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); int n,m1,m2; cin >> n >> m1; for(int i = 0; i < m1; i++) { int a,b; cin >> a >> b; graf1[a].push_back(b); graf1[b].push_back(a); kra1.push_back({a,b}); } cin >> m2; for(int i = 0; i < m2; i++) { int a,b; cin >> a >> b; kra2.push_back({a,b}); } dfs(1,0); for(auto& it: kra1) { if(it.first != 1 && it.second != 1) zapy.push_back("- " + to_string(it.first) + " " + to_string(it.second) + "\n"); } for(auto& it: kra2) { if(it.first != 1 && it.second != 1) zapy.push_back("+ " + to_string(it.first) + " " + to_string(it.second) + "\n"); else if(it.first != 1) { is[it.first] = 1; } else if(it.second != 1) { is[it.second] = 1; } } for(int i = 2; i <= n; i++) { if(odw2[i] == 0 && is[i] == 1) { dfs2(i,i); } } cout << (int)zapy.size() << "\n"; for(auto& it: zapy) { cout << it; } }
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 | #include <bits/stdc++.h> using namespace std; vector<int> graf1[30001]; vector<pair<int,int>> kra1; vector<pair<int,int>> kra2; //laczenie z 1 bitset<30001> odw; bitset<30001> odw2; bitset<30001> is; vector<string> zapy; void dfs(int v, int pop) { odw[v] = 1; if(v != 1 && pop != 1) { zapy.push_back("+ " + to_string(v) + " 1\n"); } for(auto& it: graf1[v]) { if(odw[it] == 0) dfs(it,v); } } void dfs2(int v, int pop) { if((is[v] == 1 && v != pop) || v == 1) return; odw2[v] = 1; for(auto& it: graf1[v]) { if(odw2[it] == 0) dfs2(it,v); } if(v != pop) zapy.push_back("- " + to_string(v) + " 1\n"); } int main() { ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); int n,m1,m2; cin >> n >> m1; for(int i = 0; i < m1; i++) { int a,b; cin >> a >> b; graf1[a].push_back(b); graf1[b].push_back(a); kra1.push_back({a,b}); } cin >> m2; for(int i = 0; i < m2; i++) { int a,b; cin >> a >> b; kra2.push_back({a,b}); } dfs(1,0); for(auto& it: kra1) { if(it.first != 1 && it.second != 1) zapy.push_back("- " + to_string(it.first) + " " + to_string(it.second) + "\n"); } for(auto& it: kra2) { if(it.first != 1 && it.second != 1) zapy.push_back("+ " + to_string(it.first) + " " + to_string(it.second) + "\n"); else if(it.first != 1) { is[it.first] = 1; } else if(it.second != 1) { is[it.second] = 1; } } for(int i = 2; i <= n; i++) { if(odw2[i] == 0 && is[i] == 1) { dfs2(i,i); } } cout << (int)zapy.size() << "\n"; for(auto& it: zapy) { cout << it; } } |