#include <iostream> #include <set> #include <vector> using namespace std; struct trzy { char c; int w1; int w2; }; int n, m1, m2, in1, in2, vis[30001]; set <pair<int, int>> s1, s2; vector <trzy> vec; vector <int> tab1[30001], tab2[30001]; void dfs1(int n) { vis[n] = 1; for(int i = 0; i < tab1[n].size(); i++) { if(!vis[tab1[n][i]]) { if(s1.find({1, tab1[n][i]}) == s1.end()) { vec.push_back({'+', 1, tab1[n][i]}); } dfs1(tab1[n][i]); } } } void dfs2(int n) { vis[n] = 2; for(int i = 0; i < tab2[n].size(); i++) { if(vis[tab2[n][i]] == 1) { dfs2(tab2[n][i]); } } if(n != 1 && s2.find({1, n}) == s2.end()) { vec.push_back({'-', 1, n}); } } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin>>n>>m1; for(int i = 0; i < m1; i++) { cin>>in1>>in2; tab1[in1].push_back(in2); tab1[in2].push_back(in1); if(in1 > in2) swap(in1, in2); s1.insert({in1, in2}); } cin>>m2; for(int i = 0; i < m2; i++) { cin>>in1>>in2; tab2[in1].push_back(in2); tab2[in2].push_back(in1); if(in1 > in2) swap(in1, in2); s2.insert({in1, in2}); } dfs1(1); for(auto x : s1) { if(x.first != 1) vec.push_back({'-', x.first, x.second}); } for(auto x : s2) { if(x.first != 1) vec.push_back({'+', x.first, x.second}); } dfs2(1); cout<<vec.size()<<'\n'; for(int i = 0; i < vec.size(); i++) { cout<<vec[i].c<<' '<<vec[i].w1<<' '<<vec[i].w2<<'\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 | #include <iostream> #include <set> #include <vector> using namespace std; struct trzy { char c; int w1; int w2; }; int n, m1, m2, in1, in2, vis[30001]; set <pair<int, int>> s1, s2; vector <trzy> vec; vector <int> tab1[30001], tab2[30001]; void dfs1(int n) { vis[n] = 1; for(int i = 0; i < tab1[n].size(); i++) { if(!vis[tab1[n][i]]) { if(s1.find({1, tab1[n][i]}) == s1.end()) { vec.push_back({'+', 1, tab1[n][i]}); } dfs1(tab1[n][i]); } } } void dfs2(int n) { vis[n] = 2; for(int i = 0; i < tab2[n].size(); i++) { if(vis[tab2[n][i]] == 1) { dfs2(tab2[n][i]); } } if(n != 1 && s2.find({1, n}) == s2.end()) { vec.push_back({'-', 1, n}); } } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin>>n>>m1; for(int i = 0; i < m1; i++) { cin>>in1>>in2; tab1[in1].push_back(in2); tab1[in2].push_back(in1); if(in1 > in2) swap(in1, in2); s1.insert({in1, in2}); } cin>>m2; for(int i = 0; i < m2; i++) { cin>>in1>>in2; tab2[in1].push_back(in2); tab2[in2].push_back(in1); if(in1 > in2) swap(in1, in2); s2.insert({in1, in2}); } dfs1(1); for(auto x : s1) { if(x.first != 1) vec.push_back({'-', x.first, x.second}); } for(auto x : s2) { if(x.first != 1) vec.push_back({'+', x.first, x.second}); } dfs2(1); cout<<vec.size()<<'\n'; for(int i = 0; i < vec.size(); i++) { cout<<vec[i].c<<' '<<vec[i].w1<<' '<<vec[i].w2<<'\n'; } } |