#include <bits/stdc++.h> using namespace std; typedef long long int ll; constexpr int N = 5e5; pair<int,int> kraws[N+9]; pair<int,int> krawd[N+9]; vector<int> graf[N+9]; vector<int> kol; int odw[N+9]; void dfs(int v){ odw[v]=1; kol.push_back(v); for (int x:graf[v]){ if (odw[x])continue; dfs(x); } } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n,ms,md,a,b; cin >> n; cin >> ms; map<pair<int,int>,int> jest; for (int x=0;x<ms;x++){ cin >> a >> b; if (a>b)swap(a,b); jest[{a,b}]=1; kraws[x]={a,b}; graf[a].push_back(b); graf[b].push_back(a); } map<pair<int,int>,int> jest2; cin >> md; for (int x=0;x<md;x++){ cin >> a >> b; if (a>b)swap(a,b); jest2[{a,b}]=1; krawd[x]={a,b}; } dfs(1); sort(graf[1].begin(),graf[1].end()); int p=0;graf[1].push_back(-69); vector<pair<char,pair<int,int>>> ruchy; for (int x:kol){ if (x==1)continue; if (x==graf[1][p]){ p++; continue; } ruchy.push_back({'+',{1,x}}); } for (int x=0;x<md;x++){ if (jest[krawd[x]]==1)continue; if (krawd[x].first==1){ graf[1].push_back(krawd[x].first); continue; } ruchy.push_back({'+',krawd[x]}); } for (int x=0;x<ms;x++){ if (jest2[kraws[x]]==1)continue; ruchy.push_back({'-',kraws[x]}); } sort(graf[1].begin(),graf[1].end()); p=0;reverse(kol.begin(),kol.end()); for (int x:kol){ if (x==1)continue; if (x==graf[1][p]){ p++; continue; } if (jest2[{1,x}]==1)continue; ruchy.push_back({'-',{1,x}}); } cout << ruchy.size() << '\n'; for (pair<char,pair<int,int>> x:ruchy){ cout << x.first << ' ' << x.second.first << ' ' << x.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 78 79 80 81 82 83 84 85 | #include <bits/stdc++.h> using namespace std; typedef long long int ll; constexpr int N = 5e5; pair<int,int> kraws[N+9]; pair<int,int> krawd[N+9]; vector<int> graf[N+9]; vector<int> kol; int odw[N+9]; void dfs(int v){ odw[v]=1; kol.push_back(v); for (int x:graf[v]){ if (odw[x])continue; dfs(x); } } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n,ms,md,a,b; cin >> n; cin >> ms; map<pair<int,int>,int> jest; for (int x=0;x<ms;x++){ cin >> a >> b; if (a>b)swap(a,b); jest[{a,b}]=1; kraws[x]={a,b}; graf[a].push_back(b); graf[b].push_back(a); } map<pair<int,int>,int> jest2; cin >> md; for (int x=0;x<md;x++){ cin >> a >> b; if (a>b)swap(a,b); jest2[{a,b}]=1; krawd[x]={a,b}; } dfs(1); sort(graf[1].begin(),graf[1].end()); int p=0;graf[1].push_back(-69); vector<pair<char,pair<int,int>>> ruchy; for (int x:kol){ if (x==1)continue; if (x==graf[1][p]){ p++; continue; } ruchy.push_back({'+',{1,x}}); } for (int x=0;x<md;x++){ if (jest[krawd[x]]==1)continue; if (krawd[x].first==1){ graf[1].push_back(krawd[x].first); continue; } ruchy.push_back({'+',krawd[x]}); } for (int x=0;x<ms;x++){ if (jest2[kraws[x]]==1)continue; ruchy.push_back({'-',kraws[x]}); } sort(graf[1].begin(),graf[1].end()); p=0;reverse(kol.begin(),kol.end()); for (int x:kol){ if (x==1)continue; if (x==graf[1][p]){ p++; continue; } if (jest2[{1,x}]==1)continue; ruchy.push_back({'-',{1,x}}); } cout << ruchy.size() << '\n'; for (pair<char,pair<int,int>> x:ruchy){ cout << x.first << ' ' << x.second.first << ' ' << x.second.second << '\n'; } return 0; } |