#include <bits/stdc++.h> using namespace std; #define mp make_pair #define pb push_back // #define st first #define nd second #define fi first #define se second #define vt vector #define FOR(a, b, c) for(int a=b; a<c; ++a) #define all(a) (a).begin(),(a).end() #define sz(a) (int)(a).size() typedef long long ll; typedef long double ld; typedef pair<ll, ll> pll; typedef pair<int, int> pii; typedef vector<int> vi; typedef vector<ll> vll; typedef pii PII; typedef pll PLL; constexpr ll nax = 30969, INF = 1e9+2137; constexpr ld eps = 1e-9; mt19937_64 rng(6969); // mt19937_64 rng(chrono::system_clock::now().time_since_epoch().count()); inline ll rand(ll l, ll r) { return uniform_int_distribution<ll>(l, r)(rng); } int n, ms, md; vi graph[2][nax]; int dis[2][nax]; vector<pii> in_edges, out_edges; vector<bool> vis; void bfs(int a, int which) { queue<int> q; q.push(a); vis[a] = 1; while(!q.empty()) { int cur = q.front(); q.pop(); for(auto e: graph[which][cur]) { if(vis[e]) continue; dis[which][e] = dis[which][cur] + 1; q.push(e); vis[e] = 1; } } } int32_t main() { // ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); scanf("%d%d", &n, &ms); in_edges.resize(ms); for(auto &e: in_edges) { scanf("%d%d", &e.fi, &e.se); graph[0][e.fi].pb(e.se); graph[0][e.se].pb(e.fi); } scanf("%d", &md); out_edges.resize(md); for(auto &e: out_edges) { scanf("%d%d", &e.fi, &e.se); graph[1][e.fi].pb(e.se); graph[1][e.se].pb(e.fi); } vis.resize(n+1); bfs(1, 0); vis.clear(); vis.resize(n + 1); bfs(1, 1); vector<pii> order; for(int i = 1; i <= n; i++) { if(dis[0][i] >= 2) { order.pb(mp(dis[0][i], i)); } } sort(all(order)); vector<pair<int, pii>> answers; for(auto e: order) { // printf("+ %d %d\n", 1, e.se); answers.pb(mp(1, mp(1, e.se))); } for(auto e: in_edges) { if(e.fi == 1 || e.se == 1){ continue; } // printf("- %d %d\n", e.fi, e.se); answers.pb(mp(0, mp(e.fi, e.se))); } for(auto e: out_edges) { if(e.fi == 1 || e.se == 1){ continue; } // printf("+ %d %d\n", e.fi, e.se); answers.pb(mp(1, mp(e.fi, e.se))); } order.clear(); for(int i = 1; i <= n; i++) { if(dis[1][i] >= 2) { order.pb(mp(dis[1][i], i)); } } sort(all(order)); reverse(all(order)); for(auto e: order) { // printf("- %d %d\n", 1, e.se); answers.pb(mp(0, mp(1, e.se))); } printf("%d\n", (int)answers.size()); for(auto e: answers) { if(e.fi == 0) { printf("- "); } else { printf("+ "); } printf("%d %d\n", e.se.fi, e.se.se); } 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 | #include <bits/stdc++.h> using namespace std; #define mp make_pair #define pb push_back // #define st first #define nd second #define fi first #define se second #define vt vector #define FOR(a, b, c) for(int a=b; a<c; ++a) #define all(a) (a).begin(),(a).end() #define sz(a) (int)(a).size() typedef long long ll; typedef long double ld; typedef pair<ll, ll> pll; typedef pair<int, int> pii; typedef vector<int> vi; typedef vector<ll> vll; typedef pii PII; typedef pll PLL; constexpr ll nax = 30969, INF = 1e9+2137; constexpr ld eps = 1e-9; mt19937_64 rng(6969); // mt19937_64 rng(chrono::system_clock::now().time_since_epoch().count()); inline ll rand(ll l, ll r) { return uniform_int_distribution<ll>(l, r)(rng); } int n, ms, md; vi graph[2][nax]; int dis[2][nax]; vector<pii> in_edges, out_edges; vector<bool> vis; void bfs(int a, int which) { queue<int> q; q.push(a); vis[a] = 1; while(!q.empty()) { int cur = q.front(); q.pop(); for(auto e: graph[which][cur]) { if(vis[e]) continue; dis[which][e] = dis[which][cur] + 1; q.push(e); vis[e] = 1; } } } int32_t main() { // ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); scanf("%d%d", &n, &ms); in_edges.resize(ms); for(auto &e: in_edges) { scanf("%d%d", &e.fi, &e.se); graph[0][e.fi].pb(e.se); graph[0][e.se].pb(e.fi); } scanf("%d", &md); out_edges.resize(md); for(auto &e: out_edges) { scanf("%d%d", &e.fi, &e.se); graph[1][e.fi].pb(e.se); graph[1][e.se].pb(e.fi); } vis.resize(n+1); bfs(1, 0); vis.clear(); vis.resize(n + 1); bfs(1, 1); vector<pii> order; for(int i = 1; i <= n; i++) { if(dis[0][i] >= 2) { order.pb(mp(dis[0][i], i)); } } sort(all(order)); vector<pair<int, pii>> answers; for(auto e: order) { // printf("+ %d %d\n", 1, e.se); answers.pb(mp(1, mp(1, e.se))); } for(auto e: in_edges) { if(e.fi == 1 || e.se == 1){ continue; } // printf("- %d %d\n", e.fi, e.se); answers.pb(mp(0, mp(e.fi, e.se))); } for(auto e: out_edges) { if(e.fi == 1 || e.se == 1){ continue; } // printf("+ %d %d\n", e.fi, e.se); answers.pb(mp(1, mp(e.fi, e.se))); } order.clear(); for(int i = 1; i <= n; i++) { if(dis[1][i] >= 2) { order.pb(mp(dis[1][i], i)); } } sort(all(order)); reverse(all(order)); for(auto e: order) { // printf("- %d %d\n", 1, e.se); answers.pb(mp(0, mp(1, e.se))); } printf("%d\n", (int)answers.size()); for(auto e: answers) { if(e.fi == 0) { printf("- "); } else { printf("+ "); } printf("%d %d\n", e.se.fi, e.se.se); } return 0; } |