// Author: Kajetan Ramsza #include "bits/stdc++.h" using namespace std; #define rep(i, a, b) for(int i = (a); i < (b); ++i) #define all(x) begin(x), end(x) #define sz(x) (int)(x).size() typedef long long ll; typedef pair<int, int> pii; typedef vector<int> vi; template<typename F, typename S> ostream& operator<<(ostream& os, const pair<F, S> &p) { return os<<"("<<p.first<<", "<<p.second<<")"; } template<typename T> ostream &operator<<(ostream & os, const vector<T> &v) { os << "{"; typename vector< T > :: const_iterator it; for( it = v.begin(); it != v.end(); it++ ) { if( it != v.begin() ) os << ", "; os << *it; } return os << "}"; } template<typename T> ostream &operator << ( ostream & os, const set< T > &v ) { os << "["; typename set< T > :: const_iterator it; for ( it = v.begin(); it != v.end(); it++ ) { if( it != v.begin() ) os << ", "; os << *it; } return os << "]"; } template < typename F, typename S > ostream &operator << ( ostream & os, const map< F, S > &v ) { os << "["; typename map< F , S >::const_iterator it; for( it = v.begin(); it != v.end(); it++ ) { if( it != v.begin() ) os << ", "; os << it -> first << " = " << it -> second ; } return os << "]"; } void dbg_out() { cerr<<'\n'; } template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cerr<<' '<<H; dbg_out(T...); } #ifdef DEBUG #define dbg(...) cerr<<"(" << #__VA_ARGS__ <<"):", dbg_out(__VA_ARGS__) #else #define dbg(...) #endif const int maxn = 30000; int n; int m1, m2; vi g[maxn], new_g[maxn]; set<pii> edges; set<pii> goal; vector<pii> added; vector<pii> removed; inline void add_edge(int a, int b) { if(a > b) swap(a, b); if(edges.count({a, b})) return; edges.insert({a,b}); added.push_back({a,b}); } inline void remove_edge(int a, int b) { dbg(a, b); if(a > b) swap(a, b); if(goal.count({a, b})) return; // edges.erase({a,b}); removed.push_back({a,b}); } vector<bool> vis; void dfs(int v) { vis[v] = true; if(v != 0) add_edge(0, v); for(auto e : g[v]) if(!vis[e]) dfs(e); } void dfs2(int v) { vis[v] = true; for(auto e : new_g[v]) if(!vis[e]) dfs2(e); if(v != 0) remove_edge(0, v); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin>>n; cin>>m1; rep(i,0,m1) { int a, b; cin>>a>>b; a--; b--; g[a].push_back(b); g[b].push_back(a); if(a > b) swap(a,b); edges.insert({a,b}); } vis.assign(n, 0); dfs(0); dbg(edges); cin>>m2; rep(i,0,m2) { int a, b; cin>>a>>b; a--; b--; if(a > b) swap(a,b); new_g[a].push_back(b); new_g[b].push_back(a); goal.insert({a, b}); add_edge(a, b); } dbg(edges); for(auto [a,b] : edges) if(a != 0 && !goal.count({a,b})) remove_edge(a, b); fill(all(vis), 0); dbg(edges); dfs2(0); cout<<sz(added) + sz(removed)<<'\n'; for(auto [a,b] : added) cout<<"+ "<<a+1<<' '<<b+1<<'\n'; for(auto [a,b] : removed) cout<<"- "<<a+1<<' '<<b+1<<'\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 117 118 119 120 121 122 123 124 125 126 127 128 | // Author: Kajetan Ramsza #include "bits/stdc++.h" using namespace std; #define rep(i, a, b) for(int i = (a); i < (b); ++i) #define all(x) begin(x), end(x) #define sz(x) (int)(x).size() typedef long long ll; typedef pair<int, int> pii; typedef vector<int> vi; template<typename F, typename S> ostream& operator<<(ostream& os, const pair<F, S> &p) { return os<<"("<<p.first<<", "<<p.second<<")"; } template<typename T> ostream &operator<<(ostream & os, const vector<T> &v) { os << "{"; typename vector< T > :: const_iterator it; for( it = v.begin(); it != v.end(); it++ ) { if( it != v.begin() ) os << ", "; os << *it; } return os << "}"; } template<typename T> ostream &operator << ( ostream & os, const set< T > &v ) { os << "["; typename set< T > :: const_iterator it; for ( it = v.begin(); it != v.end(); it++ ) { if( it != v.begin() ) os << ", "; os << *it; } return os << "]"; } template < typename F, typename S > ostream &operator << ( ostream & os, const map< F, S > &v ) { os << "["; typename map< F , S >::const_iterator it; for( it = v.begin(); it != v.end(); it++ ) { if( it != v.begin() ) os << ", "; os << it -> first << " = " << it -> second ; } return os << "]"; } void dbg_out() { cerr<<'\n'; } template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cerr<<' '<<H; dbg_out(T...); } #ifdef DEBUG #define dbg(...) cerr<<"(" << #__VA_ARGS__ <<"):", dbg_out(__VA_ARGS__) #else #define dbg(...) #endif const int maxn = 30000; int n; int m1, m2; vi g[maxn], new_g[maxn]; set<pii> edges; set<pii> goal; vector<pii> added; vector<pii> removed; inline void add_edge(int a, int b) { if(a > b) swap(a, b); if(edges.count({a, b})) return; edges.insert({a,b}); added.push_back({a,b}); } inline void remove_edge(int a, int b) { dbg(a, b); if(a > b) swap(a, b); if(goal.count({a, b})) return; // edges.erase({a,b}); removed.push_back({a,b}); } vector<bool> vis; void dfs(int v) { vis[v] = true; if(v != 0) add_edge(0, v); for(auto e : g[v]) if(!vis[e]) dfs(e); } void dfs2(int v) { vis[v] = true; for(auto e : new_g[v]) if(!vis[e]) dfs2(e); if(v != 0) remove_edge(0, v); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin>>n; cin>>m1; rep(i,0,m1) { int a, b; cin>>a>>b; a--; b--; g[a].push_back(b); g[b].push_back(a); if(a > b) swap(a,b); edges.insert({a,b}); } vis.assign(n, 0); dfs(0); dbg(edges); cin>>m2; rep(i,0,m2) { int a, b; cin>>a>>b; a--; b--; if(a > b) swap(a,b); new_g[a].push_back(b); new_g[b].push_back(a); goal.insert({a, b}); add_edge(a, b); } dbg(edges); for(auto [a,b] : edges) if(a != 0 && !goal.count({a,b})) remove_edge(a, b); fill(all(vis), 0); dbg(edges); dfs2(0); cout<<sz(added) + sz(removed)<<'\n'; for(auto [a,b] : added) cout<<"+ "<<a+1<<' '<<b+1<<'\n'; for(auto [a,b] : removed) cout<<"- "<<a+1<<' '<<b+1<<'\n'; } |