#include <bits/stdc++.h> using namespace std; const int mn = 50003; int n,m1,m2; vector<int> G1[mn], G2[mn]; bool odw1[mn], odw2[mn]; vector<pair<char, pair<int,int>>> wyn; map<pair<int,int>, bool> mapa1, mapa2; vector<pair<int,int>> kraw1, kraw2; void dfs1(int v, int r){ odw1[v] = 1; for (auto i : G1[v]){ if (!odw1[i]){ if (v != r){ if (!mapa1[{r, i}] && !mapa1[{i, r}]) wyn.push_back({'+', {r, i}}); } dfs1(i, r); } } } void dfs2(int v, int r){ odw2[v] = 1; for (auto i : G2[v]){ if (!odw2[i]){ dfs2(i, r); if (!mapa2[{i, r}] && !mapa2[{r, i}]) wyn.push_back({'-', {r, i}}); } } } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin>>n>>m1; for (int i = 0; i < m1; i++){ int a,b; cin>>a>>b; G1[a].push_back(b); G1[b].push_back(a); kraw1.push_back({a,b}); mapa1[{a, b}] = 1; } cin>>m2; for (int i = 0; i < m2; i++){ int a,b; cin>>a>>b; G2[a].push_back(b); G2[b].push_back(a); kraw2.push_back({a,b}); mapa2[{a,b}] = 1; } dfs1(1, 1); for (auto i : kraw1){ if (i.first == 1 || i.second == 1) continue; if (!mapa2[i] && !mapa2[{i.second, i.first}]) wyn.push_back({'-', i}); } for (auto i : kraw2){ if (i.first == 1 || i.second == 1) continue; if (!mapa1[i] && !mapa1[{i.second, i.first}]) wyn.push_back({'+', i}); } dfs2(1, 1); cout<<wyn.size()<<"\n"; for (auto i : wyn){ cout<<i.first<<" "<<i.second.first<<" "<<i.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 | #include <bits/stdc++.h> using namespace std; const int mn = 50003; int n,m1,m2; vector<int> G1[mn], G2[mn]; bool odw1[mn], odw2[mn]; vector<pair<char, pair<int,int>>> wyn; map<pair<int,int>, bool> mapa1, mapa2; vector<pair<int,int>> kraw1, kraw2; void dfs1(int v, int r){ odw1[v] = 1; for (auto i : G1[v]){ if (!odw1[i]){ if (v != r){ if (!mapa1[{r, i}] && !mapa1[{i, r}]) wyn.push_back({'+', {r, i}}); } dfs1(i, r); } } } void dfs2(int v, int r){ odw2[v] = 1; for (auto i : G2[v]){ if (!odw2[i]){ dfs2(i, r); if (!mapa2[{i, r}] && !mapa2[{r, i}]) wyn.push_back({'-', {r, i}}); } } } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin>>n>>m1; for (int i = 0; i < m1; i++){ int a,b; cin>>a>>b; G1[a].push_back(b); G1[b].push_back(a); kraw1.push_back({a,b}); mapa1[{a, b}] = 1; } cin>>m2; for (int i = 0; i < m2; i++){ int a,b; cin>>a>>b; G2[a].push_back(b); G2[b].push_back(a); kraw2.push_back({a,b}); mapa2[{a,b}] = 1; } dfs1(1, 1); for (auto i : kraw1){ if (i.first == 1 || i.second == 1) continue; if (!mapa2[i] && !mapa2[{i.second, i.first}]) wyn.push_back({'-', i}); } for (auto i : kraw2){ if (i.first == 1 || i.second == 1) continue; if (!mapa1[i] && !mapa1[{i.second, i.first}]) wyn.push_back({'+', i}); } dfs2(1, 1); cout<<wyn.size()<<"\n"; for (auto i : wyn){ cout<<i.first<<" "<<i.second.first<<" "<<i.second.second<<"\n"; } } |