#include<bits/stdc++.h> using namespace std; int n,m,m2,a,b; pair<int,int> glist[50009]; pair<int,int> g2list[50009]; vector<int>G[30009]; vector<int>G2[30009]; bool visited[30009]; bool visited2[30009]; queue<int>toVisit; tuple<char,int,int> result[200009]; int resultCount=0; bool comparePairs(const pair<int,int> l, const pair<int,int> r) { return l.second < r.second; } priority_queue<pair<int,int>, vector<pair<int,int>>, std::function<bool(pair<int,int>, pair<int,int>)>> distancesTo1(comparePairs); queue<pair<int,int>> toVisitWithDistance; int main() { ios_base::sync_with_stdio(0); cin.tie(NULL); cin>>n>>m; for(int i=0;i<m;i++) { cin>>a>>b; G[a].push_back(b); G[b].push_back(a); glist[i]=make_pair(a,b); } cin>>m2; for(int i=0;i<m2;i++) { cin>>a>>b; G2[a].push_back(b); G2[b].push_back(a); g2list[i]=make_pair(a,b); } visited[1]=true; for(int x:G[1]) { toVisit.push(x); visited[x]=true; } while(!toVisit.empty()) { //cerr<<"xd"; int x = toVisit.front(); toVisit.pop(); for(int y : G[x]) { if(!visited[y]) { visited[y]=true; toVisit.push(y); result[resultCount] = make_tuple('+',1,y); resultCount++; } } } for(int i=0;i<m;i++) { if(glist[i].first!=1 && glist[i].second!=1 ) { result[resultCount] = make_tuple('-',glist[i].first,glist[i].second); resultCount++; } } for(int i=0;i<m2;i++) { if(g2list[i].first!=1 && g2list[i].second!=1 ) { //cerr<<"adding "<<g2list[i].first<<endl; result[resultCount] = make_tuple('+',g2list[i].first,g2list[i].second); resultCount++; } } visited2[1]=true; for(int x:G2[1]) { toVisitWithDistance.push(make_pair(x,1)); visited2[x]=true; //cerr<<x<<" "<<1<<endl; distancesTo1.push(make_pair(x,1)); } while(!toVisitWithDistance.empty()) { auto x = toVisitWithDistance.front(); toVisitWithDistance.pop(); for(int y : G2[x.first]) { if(!visited2[y]) { toVisitWithDistance.push(make_pair(y,x.second+1)); //cerr<<y<<" "<<x.second+1<<" from "<<x.first<<endl; distancesTo1.push(make_pair(y,x.second+1)); visited2[y]=true; } } } //cerr<<"dist "<<distancesTo1.top().second<<endl;; while(!distancesTo1.empty()) { auto x = distancesTo1.top(); distancesTo1.pop(); if(x.second>1) { //cerr<<"removing "<<x.first<<" with distance "<<x.second<<endl; result[resultCount]=make_tuple('-',1,x.first); resultCount++; } } cout<<resultCount<<'\n'; char c; for(int i=0;i<resultCount;i++) { cout<<get<0>(result[i])<<" "<<get<1>(result[i])<<" "<<get<2>(result[i])<<"\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 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 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 | #include<bits/stdc++.h> using namespace std; int n,m,m2,a,b; pair<int,int> glist[50009]; pair<int,int> g2list[50009]; vector<int>G[30009]; vector<int>G2[30009]; bool visited[30009]; bool visited2[30009]; queue<int>toVisit; tuple<char,int,int> result[200009]; int resultCount=0; bool comparePairs(const pair<int,int> l, const pair<int,int> r) { return l.second < r.second; } priority_queue<pair<int,int>, vector<pair<int,int>>, std::function<bool(pair<int,int>, pair<int,int>)>> distancesTo1(comparePairs); queue<pair<int,int>> toVisitWithDistance; int main() { ios_base::sync_with_stdio(0); cin.tie(NULL); cin>>n>>m; for(int i=0;i<m;i++) { cin>>a>>b; G[a].push_back(b); G[b].push_back(a); glist[i]=make_pair(a,b); } cin>>m2; for(int i=0;i<m2;i++) { cin>>a>>b; G2[a].push_back(b); G2[b].push_back(a); g2list[i]=make_pair(a,b); } visited[1]=true; for(int x:G[1]) { toVisit.push(x); visited[x]=true; } while(!toVisit.empty()) { //cerr<<"xd"; int x = toVisit.front(); toVisit.pop(); for(int y : G[x]) { if(!visited[y]) { visited[y]=true; toVisit.push(y); result[resultCount] = make_tuple('+',1,y); resultCount++; } } } for(int i=0;i<m;i++) { if(glist[i].first!=1 && glist[i].second!=1 ) { result[resultCount] = make_tuple('-',glist[i].first,glist[i].second); resultCount++; } } for(int i=0;i<m2;i++) { if(g2list[i].first!=1 && g2list[i].second!=1 ) { //cerr<<"adding "<<g2list[i].first<<endl; result[resultCount] = make_tuple('+',g2list[i].first,g2list[i].second); resultCount++; } } visited2[1]=true; for(int x:G2[1]) { toVisitWithDistance.push(make_pair(x,1)); visited2[x]=true; //cerr<<x<<" "<<1<<endl; distancesTo1.push(make_pair(x,1)); } while(!toVisitWithDistance.empty()) { auto x = toVisitWithDistance.front(); toVisitWithDistance.pop(); for(int y : G2[x.first]) { if(!visited2[y]) { toVisitWithDistance.push(make_pair(y,x.second+1)); //cerr<<y<<" "<<x.second+1<<" from "<<x.first<<endl; distancesTo1.push(make_pair(y,x.second+1)); visited2[y]=true; } } } //cerr<<"dist "<<distancesTo1.top().second<<endl;; while(!distancesTo1.empty()) { auto x = distancesTo1.top(); distancesTo1.pop(); if(x.second>1) { //cerr<<"removing "<<x.first<<" with distance "<<x.second<<endl; result[resultCount]=make_tuple('-',1,x.first); resultCount++; } } cout<<resultCount<<'\n'; char c; for(int i=0;i<resultCount;i++) { cout<<get<0>(result[i])<<" "<<get<1>(result[i])<<" "<<get<2>(result[i])<<"\n"; } } |