#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> #define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update> using namespace __gnu_pbds; using namespace std; #define pb push_back #define st first #define nd second typedef long long ll; typedef long double ld; const ll I = 1000LL * 1000LL * 1000LL * 1000LL * 1000LL * 1000LL; const ll M = 1000LL * 1000LL * 1000LL + 7LL; const int N = 50 * 1000 + 7; bool vis[N], czy1[N]; vector<int> eda[N], edb[N]; vector<pair<char, pair<int, int>>> answer; inline void Clean(int n) { for(int i = 1; i <= n; ++i) {vis[i] = false; czy1[i] = false;} } inline void A(int a, int b) {answer.pb(make_pair('+', make_pair(a, b)));} inline void D(int a, int b) {answer.pb(make_pair('-', make_pair(a, b)));} void DFS1(int v) { if(!czy1[v]) A(1, v); vis[v] = true; for(int i = 0; i < (int)eda[v].size(); ++i) if(!vis[eda[v][i]]) DFS1(eda[v][i]); } void Upd(int v) { for(int i = 0; i < (int)eda[v].size(); ++i) if(eda[v][i] > v) D(v, eda[v][i]); for(int i = 0; i < (int)edb[v].size(); ++i) if(edb[v][i] > v) A(v, edb[v][i]); } void DFSR(int v) { vis[v] = true; for(int i = 0; i < (int)edb[v].size(); ++i) if(!vis[edb[v][i]]) DFSR(edb[v][i]); if(!czy1[v]) D(1, v); } inline void Print() { cout << answer.size() << "\n"; for(int i = 0; i < (int)answer.size(); ++i) cout << answer[i].st << " " << answer[i].nd.st << " " << answer[i].nd.nd << "\n"; } void Solve() { int n, m1, m2, a, b; cin >> n; cin >> m1; for(int i = 1; i <= m1; ++i) { cin >> a >> b; eda[a].pb(b); eda[b].pb(a); } cin >> m2; for(int i = 1; i <= m2; ++i) { cin >> a >> b; edb[a].pb(b); edb[b].pb(a); } for(int i = 0; i < (int)eda[1].size(); ++i) czy1[eda[1][i]] = true; czy1[1] = true; DFS1(1); Clean(n); for(int i = 2; i <= n; ++i) Upd(i); for(int i = 0; i < (int)edb[1].size(); ++i) czy1[edb[1][i]] = true; vis[1] = true; for(int i = 2; i <= n; ++i) if(czy1[i] && !vis[i]) DFSR(i); Print(); } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); //int t; cin >> t; //while(t--) Solve(); 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 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 | #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> #define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update> using namespace __gnu_pbds; using namespace std; #define pb push_back #define st first #define nd second typedef long long ll; typedef long double ld; const ll I = 1000LL * 1000LL * 1000LL * 1000LL * 1000LL * 1000LL; const ll M = 1000LL * 1000LL * 1000LL + 7LL; const int N = 50 * 1000 + 7; bool vis[N], czy1[N]; vector<int> eda[N], edb[N]; vector<pair<char, pair<int, int>>> answer; inline void Clean(int n) { for(int i = 1; i <= n; ++i) {vis[i] = false; czy1[i] = false;} } inline void A(int a, int b) {answer.pb(make_pair('+', make_pair(a, b)));} inline void D(int a, int b) {answer.pb(make_pair('-', make_pair(a, b)));} void DFS1(int v) { if(!czy1[v]) A(1, v); vis[v] = true; for(int i = 0; i < (int)eda[v].size(); ++i) if(!vis[eda[v][i]]) DFS1(eda[v][i]); } void Upd(int v) { for(int i = 0; i < (int)eda[v].size(); ++i) if(eda[v][i] > v) D(v, eda[v][i]); for(int i = 0; i < (int)edb[v].size(); ++i) if(edb[v][i] > v) A(v, edb[v][i]); } void DFSR(int v) { vis[v] = true; for(int i = 0; i < (int)edb[v].size(); ++i) if(!vis[edb[v][i]]) DFSR(edb[v][i]); if(!czy1[v]) D(1, v); } inline void Print() { cout << answer.size() << "\n"; for(int i = 0; i < (int)answer.size(); ++i) cout << answer[i].st << " " << answer[i].nd.st << " " << answer[i].nd.nd << "\n"; } void Solve() { int n, m1, m2, a, b; cin >> n; cin >> m1; for(int i = 1; i <= m1; ++i) { cin >> a >> b; eda[a].pb(b); eda[b].pb(a); } cin >> m2; for(int i = 1; i <= m2; ++i) { cin >> a >> b; edb[a].pb(b); edb[b].pb(a); } for(int i = 0; i < (int)eda[1].size(); ++i) czy1[eda[1][i]] = true; czy1[1] = true; DFS1(1); Clean(n); for(int i = 2; i <= n; ++i) Upd(i); for(int i = 0; i < (int)edb[1].size(); ++i) czy1[edb[1][i]] = true; vis[1] = true; for(int i = 2; i <= n; ++i) if(czy1[i] && !vis[i]) DFSR(i); Print(); } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); //int t; cin >> t; //while(t--) Solve(); return 0; } |