#include <iostream> #include <vector> #include <queue> #include <algorithm> using namespace std; queue <int> kol; void bfs(vector <vector <int>> &som,vector <int> &kod) { kol.push(1); while(!kol.empty()) { int a=kol.front(); kol.pop(); for(int i=0;i<som[a].size();i++) { if(kod[som[a][i]]==-1) { kol.push(som[a][i]); kod[som[a][i]]=kod[a]+1; } } } } vector <int> od,dod; bool cmp(int a,int b) { return od[a]<od[b]; } bool dmp(int a,int b) { return dod[a]>dod[b]; } int main() { cin.tie(0)->sync_with_stdio(false); int n,p,k; cin>>n>>p; vector <int> zaw(n); for(int i=0;i<n;i++) zaw[i]=i+1; vector <int> pus(0); od.resize(n+1),dod.resize(n+1); for(int i=0;i<=n;i++) { dod[i]=-1; od[i]=-1; } vector <pair<int,int>> pie(0),rug(0); vector <vector <int>> som(n+1,pus),dom(n+1,pus); for(int i=0;i<p;i++) { int a,b; cin>>a>>b; pair <int,int> o; o.first=a; o.second=b; pie.push_back(o); som[a].push_back(b); som[b].push_back(a); } cin>>k; for(int i=0;i<k;i++) { int a,b; cin>>a>>b; pair <int,int> o; o.first=a; o.second=b; rug.push_back(o); dom[a].push_back(b); dom[b].push_back(a); } od[1]=0; dod[1]=0; bfs(som,od); bfs(dom,dod); vector <string> wrot(0); //for(int i=1;i<=n;i++) cout<<i<<" "<<od[i]<<" "<<dod[i]<<"\n"; sort(zaw.begin(),zaw.end(),cmp); for(int i=0;i<n;i++) { if(od[zaw[i]]>1) { wrot.push_back(""); wrot[wrot.size()-1]="+ 1 "+to_string(zaw[i]); } } for(int i=0;i<pie.size();i++) { if(pie[i].first!=1&&pie[i].second!=1) { wrot.push_back(""); wrot[wrot.size()-1]="- "+to_string(pie[i].first)+" "+to_string(pie[i].second); } } for(int i=0;i<rug.size();i++) { if(rug[i].first!=1&&rug[i].second!=1) { wrot.push_back(""); wrot[wrot.size()-1]="+ "+to_string(rug[i].first)+" "+to_string(rug[i].second); } } sort(zaw.begin(),zaw.end(),dmp); for(int i=0;i<n;i++) { if(dod[zaw[i]]>1) { wrot.push_back(""); wrot[wrot.size()-1]="- 1 "+to_string(zaw[i]); } } cout<<wrot.size()<<"\n"; for(int i=0;i<wrot.size();i++) cout<<wrot[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 | #include <iostream> #include <vector> #include <queue> #include <algorithm> using namespace std; queue <int> kol; void bfs(vector <vector <int>> &som,vector <int> &kod) { kol.push(1); while(!kol.empty()) { int a=kol.front(); kol.pop(); for(int i=0;i<som[a].size();i++) { if(kod[som[a][i]]==-1) { kol.push(som[a][i]); kod[som[a][i]]=kod[a]+1; } } } } vector <int> od,dod; bool cmp(int a,int b) { return od[a]<od[b]; } bool dmp(int a,int b) { return dod[a]>dod[b]; } int main() { cin.tie(0)->sync_with_stdio(false); int n,p,k; cin>>n>>p; vector <int> zaw(n); for(int i=0;i<n;i++) zaw[i]=i+1; vector <int> pus(0); od.resize(n+1),dod.resize(n+1); for(int i=0;i<=n;i++) { dod[i]=-1; od[i]=-1; } vector <pair<int,int>> pie(0),rug(0); vector <vector <int>> som(n+1,pus),dom(n+1,pus); for(int i=0;i<p;i++) { int a,b; cin>>a>>b; pair <int,int> o; o.first=a; o.second=b; pie.push_back(o); som[a].push_back(b); som[b].push_back(a); } cin>>k; for(int i=0;i<k;i++) { int a,b; cin>>a>>b; pair <int,int> o; o.first=a; o.second=b; rug.push_back(o); dom[a].push_back(b); dom[b].push_back(a); } od[1]=0; dod[1]=0; bfs(som,od); bfs(dom,dod); vector <string> wrot(0); //for(int i=1;i<=n;i++) cout<<i<<" "<<od[i]<<" "<<dod[i]<<"\n"; sort(zaw.begin(),zaw.end(),cmp); for(int i=0;i<n;i++) { if(od[zaw[i]]>1) { wrot.push_back(""); wrot[wrot.size()-1]="+ 1 "+to_string(zaw[i]); } } for(int i=0;i<pie.size();i++) { if(pie[i].first!=1&&pie[i].second!=1) { wrot.push_back(""); wrot[wrot.size()-1]="- "+to_string(pie[i].first)+" "+to_string(pie[i].second); } } for(int i=0;i<rug.size();i++) { if(rug[i].first!=1&&rug[i].second!=1) { wrot.push_back(""); wrot[wrot.size()-1]="+ "+to_string(rug[i].first)+" "+to_string(rug[i].second); } } sort(zaw.begin(),zaw.end(),dmp); for(int i=0;i<n;i++) { if(dod[zaw[i]]>1) { wrot.push_back(""); wrot[wrot.size()-1]="- 1 "+to_string(zaw[i]); } } cout<<wrot.size()<<"\n"; for(int i=0;i<wrot.size();i++) cout<<wrot[i]<<"\n"; } |