#include <bits/stdc++.h> using namespace std; int n,m[2]; vector<set<int>> G[2]; void dfs(int v, vector<set<int>>& Gr, vector<int>& res, int p = -1){ static vector<int> was; static int start_point; if(p == -1){ was.assign(n+1, 0); start_point = v; } was[v] = 1; bool connect_to_sp = false; for(auto u : Gr[v]){ if(u == start_point) connect_to_sp = true; if(u != p && !was[u]){ dfs(u, Gr, res, v); } } if(v != start_point && !connect_to_sp) res.push_back(v); } int main() { ios_base::sync_with_stdio(0); cin>>n>>m[0]; for(int j = 0; j<2; ++j){ G[j].assign(n+1, set<int>()); for(int i = 0, a, b; i<m[j]; ++i){ cin>>a>>b; G[j][a].insert(b); G[j][b].insert(a); } if(j == 0){ cin>>m[1]; } } vector<int> stel[2]; int sp = 1; dfs(sp, G[0], stel[0]); dfs(sp, G[1], stel[1]); vector<tuple<string,int,int>> res; for(int i = stel[0].size() - 1; i >= 0; i--){ res.push_back({"+", sp, stel[0][i]}); } for(int v = 2; v<=n; v++){ for(auto u : G[0][v])if(u > v){ res.push_back({"-", v, u}); } } for(int v = 2; v<=n; v++){ for(auto u : G[1][v])if(u > v){ res.push_back({"+", v, u}); } } for(int u : stel[1]){ res.push_back({"-", sp, u}); } cout<<res.size()<<"\n"; for(auto [s, a, b] : res){ cout<<s<<" "<<a<<" "<<b<<"\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 | #include <bits/stdc++.h> using namespace std; int n,m[2]; vector<set<int>> G[2]; void dfs(int v, vector<set<int>>& Gr, vector<int>& res, int p = -1){ static vector<int> was; static int start_point; if(p == -1){ was.assign(n+1, 0); start_point = v; } was[v] = 1; bool connect_to_sp = false; for(auto u : Gr[v]){ if(u == start_point) connect_to_sp = true; if(u != p && !was[u]){ dfs(u, Gr, res, v); } } if(v != start_point && !connect_to_sp) res.push_back(v); } int main() { ios_base::sync_with_stdio(0); cin>>n>>m[0]; for(int j = 0; j<2; ++j){ G[j].assign(n+1, set<int>()); for(int i = 0, a, b; i<m[j]; ++i){ cin>>a>>b; G[j][a].insert(b); G[j][b].insert(a); } if(j == 0){ cin>>m[1]; } } vector<int> stel[2]; int sp = 1; dfs(sp, G[0], stel[0]); dfs(sp, G[1], stel[1]); vector<tuple<string,int,int>> res; for(int i = stel[0].size() - 1; i >= 0; i--){ res.push_back({"+", sp, stel[0][i]}); } for(int v = 2; v<=n; v++){ for(auto u : G[0][v])if(u > v){ res.push_back({"-", v, u}); } } for(int v = 2; v<=n; v++){ for(auto u : G[1][v])if(u > v){ res.push_back({"+", v, u}); } } for(int u : stel[1]){ res.push_back({"-", sp, u}); } cout<<res.size()<<"\n"; for(auto [s, a, b] : res){ cout<<s<<" "<<a<<" "<<b<<"\n"; } return 0; } |