#include<cstdio> #include<algorithm> #include<vector> #define S 50007 using namespace std; vector<int> V[S]; bool vis[S]; bool connectedTo1[S]; vector<int> order; void DFS(int x, int r){ order.push_back(x); vis[x] = 1; for(int i = 0; i < V[x].size(); i++){ int v = V[x][i]; if(!vis[v]){ DFS(v,x); } } } int main(void){ int n; scanf("%d", &n); int ma,mb; scanf("%d", &ma); for(int i = 1; i <= ma; i++){ int a,b; scanf("%d %d",&a,&b); V[a].push_back(b); V[b].push_back(a); } vector<pair<char,pair<int,int>>> ans; for(int i = 0; i < V[1].size(); i++){ int v = V[1][i]; connectedTo1[v] = 1; } connectedTo1[1] = 1; DFS(1, 1); for(int i = 0; i < order.size(); i++){ int v = order[i]; if(connectedTo1[v]) continue; ans.push_back({'+', {v, 1}}); } for(int i = 1; i <= n; i++){ for(int j = 0; j < V[i].size(); j++){ int v = V[i][j]; if(v < i && v != 1){ ans.push_back({'-', {v, i}}); } } } for(int i = 1; i <= n; i++){ vis[i] = 0; V[i].clear(); connectedTo1[i] = 0; } order.clear(); scanf("%d", &mb); for(int i = 1; i <= mb; i++){ int a,b; scanf("%d %d",&a,&b); V[a].push_back(b); V[b].push_back(a); if(a != 1 && b != 1){ ans.push_back({'+', {a,b}}); } } for(int i = 0; i < V[1].size(); i++){ int v = V[1][i]; connectedTo1[v] = 1; } connectedTo1[1] = 1; DFS(1, 1); for(int i = order.size()-1; i >= 0; i--){ int v = order[i]; if(connectedTo1[v]) continue; ans.push_back({'-', {v, 1}}); } printf("%d\n",ans.size()); for(int i = 0; i < ans.size(); i++){ printf("%c %d %d\n", ans[i].first, ans[i].second.first, ans[i].second.second); } 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 | #include<cstdio> #include<algorithm> #include<vector> #define S 50007 using namespace std; vector<int> V[S]; bool vis[S]; bool connectedTo1[S]; vector<int> order; void DFS(int x, int r){ order.push_back(x); vis[x] = 1; for(int i = 0; i < V[x].size(); i++){ int v = V[x][i]; if(!vis[v]){ DFS(v,x); } } } int main(void){ int n; scanf("%d", &n); int ma,mb; scanf("%d", &ma); for(int i = 1; i <= ma; i++){ int a,b; scanf("%d %d",&a,&b); V[a].push_back(b); V[b].push_back(a); } vector<pair<char,pair<int,int>>> ans; for(int i = 0; i < V[1].size(); i++){ int v = V[1][i]; connectedTo1[v] = 1; } connectedTo1[1] = 1; DFS(1, 1); for(int i = 0; i < order.size(); i++){ int v = order[i]; if(connectedTo1[v]) continue; ans.push_back({'+', {v, 1}}); } for(int i = 1; i <= n; i++){ for(int j = 0; j < V[i].size(); j++){ int v = V[i][j]; if(v < i && v != 1){ ans.push_back({'-', {v, i}}); } } } for(int i = 1; i <= n; i++){ vis[i] = 0; V[i].clear(); connectedTo1[i] = 0; } order.clear(); scanf("%d", &mb); for(int i = 1; i <= mb; i++){ int a,b; scanf("%d %d",&a,&b); V[a].push_back(b); V[b].push_back(a); if(a != 1 && b != 1){ ans.push_back({'+', {a,b}}); } } for(int i = 0; i < V[1].size(); i++){ int v = V[1][i]; connectedTo1[v] = 1; } connectedTo1[1] = 1; DFS(1, 1); for(int i = order.size()-1; i >= 0; i--){ int v = order[i]; if(connectedTo1[v]) continue; ans.push_back({'-', {v, 1}}); } printf("%d\n",ans.size()); for(int i = 0; i < ans.size(); i++){ printf("%c %d %d\n", ans[i].first, ans[i].second.first, ans[i].second.second); } return 0; } |