#include<cstdio>
#include<algorithm>
#include<utility>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<cmath>
// macros
#define FORE(c, a, b) for(int c=a; c<= int(b); ++c)
#define FORD(c, a, b) for(int c=a; c>= int(b); --c)
#define FORIT(it, cont, cont_t) for(cont_t::iterator it = cont.begin(); it != cont.end(); ++it)
#define REP(i, n) for(int i = 0; i < (n); ++i)
#define ALL(a) a.begin(), a.end()
#define PR(n) printf("%d ", (int) (n))
#define PRL(n) printf("%lld ", (ll) (n))
#define xx first
#define yy second
#define pb push_back
#define mp make_pair
#define itr iterator
#define dbg if(0)
#define prd dbg printf
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned int uint;
typedef vector<int> vi;
typedef set<int> si;
typedef map<int, int> mi;
typedef pair<int, int> pi;
typedef vector<pi> vii;
typedef vector<vi> vvi;
// actual code
#define NMAX 30005
int n, layer[NMAX];
vi edges[NMAX];
vii moves_plus, moves_minus;
vector<string> ans;
void scan_graph() {
FORE(i, 1, n) {
edges[i].clear();
layer[i] = 0;
}
moves_plus.clear();
moves_minus.clear();
int m;
scanf("%d", &m);
REP(i, m) {
int u, v;
scanf("%d%d", &u, &v);
edges[u].pb(v);
edges[v].pb(u);
if (u!=1 && v!=1)
moves_minus.pb(mp(u,v));
}
queue<int> bfs;
bfs.push(1);
layer[1] = 1;
while (!bfs.empty()) {
int v = bfs.front();
bfs.pop();
if (layer[v] > 2)
moves_plus.pb(mp(1,v));
FORIT(it, edges[v], vi) {
int u = *it;
if (layer[u] > 0)
continue;
layer[u] = layer[v] + 1;
bfs.push(u);
}
}
}
int main() {
scanf("%d", &n);
scan_graph();
FORIT(it, moves_plus, vii) {
//printf("+ %d %d\n", it->xx, it->yy);
ans.pb("+ " + to_string(it->xx) + " " + to_string(it->yy));
}
FORIT(it, moves_minus, vii) {
//printf("- %d %d\n", it->xx, it->yy);
ans.pb("- " + to_string(it->xx) + " " + to_string(it->yy));
}
scan_graph();
FORIT(it, moves_minus, vii) {
//printf("+ %d %d\n", it->xx, it->yy);
ans.pb("+ " + to_string(it->xx) + " " + to_string(it->yy));
}
reverse(ALL(moves_plus));
FORIT(it, moves_plus, vii) {
//printf("- %d %d\n", it->xx, it->yy);
ans.pb("- " + to_string(it->xx) + " " + to_string(it->yy));
}
printf("%d\n", (int) ans.size());
FORIT(it, ans, vector<string>)
printf("%s\n", it->c_str());
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 | #include<cstdio> #include<algorithm> #include<utility> #include<string> #include<vector> #include<map> #include<set> #include<queue> #include<stack> #include<cmath> // macros #define FORE(c, a, b) for(int c=a; c<= int(b); ++c) #define FORD(c, a, b) for(int c=a; c>= int(b); --c) #define FORIT(it, cont, cont_t) for(cont_t::iterator it = cont.begin(); it != cont.end(); ++it) #define REP(i, n) for(int i = 0; i < (n); ++i) #define ALL(a) a.begin(), a.end() #define PR(n) printf("%d ", (int) (n)) #define PRL(n) printf("%lld ", (ll) (n)) #define xx first #define yy second #define pb push_back #define mp make_pair #define itr iterator #define dbg if(0) #define prd dbg printf using namespace std; typedef long long ll; typedef long double ld; typedef unsigned int uint; typedef vector<int> vi; typedef set<int> si; typedef map<int, int> mi; typedef pair<int, int> pi; typedef vector<pi> vii; typedef vector<vi> vvi; // actual code #define NMAX 30005 int n, layer[NMAX]; vi edges[NMAX]; vii moves_plus, moves_minus; vector<string> ans; void scan_graph() { FORE(i, 1, n) { edges[i].clear(); layer[i] = 0; } moves_plus.clear(); moves_minus.clear(); int m; scanf("%d", &m); REP(i, m) { int u, v; scanf("%d%d", &u, &v); edges[u].pb(v); edges[v].pb(u); if (u!=1 && v!=1) moves_minus.pb(mp(u,v)); } queue<int> bfs; bfs.push(1); layer[1] = 1; while (!bfs.empty()) { int v = bfs.front(); bfs.pop(); if (layer[v] > 2) moves_plus.pb(mp(1,v)); FORIT(it, edges[v], vi) { int u = *it; if (layer[u] > 0) continue; layer[u] = layer[v] + 1; bfs.push(u); } } } int main() { scanf("%d", &n); scan_graph(); FORIT(it, moves_plus, vii) { //printf("+ %d %d\n", it->xx, it->yy); ans.pb("+ " + to_string(it->xx) + " " + to_string(it->yy)); } FORIT(it, moves_minus, vii) { //printf("- %d %d\n", it->xx, it->yy); ans.pb("- " + to_string(it->xx) + " " + to_string(it->yy)); } scan_graph(); FORIT(it, moves_minus, vii) { //printf("+ %d %d\n", it->xx, it->yy); ans.pb("+ " + to_string(it->xx) + " " + to_string(it->yy)); } reverse(ALL(moves_plus)); FORIT(it, moves_plus, vii) { //printf("- %d %d\n", it->xx, it->yy); ans.pb("- " + to_string(it->xx) + " " + to_string(it->yy)); } printf("%d\n", (int) ans.size()); FORIT(it, ans, vector<string>) printf("%s\n", it->c_str()); return 0; } |
English