#include <bits/stdc++.h> using namespace std; struct trojka{ char znak; int a,b; }; vector <int> Con1[30001]; vector <int> Con2[30001]; vector <trojka> Res; int Odw1[30001]; int Odw2[30001]; bool czyconz1[30001]; bool czyconz2[30001]; void DFS1(int a,int ojc){ Odw1[a] = 1; if(czyconz1[a] == 0 && a != 1){ Res.push_back({'+',1,a}); } for (int i = 0; i < Con1[a].size();i++){ int x = Con1[a][i]; if(Odw1[x] == 0 && x != ojc){ DFS1(x,a); } } } void DFS2(int a,int ojc){ Odw2[a] = 1; for (int i = 0; i < Con2[a].size();i++){ int x = Con2[a][i]; if(Odw2[x] == 0 && x != ojc){ DFS2(x,a); } } if(czyconz2[a] == 0 && a != 1){ Res.push_back({'-',1,a}); } } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); int n; cin >> n; int m1,m2; cin >> m1; vector < pair<int,int> > V1; vector < pair<int,int> > V2; for (int i = 0; i < m1;i++){ int a,b; cin >> a >> b; V1.push_back({a,b}); Con1[a].push_back(b); Con1[b].push_back(a); if(min(a,b) == 1){ czyconz1[max(a,b)] = 1; } } DFS1(1,-1); for (int i = 0; i < V1.size();i++){ int a = V1[i].first; int b = V1[i].second; if(min(a,b) != 1){ Res.push_back({'-',a,b}); } } cin >> m2; for (int i = 0; i < m2;i++){ int a,b; cin >> a >> b; V2.push_back({a,b}); Con2[a].push_back(b); Con2[b].push_back(a); if(min(a,b) == 1){ czyconz2[max(a,b)] = 1; } else{ Res.push_back({'+',a,b}); } } DFS2(1,-1); cout << Res.size() << '\n'; for (int i = 0; i < Res.size();i++){ cout << Res[i].znak << " " << Res[i].a << " " << Res[i].b << '\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 | #include <bits/stdc++.h> using namespace std; struct trojka{ char znak; int a,b; }; vector <int> Con1[30001]; vector <int> Con2[30001]; vector <trojka> Res; int Odw1[30001]; int Odw2[30001]; bool czyconz1[30001]; bool czyconz2[30001]; void DFS1(int a,int ojc){ Odw1[a] = 1; if(czyconz1[a] == 0 && a != 1){ Res.push_back({'+',1,a}); } for (int i = 0; i < Con1[a].size();i++){ int x = Con1[a][i]; if(Odw1[x] == 0 && x != ojc){ DFS1(x,a); } } } void DFS2(int a,int ojc){ Odw2[a] = 1; for (int i = 0; i < Con2[a].size();i++){ int x = Con2[a][i]; if(Odw2[x] == 0 && x != ojc){ DFS2(x,a); } } if(czyconz2[a] == 0 && a != 1){ Res.push_back({'-',1,a}); } } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); int n; cin >> n; int m1,m2; cin >> m1; vector < pair<int,int> > V1; vector < pair<int,int> > V2; for (int i = 0; i < m1;i++){ int a,b; cin >> a >> b; V1.push_back({a,b}); Con1[a].push_back(b); Con1[b].push_back(a); if(min(a,b) == 1){ czyconz1[max(a,b)] = 1; } } DFS1(1,-1); for (int i = 0; i < V1.size();i++){ int a = V1[i].first; int b = V1[i].second; if(min(a,b) != 1){ Res.push_back({'-',a,b}); } } cin >> m2; for (int i = 0; i < m2;i++){ int a,b; cin >> a >> b; V2.push_back({a,b}); Con2[a].push_back(b); Con2[b].push_back(a); if(min(a,b) == 1){ czyconz2[max(a,b)] = 1; } else{ Res.push_back({'+',a,b}); } } DFS2(1,-1); cout << Res.size() << '\n'; for (int i = 0; i < Res.size();i++){ cout << Res[i].znak << " " << Res[i].a << " " << Res[i].b << '\n'; } } |