#include <bits/stdc++.h> using namespace std; //define int long long constexpr int maxN = 3e4+7; int n,m1,m2,dist[maxN]; bool ok[maxN]; pair<int,int>edges1[maxN*2],edges2[maxN*2]; vector<int>v[maxN]; struct operacja{ char typ; int a,b; void wypisz(){ cout<<typ<<' '<<a<<' '<<b<<"\n"; } }; vector<operacja>ans; void bfs(){ dist[1] = 1; queue<int>q; q.push(1); while(!q.empty()){ int x = q.front(); q.pop(); if(dist[x] > 2) ans.push_back({'+',x,1}); for(int a:v[x]){ if(!dist[a]){ dist[a] = dist[x] + 1; q.push(a); } } } } int32_t main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin>>n; cin>>m1; for(int i=1,a,b;i<=m1;i++){ cin>>a>>b; v[a].push_back(b); v[b].push_back(a); edges1[i] = {a,b}; } cin>>m2; for(int i=1,a,b;i<=m2;i++){ cin>>a>>b; edges2[i] = {a,b}; } bfs(); for(int i=1,a,b;i<=m1;i++){ a = edges1[i].first, b = edges1[i].second; if(a == 1 || b == 1) continue; ans.push_back({'-',a,b}); } for(int i=1,a,b;i<=m2;i++){ a = edges2[i].first, b = edges2[i].second; if(a == 1 || b == 1){ ok[a] = true; ok[b] = true; continue; } ans.push_back({'+',a,b}); } for(int i=2;i<=n;i++){ if(!ok[i]){ ans.push_back({'-',1,i}); } } cout<<ans.size()<<"\n"; for(int i=0;i<ans.size();i++) ans[i].wypisz(); } /* 4 3 1 2 3 4 3 2 4 1 4 1 2 2 3 3 4 */
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 86 87 88 89 90 91 92 93 94 95 | #include <bits/stdc++.h> using namespace std; //define int long long constexpr int maxN = 3e4+7; int n,m1,m2,dist[maxN]; bool ok[maxN]; pair<int,int>edges1[maxN*2],edges2[maxN*2]; vector<int>v[maxN]; struct operacja{ char typ; int a,b; void wypisz(){ cout<<typ<<' '<<a<<' '<<b<<"\n"; } }; vector<operacja>ans; void bfs(){ dist[1] = 1; queue<int>q; q.push(1); while(!q.empty()){ int x = q.front(); q.pop(); if(dist[x] > 2) ans.push_back({'+',x,1}); for(int a:v[x]){ if(!dist[a]){ dist[a] = dist[x] + 1; q.push(a); } } } } int32_t main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin>>n; cin>>m1; for(int i=1,a,b;i<=m1;i++){ cin>>a>>b; v[a].push_back(b); v[b].push_back(a); edges1[i] = {a,b}; } cin>>m2; for(int i=1,a,b;i<=m2;i++){ cin>>a>>b; edges2[i] = {a,b}; } bfs(); for(int i=1,a,b;i<=m1;i++){ a = edges1[i].first, b = edges1[i].second; if(a == 1 || b == 1) continue; ans.push_back({'-',a,b}); } for(int i=1,a,b;i<=m2;i++){ a = edges2[i].first, b = edges2[i].second; if(a == 1 || b == 1){ ok[a] = true; ok[b] = true; continue; } ans.push_back({'+',a,b}); } for(int i=2;i<=n;i++){ if(!ok[i]){ ans.push_back({'-',1,i}); } } cout<<ans.size()<<"\n"; for(int i=0;i<ans.size();i++) ans[i].wypisz(); } /* 4 3 1 2 3 4 3 2 4 1 4 1 2 2 3 3 4 */ |