#include <bits/stdc++.h> using namespace std; #define st first #define nd second typedef long long ll; typedef long double ld; typedef __int128 int128; typedef vector<int> vi; typedef pair<int,int> pi; typedef pair<double,double> pd; typedef pair<ll,ll> pl; const int max_n = 3e4+7; vi g[max_n]; vi g2[max_n]; set<pi> start; set<pi> target; bool odw[max_n]; queue<int> q; vi kolejka; vector<pair<char,pi>> ans; int main(){ ios::sync_with_stdio(0); cin.tie(0); int n,m1; cin >> n >> m1; for(int i = 1; i <= m1; i++){ int a,b; cin >> a >> b; if(a > b) swap(a,b); g[a].push_back(b); g[b].push_back(a); start.insert({a,b}); } int m2; cin >> m2; for(int i = 1; i <= m2; i++){ int a,b; cin >> a >> b; if(a > b) swap(a,b); g2[a].push_back(b); g2[b].push_back(a); target.insert({a,b}); } //bfs q.push(1); odw[1] = 1; while(!q.empty()){ int v = q.front(); q.pop(); if(v != 1) kolejka.push_back(v); for(auto x:g[v]){ if(odw[x]) continue; q.push(x); odw[x] = 1; } } for(int i = 1;i <= n; i++) odw[i] = 0; for(auto x:kolejka){ if(start.count({1,x})) continue; ans.push_back({'+',{1,x}}); } for(auto x:target){ if(start.count(x)) continue; if(x.st == 1) continue; ans.push_back({'+',x}); } for(auto x:start){ if(target.count(x)) continue; if(x.st == 1) continue; ans.push_back({'-',x}); } int wierzcholek = 0; for(auto x:target){ if(x.st == 1){ wierzcholek = x.nd; break; } } //cout << "wierzcholek: " << wierzcholek << '\n'; q.push(wierzcholek); odw[wierzcholek] = 1; kolejka.clear(); while(!q.empty()){ int v = q.front(); q.pop(); //cout << "kolejka, v: " << v << '\n'; if(v != 1) kolejka.push_back(v); for(auto x:g2[v]){ if(odw[x]) continue; q.push(x); odw[x] = 1; } } reverse(kolejka.begin(),kolejka.end()); for(auto x:kolejka){ if(target.count({1,x})) continue; ans.push_back({'-',{1,x}}); } cout << ans.size() << '\n'; for(auto x:ans) cout << x.st << ' ' << x.nd.st << ' ' << x.nd.nd << '\n'; return 0; } //g++ -O3 -static -Wall .cpp -std=c++17
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 124 125 126 127 128 129 130 131 132 133 | #include <bits/stdc++.h> using namespace std; #define st first #define nd second typedef long long ll; typedef long double ld; typedef __int128 int128; typedef vector<int> vi; typedef pair<int,int> pi; typedef pair<double,double> pd; typedef pair<ll,ll> pl; const int max_n = 3e4+7; vi g[max_n]; vi g2[max_n]; set<pi> start; set<pi> target; bool odw[max_n]; queue<int> q; vi kolejka; vector<pair<char,pi>> ans; int main(){ ios::sync_with_stdio(0); cin.tie(0); int n,m1; cin >> n >> m1; for(int i = 1; i <= m1; i++){ int a,b; cin >> a >> b; if(a > b) swap(a,b); g[a].push_back(b); g[b].push_back(a); start.insert({a,b}); } int m2; cin >> m2; for(int i = 1; i <= m2; i++){ int a,b; cin >> a >> b; if(a > b) swap(a,b); g2[a].push_back(b); g2[b].push_back(a); target.insert({a,b}); } //bfs q.push(1); odw[1] = 1; while(!q.empty()){ int v = q.front(); q.pop(); if(v != 1) kolejka.push_back(v); for(auto x:g[v]){ if(odw[x]) continue; q.push(x); odw[x] = 1; } } for(int i = 1;i <= n; i++) odw[i] = 0; for(auto x:kolejka){ if(start.count({1,x})) continue; ans.push_back({'+',{1,x}}); } for(auto x:target){ if(start.count(x)) continue; if(x.st == 1) continue; ans.push_back({'+',x}); } for(auto x:start){ if(target.count(x)) continue; if(x.st == 1) continue; ans.push_back({'-',x}); } int wierzcholek = 0; for(auto x:target){ if(x.st == 1){ wierzcholek = x.nd; break; } } //cout << "wierzcholek: " << wierzcholek << '\n'; q.push(wierzcholek); odw[wierzcholek] = 1; kolejka.clear(); while(!q.empty()){ int v = q.front(); q.pop(); //cout << "kolejka, v: " << v << '\n'; if(v != 1) kolejka.push_back(v); for(auto x:g2[v]){ if(odw[x]) continue; q.push(x); odw[x] = 1; } } reverse(kolejka.begin(),kolejka.end()); for(auto x:kolejka){ if(target.count({1,x})) continue; ans.push_back({'-',{1,x}}); } cout << ans.size() << '\n'; for(auto x:ans) cout << x.st << ' ' << x.nd.st << ' ' << x.nd.nd << '\n'; return 0; } //g++ -O3 -static -Wall .cpp -std=c++17 |