#include <bits/stdc++.h> using namespace std; typedef long long ll; vector<int> sas[30001][2]; bool vis[30001]; set<pair<int,int> > kraw[2]; vector<pair<int,pair<int,int> > > ruchy; void dodaj(int r, int a, int b){ ruchy.push_back({r,{a,b}}); } void DFS(int v){ vis[v] = 1; for (int u : sas[v][1]){ if (!vis[u]) DFS(u); } if (v != 1 && kraw[1].find({1,v}) == kraw[1].end()){ dodaj(-1,1,v); } } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); int i; int n; cin>>n; for (int j = 0; j < 2; j++){ int m; cin>>m; while (m--){ int a,b; cin>>a>>b; if (a > b) swap(a,b); kraw[j].insert({a,b}); sas[a][j].push_back(b); sas[b][j].push_back(a); } } queue<int> Q; vis[1] = 1; for (int u : sas[1][0]){ Q.push(u); vis[u] = 1; } while (Q.size()){ int v = Q.front(); vis[v] = 1; Q.pop(); for (int u : sas[v][0]){ if (vis[u]) continue; dodaj(1,1,u); vis[u] = 1; Q.push(u); } } for (auto j : kraw[1]){ if (kraw[0].find(j) == kraw[0].end() && j.first != 1){ dodaj(1,j.first,j.second); } } for (auto j : kraw[0]){ if (kraw[1].find(j) == kraw[1].end() && j.first != 1){ dodaj(-1,j.first,j.second); } } for (i = 1; i <= n; i++) vis[i] = 0; DFS(1); cout<<ruchy.size()<<"\n"; for (auto j : ruchy){ if (j.first == 1) cout<<"+ "; else cout<<"- "; cout<<j.second.first<<" "<<j.second.second<<"\n"; } 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 | #include <bits/stdc++.h> using namespace std; typedef long long ll; vector<int> sas[30001][2]; bool vis[30001]; set<pair<int,int> > kraw[2]; vector<pair<int,pair<int,int> > > ruchy; void dodaj(int r, int a, int b){ ruchy.push_back({r,{a,b}}); } void DFS(int v){ vis[v] = 1; for (int u : sas[v][1]){ if (!vis[u]) DFS(u); } if (v != 1 && kraw[1].find({1,v}) == kraw[1].end()){ dodaj(-1,1,v); } } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); int i; int n; cin>>n; for (int j = 0; j < 2; j++){ int m; cin>>m; while (m--){ int a,b; cin>>a>>b; if (a > b) swap(a,b); kraw[j].insert({a,b}); sas[a][j].push_back(b); sas[b][j].push_back(a); } } queue<int> Q; vis[1] = 1; for (int u : sas[1][0]){ Q.push(u); vis[u] = 1; } while (Q.size()){ int v = Q.front(); vis[v] = 1; Q.pop(); for (int u : sas[v][0]){ if (vis[u]) continue; dodaj(1,1,u); vis[u] = 1; Q.push(u); } } for (auto j : kraw[1]){ if (kraw[0].find(j) == kraw[0].end() && j.first != 1){ dodaj(1,j.first,j.second); } } for (auto j : kraw[0]){ if (kraw[1].find(j) == kraw[1].end() && j.first != 1){ dodaj(-1,j.first,j.second); } } for (i = 1; i <= n; i++) vis[i] = 0; DFS(1); cout<<ruchy.size()<<"\n"; for (auto j : ruchy){ if (j.first == 1) cout<<"+ "; else cout<<"- "; cout<<j.second.first<<" "<<j.second.second<<"\n"; } return 0; } |