#include<bits/stdc++.h> using namespace std; #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> ordered_set; typedef long long ll; typedef pair<ll,ll> pll; typedef pair<ll,int> pli; typedef pair<int,ll> pil; typedef pair<int,int> pii; const ll INFLL=1e18+7; const int INF=1e9+7; #define pb push_back const int MAXN = 1e5+7; vector<int> G[MAXN]; bool visited[MAXN]; vector<pii> edges; map<pii, bool> conn; vector<pair<char, pii>> opers; void DFS(int x){ visited[x] = 1; if(x != 1 && conn.find({1, x}) == conn.end()){ edges.pb({1, x}); } for(int &v:G[x]) if(!visited[v]) DFS(v); } pii org[MAXN]; vector<pii> order; vector<int> G2[MAXN]; void DFS2(int x){ if(x != 1) order.pb({1, x}); visited[x] = 1; for(int &v:G2[x]) if(!visited[v]) DFS2(v); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n; cin>>n; int m; cin>>m; for(int i=1;i<=m;++i){ int a,b; cin>>a>>b; G[a].pb(b); G[b].pb(a); conn[{a, b}] = 1; conn[{b, a}] = 1; org[i] = {min(a, b), max(a, b)}; } DFS(1); for(pii &p:edges) opers.pb({'+', p}); int m2; cin>>m2; set<int> must_stay; map<pii, bool> is_edge; for(int i=1;i<=m2;++i){ int a,b; cin>>a>>b; G2[a].pb(b); G2[b].pb(a); if(a > b) swap(a, b); is_edge[{a, b}] = 1; is_edge[{b, a}] = 1; if(a == 1) must_stay.insert(b); else if(conn.find({a, b}) == conn.end()){ opers.pb({'+', {a, b}}); } } for(int i=1;i<=m;++i){ if(is_edge.find({org[i].first, org[i].second}) == is_edge.end() && org[i].first != 1){ opers.pb({'-', {org[i].first, org[i].second}}); } } for(int i=1;i<=n;++i) visited[i] = 0; DFS2(1); reverse(order.begin(), order.end()); for(pii p:order){ if(must_stay.find(max(p.first, p.second)) == must_stay.end()){ opers.pb({'-', p}); } } cout<<opers.size()<<"\n"; for(auto &p:opers) cout<<p.first<<" "<<p.second.first<<" "<<p.second.second<<"\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 | #include<bits/stdc++.h> using namespace std; #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> ordered_set; typedef long long ll; typedef pair<ll,ll> pll; typedef pair<ll,int> pli; typedef pair<int,ll> pil; typedef pair<int,int> pii; const ll INFLL=1e18+7; const int INF=1e9+7; #define pb push_back const int MAXN = 1e5+7; vector<int> G[MAXN]; bool visited[MAXN]; vector<pii> edges; map<pii, bool> conn; vector<pair<char, pii>> opers; void DFS(int x){ visited[x] = 1; if(x != 1 && conn.find({1, x}) == conn.end()){ edges.pb({1, x}); } for(int &v:G[x]) if(!visited[v]) DFS(v); } pii org[MAXN]; vector<pii> order; vector<int> G2[MAXN]; void DFS2(int x){ if(x != 1) order.pb({1, x}); visited[x] = 1; for(int &v:G2[x]) if(!visited[v]) DFS2(v); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n; cin>>n; int m; cin>>m; for(int i=1;i<=m;++i){ int a,b; cin>>a>>b; G[a].pb(b); G[b].pb(a); conn[{a, b}] = 1; conn[{b, a}] = 1; org[i] = {min(a, b), max(a, b)}; } DFS(1); for(pii &p:edges) opers.pb({'+', p}); int m2; cin>>m2; set<int> must_stay; map<pii, bool> is_edge; for(int i=1;i<=m2;++i){ int a,b; cin>>a>>b; G2[a].pb(b); G2[b].pb(a); if(a > b) swap(a, b); is_edge[{a, b}] = 1; is_edge[{b, a}] = 1; if(a == 1) must_stay.insert(b); else if(conn.find({a, b}) == conn.end()){ opers.pb({'+', {a, b}}); } } for(int i=1;i<=m;++i){ if(is_edge.find({org[i].first, org[i].second}) == is_edge.end() && org[i].first != 1){ opers.pb({'-', {org[i].first, org[i].second}}); } } for(int i=1;i<=n;++i) visited[i] = 0; DFS2(1); reverse(order.begin(), order.end()); for(pii p:order){ if(must_stay.find(max(p.first, p.second)) == must_stay.end()){ opers.pb({'-', p}); } } cout<<opers.size()<<"\n"; for(auto &p:opers) cout<<p.first<<" "<<p.second.first<<" "<<p.second.second<<"\n"; } |