//ANMHLIJKTJIY! #pragma GCC optimize(2) #pragma GCC optimize("Ofast") #pragma GCC optimize("inline","fast-math","unroll-loops","no-stack-protector") #pragma GCC diagnostic error "-fwhole-program" #pragma GCC diagnostic error "-fcse-skip-blocks" #pragma GCC diagnostic error "-funsafe-loop-optimizations" #include <bits/stdc++.h> #define INF 1000000000 #define LINF 1000000000000000000 #define MOD 1000000007 #define mod 998244353 #define F first #define S second #define ll int #define N 30010 using namespace std; ll n,m1,m2,dep[N]; vector<pair<ll,pair<ll,ll> > > ans; vector<pair<ll,ll> > ed1,ed2; vector<ll> vt[N]; void bfs1() { ll i; memset(dep,63,sizeof(dep)); dep[0]=0; for(i=0;i<n;i++) { vt[i].clear(); } for(i=0;i<ed1.size();i++) { vt[ed1[i].F].push_back(ed1[i].S); vt[ed1[i].S].push_back(ed1[i].F); } vector<ll> seq; queue<ll> qu; qu.push(0); while(!qu.empty()) { ll x=qu.front(); qu.pop(); if(dep[x]>1) { seq.push_back(x); } for(i=0;i<vt[x].size();i++) { if(dep[vt[x][i]]>dep[x]+1) { dep[vt[x][i]]=dep[x]+1; qu.push(vt[x][i]); } } } for(i=0;i<seq.size();i++) { ans.push_back(make_pair(0,make_pair(0,seq[i]))); } return; } void bfs2() { ll i; memset(dep,63,sizeof(dep)); dep[0]=0; for(i=0;i<n;i++) { vt[i].clear(); } for(i=0;i<ed2.size();i++) { vt[ed2[i].F].push_back(ed2[i].S); vt[ed2[i].S].push_back(ed2[i].F); } vector<ll> seq; queue<ll> qu; qu.push(0); while(!qu.empty()) { ll x=qu.front(); qu.pop(); if(dep[x]>1) { seq.push_back(x); } for(i=0;i<vt[x].size();i++) { if(dep[vt[x][i]]>dep[x]+1) { dep[vt[x][i]]=dep[x]+1; qu.push(vt[x][i]); } } } for(i=seq.size()-1;i>=0;i--) { ans.push_back(make_pair(1,make_pair(0,seq[i]))); } return; } int main(){ ll i,x,y; scanf("%d%d",&n,&m1); for(i=0;i<m1;i++) { scanf("%d%d",&x,&y); x--,y--; ed1.push_back(make_pair(x,y)); } scanf("%d",&m2); for(i=0;i<m2;i++) { scanf("%d%d",&x,&y); x--,y--; ed2.push_back(make_pair(x,y)); } bfs1(); for(i=0;i<m1;i++) { if(ed1[i].F>0&&ed1[i].S>0) { ans.push_back(make_pair(1,ed1[i])); } } for(i=0;i<m2;i++) { if(ed2[i].F>0&&ed2[i].S>0) { ans.push_back(make_pair(0,ed2[i])); } } bfs2(); printf("%d\n",ans.size()); for(i=0;i<ans.size();i++) { printf("%c %d %d\n",ans[i].F?'-':'+',ans[i].S.F+1,ans[i].S.S+1); } 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 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 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 | //ANMHLIJKTJIY! #pragma GCC optimize(2) #pragma GCC optimize("Ofast") #pragma GCC optimize("inline","fast-math","unroll-loops","no-stack-protector") #pragma GCC diagnostic error "-fwhole-program" #pragma GCC diagnostic error "-fcse-skip-blocks" #pragma GCC diagnostic error "-funsafe-loop-optimizations" #include <bits/stdc++.h> #define INF 1000000000 #define LINF 1000000000000000000 #define MOD 1000000007 #define mod 998244353 #define F first #define S second #define ll int #define N 30010 using namespace std; ll n,m1,m2,dep[N]; vector<pair<ll,pair<ll,ll> > > ans; vector<pair<ll,ll> > ed1,ed2; vector<ll> vt[N]; void bfs1() { ll i; memset(dep,63,sizeof(dep)); dep[0]=0; for(i=0;i<n;i++) { vt[i].clear(); } for(i=0;i<ed1.size();i++) { vt[ed1[i].F].push_back(ed1[i].S); vt[ed1[i].S].push_back(ed1[i].F); } vector<ll> seq; queue<ll> qu; qu.push(0); while(!qu.empty()) { ll x=qu.front(); qu.pop(); if(dep[x]>1) { seq.push_back(x); } for(i=0;i<vt[x].size();i++) { if(dep[vt[x][i]]>dep[x]+1) { dep[vt[x][i]]=dep[x]+1; qu.push(vt[x][i]); } } } for(i=0;i<seq.size();i++) { ans.push_back(make_pair(0,make_pair(0,seq[i]))); } return; } void bfs2() { ll i; memset(dep,63,sizeof(dep)); dep[0]=0; for(i=0;i<n;i++) { vt[i].clear(); } for(i=0;i<ed2.size();i++) { vt[ed2[i].F].push_back(ed2[i].S); vt[ed2[i].S].push_back(ed2[i].F); } vector<ll> seq; queue<ll> qu; qu.push(0); while(!qu.empty()) { ll x=qu.front(); qu.pop(); if(dep[x]>1) { seq.push_back(x); } for(i=0;i<vt[x].size();i++) { if(dep[vt[x][i]]>dep[x]+1) { dep[vt[x][i]]=dep[x]+1; qu.push(vt[x][i]); } } } for(i=seq.size()-1;i>=0;i--) { ans.push_back(make_pair(1,make_pair(0,seq[i]))); } return; } int main(){ ll i,x,y; scanf("%d%d",&n,&m1); for(i=0;i<m1;i++) { scanf("%d%d",&x,&y); x--,y--; ed1.push_back(make_pair(x,y)); } scanf("%d",&m2); for(i=0;i<m2;i++) { scanf("%d%d",&x,&y); x--,y--; ed2.push_back(make_pair(x,y)); } bfs1(); for(i=0;i<m1;i++) { if(ed1[i].F>0&&ed1[i].S>0) { ans.push_back(make_pair(1,ed1[i])); } } for(i=0;i<m2;i++) { if(ed2[i].F>0&&ed2[i].S>0) { ans.push_back(make_pair(0,ed2[i])); } } bfs2(); printf("%d\n",ans.size()); for(i=0;i<ans.size();i++) { printf("%c %d %d\n",ans[i].F?'-':'+',ans[i].S.F+1,ans[i].S.S+1); } return 0; } |