#include<bits/stdc++.h> using namespace std; const int N = 3e4 + 5; class info{ public: int p, u, v; inline info() {} inline info(int p0, int u0, int v0){ p = p0, u = u0, v = v0; } }; int n = 0, m1 = 0, m2 = 0, fa[N] = {}, vis[N] = {}, vis1[N] = {}, vis2[N] = {}; vector<vector<int> > G1(N), G2(N); vector<info> ans; inline void dfs1(int u){ if(u > 1 && !vis1[u]) ans.push_back(info(1, 1, u)); vis[u] = 1; for(int v : G1[u]) if(!vis[v]) dfs1(v); } inline void dfs2(int u){ vis[u] = 1; for(int v : G2[u]) if(!vis[v]) dfs2(v); if(u > 1 && !vis2[u]) ans.push_back(info(0, 1, u)); } int main(){ scanf("%d", &n); scanf("%d", &m1); for(int i = 1, u = 0, v = 0 ; i <= m1 ; i ++){ scanf("%d %d", &u, &v); G1[u].push_back(v), G1[v].push_back(u); if(min(u, v) == 1) vis1[max(u, v)] = 1; } scanf("%d", &m2); for(int i = 1, u = 0, v = 0 ; i <= m2 ; i ++){ scanf("%d %d", &u, &v); G2[u].push_back(v), G2[v].push_back(u); if(min(u, v) == 1) vis2[max(u, v)] = 1; } memset(vis, 0, sizeof(vis)), dfs1(1); for(int u = 2 ; u <= n ; u ++) for(int v : G1[u]) if(u < v) ans.push_back(info(0, u, v)); for(int u = 2 ; u <= n ; u ++) for(int v : G2[u]) if(u < v) ans.push_back(info(1, u, v)); memset(vis, 0, sizeof(vis)), dfs2(1); printf("%d\n", int(ans.size())); for(auto bag : ans){ int p = bag.p, u = bag.u, v = bag.v; if(p) printf("+ %d %d\n", u, v); else printf("- %d %d\n", u, v); } 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 | #include<bits/stdc++.h> using namespace std; const int N = 3e4 + 5; class info{ public: int p, u, v; inline info() {} inline info(int p0, int u0, int v0){ p = p0, u = u0, v = v0; } }; int n = 0, m1 = 0, m2 = 0, fa[N] = {}, vis[N] = {}, vis1[N] = {}, vis2[N] = {}; vector<vector<int> > G1(N), G2(N); vector<info> ans; inline void dfs1(int u){ if(u > 1 && !vis1[u]) ans.push_back(info(1, 1, u)); vis[u] = 1; for(int v : G1[u]) if(!vis[v]) dfs1(v); } inline void dfs2(int u){ vis[u] = 1; for(int v : G2[u]) if(!vis[v]) dfs2(v); if(u > 1 && !vis2[u]) ans.push_back(info(0, 1, u)); } int main(){ scanf("%d", &n); scanf("%d", &m1); for(int i = 1, u = 0, v = 0 ; i <= m1 ; i ++){ scanf("%d %d", &u, &v); G1[u].push_back(v), G1[v].push_back(u); if(min(u, v) == 1) vis1[max(u, v)] = 1; } scanf("%d", &m2); for(int i = 1, u = 0, v = 0 ; i <= m2 ; i ++){ scanf("%d %d", &u, &v); G2[u].push_back(v), G2[v].push_back(u); if(min(u, v) == 1) vis2[max(u, v)] = 1; } memset(vis, 0, sizeof(vis)), dfs1(1); for(int u = 2 ; u <= n ; u ++) for(int v : G1[u]) if(u < v) ans.push_back(info(0, u, v)); for(int u = 2 ; u <= n ; u ++) for(int v : G2[u]) if(u < v) ans.push_back(info(1, u, v)); memset(vis, 0, sizeof(vis)), dfs2(1); printf("%d\n", int(ans.size())); for(auto bag : ans){ int p = bag.p, u = bag.u, v = bag.v; if(p) printf("+ %d %d\n", u, v); else printf("- %d %d\n", u, v); } return 0; } |