#include<bits/stdc++.h> #define FOR(i,a,b) for(int i=a;i<(int)b;++i) #define FORD(i,a,b) for(int i=a;i>=(int)b;--i) #define PB push_back #define EB emplace_back #define FI first #define SE second #define umap unordered_map #define uset unordered_set #define vi vector<int> #define vvi vector<vi> #define vll vector<ll> #define vvll vector<vll> #define vpii vector<pii> #define pii pair<int, int> #define pll pair<ll, ll> #define ALL(X) (X).begin(),(X).end() #ifndef DEBUG #define endl (char)10 #endif using namespace std; using ll = long long; using ld = long double; template <class T> istream& operator>> (istream& is, vector<T>& vec){ FOR(i,0,vec.size()) is >> vec[i]; return is; } template <class T> ostream& operator<< (ostream& os, vector<T>& vec){ for(auto& t : vec) os << t << " "; return os; } template<class T, class U> ostream& operator<< (ostream& os, const pair<T, U>& p){ os << p.FI << " " << p.SE; return os; } template<class T, class U> istream& operator>> (istream& is, pair<T, U>& p){ is >> p.FI >> p.SE; return is; } void fixx(const vvi& A, const vvi& B, vpii& ans){ int n = A.size(); vi c(n, 0); for(int x : A[0]) c[x] = 1; c[0] = 2; queue<int> Q; Q.push(0); while(!Q.empty()){ int v = Q.front(); Q.pop(); for(int x : A[v]){ if (c[x] != 2){ if (c[x] == 0) ans.EB(0, x); c[x] = 2; Q.push(x); } } } FOR(i,1,n){ int a = 0, b = 0; while(a < A[i].size() || b < B[i].size()){ if (a == A[i].size()) { if(i < B[i][b]) ans.EB(i, B[i][b]); b++; } else if (b == B[i].size()) { break; } else if (A[i][a] == B[i][b]){ a++; b++; } else if (A[i][a] < B[i][b]){ a++; } else { if(i < B[i][b]) ans.EB(i, B[i][b]); b++; } } } } bool can(vector<set<int>> &G, int x, int y) { auto ix = G[x].begin(); auto iy = G[y].begin(); while(ix != G[x].end() && iy != G[y].end()) { if (*ix < *iy) ix++; else if (*ix == *iy) return true; else iy++; } return false; } int main () { ios_base::sync_with_stdio(false); cin.tie(0); int n; cin >> n; vvi A(n), B(n); auto pom = [n](vvi& V){ int k; cin >> k; FOR(i,0,k){ int a, b; cin >> a >> b; a--; b--; V[a].PB(b); V[b].PB(a); } FOR(i,0,n) sort(ALL(V[i])); }; pom(A); pom(B); vpii fi, se; fixx(A, B, fi); fixx(B, A, se); reverse(ALL(se)); cout << fi.size() + se.size() << endl; for(auto& e : fi) cout << "+ " << e.FI + 1 << " " << e.SE + 1 << endl; for(auto& e : se) cout << "- " << e.FI + 1 << " " << e.SE + 1 << endl; /* bool ok = true; if (fi.size() + se.size() > 199999){ ok = false; } else { vector<set<int>> G(n); FOR(i,0,n) for(int x : A[i]) G[i].insert(x); for(auto& e : fi) { if(!can(G, e.FI, e.SE) || G[e.FI].count(e.SE) != 0){ ok = false; break; } else { G[e.FI].insert(e.SE); G[e.SE].insert(e.FI); } } for(auto& e : se) { if(!can(G, e.FI, e.SE) || G[e.FI].count(e.SE) == 0){ ok = false; break; } else { G[e.FI].erase(e.SE); G[e.SE].erase(e.FI); } } FOR(i,0,n){ if (G[i].size() != B[i].size()) ok = false; for(int x : B[i]) if (G[i].count(x) == 0) ok = false; } } cout << (ok ? "YES" : "NO") << endl; */ }
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 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 | #include<bits/stdc++.h> #define FOR(i,a,b) for(int i=a;i<(int)b;++i) #define FORD(i,a,b) for(int i=a;i>=(int)b;--i) #define PB push_back #define EB emplace_back #define FI first #define SE second #define umap unordered_map #define uset unordered_set #define vi vector<int> #define vvi vector<vi> #define vll vector<ll> #define vvll vector<vll> #define vpii vector<pii> #define pii pair<int, int> #define pll pair<ll, ll> #define ALL(X) (X).begin(),(X).end() #ifndef DEBUG #define endl (char)10 #endif using namespace std; using ll = long long; using ld = long double; template <class T> istream& operator>> (istream& is, vector<T>& vec){ FOR(i,0,vec.size()) is >> vec[i]; return is; } template <class T> ostream& operator<< (ostream& os, vector<T>& vec){ for(auto& t : vec) os << t << " "; return os; } template<class T, class U> ostream& operator<< (ostream& os, const pair<T, U>& p){ os << p.FI << " " << p.SE; return os; } template<class T, class U> istream& operator>> (istream& is, pair<T, U>& p){ is >> p.FI >> p.SE; return is; } void fixx(const vvi& A, const vvi& B, vpii& ans){ int n = A.size(); vi c(n, 0); for(int x : A[0]) c[x] = 1; c[0] = 2; queue<int> Q; Q.push(0); while(!Q.empty()){ int v = Q.front(); Q.pop(); for(int x : A[v]){ if (c[x] != 2){ if (c[x] == 0) ans.EB(0, x); c[x] = 2; Q.push(x); } } } FOR(i,1,n){ int a = 0, b = 0; while(a < A[i].size() || b < B[i].size()){ if (a == A[i].size()) { if(i < B[i][b]) ans.EB(i, B[i][b]); b++; } else if (b == B[i].size()) { break; } else if (A[i][a] == B[i][b]){ a++; b++; } else if (A[i][a] < B[i][b]){ a++; } else { if(i < B[i][b]) ans.EB(i, B[i][b]); b++; } } } } bool can(vector<set<int>> &G, int x, int y) { auto ix = G[x].begin(); auto iy = G[y].begin(); while(ix != G[x].end() && iy != G[y].end()) { if (*ix < *iy) ix++; else if (*ix == *iy) return true; else iy++; } return false; } int main () { ios_base::sync_with_stdio(false); cin.tie(0); int n; cin >> n; vvi A(n), B(n); auto pom = [n](vvi& V){ int k; cin >> k; FOR(i,0,k){ int a, b; cin >> a >> b; a--; b--; V[a].PB(b); V[b].PB(a); } FOR(i,0,n) sort(ALL(V[i])); }; pom(A); pom(B); vpii fi, se; fixx(A, B, fi); fixx(B, A, se); reverse(ALL(se)); cout << fi.size() + se.size() << endl; for(auto& e : fi) cout << "+ " << e.FI + 1 << " " << e.SE + 1 << endl; for(auto& e : se) cout << "- " << e.FI + 1 << " " << e.SE + 1 << endl; /* bool ok = true; if (fi.size() + se.size() > 199999){ ok = false; } else { vector<set<int>> G(n); FOR(i,0,n) for(int x : A[i]) G[i].insert(x); for(auto& e : fi) { if(!can(G, e.FI, e.SE) || G[e.FI].count(e.SE) != 0){ ok = false; break; } else { G[e.FI].insert(e.SE); G[e.SE].insert(e.FI); } } for(auto& e : se) { if(!can(G, e.FI, e.SE) || G[e.FI].count(e.SE) == 0){ ok = false; break; } else { G[e.FI].erase(e.SE); G[e.SE].erase(e.FI); } } FOR(i,0,n){ if (G[i].size() != B[i].size()) ok = false; for(int x : B[i]) if (G[i].count(x) == 0) ok = false; } } cout << (ok ? "YES" : "NO") << endl; */ } |