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
#include <cstdio>
#include <set>
#include <vector>
using namespace std;

#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)
#define VAR(v,w) __typeof(w) v=(w)
#define FORE(it,c) for(VAR(it,(c).begin());it!=(c).end();++it)
#define PB push_back
#define SIZE(c) ((int)(c).size())
#define MP make_pair
#define FT first
#define SD second
#define INT(x) int x; scanf("%d", &x)

typedef pair<int,int> PII;
typedef vector<int> VI;
typedef vector<VI> VVI;

struct Op {
	Op(char t, int a, int b) : t(t), a(a), b(b) {}
	char t;
	int a, b;
};

VVI gs, gd;
set<PII> es, ed, add, rem;
vector<Op> op;
bool added[30000], remed[30000];

void goAdd(int v) {
	if (added[v]) return;
	added[v] = 1;
	if (v && es.find(MP(0, v)) == es.end())
		op.PB(Op('+', 0, v));
	FORE(it,gs[v]) goAdd(*it);
}

void goRem(int v) {
	if (remed[v]) return;
	remed[v] = 1;
	FORE(it,gd[v]) goRem(*it);
	if (v && ed.find(MP(0, v)) == ed.end())
		op.PB(Op('-', 0, v));
}

int main() {
	INT(n);
	INT(ms);
	REP(j,ms) {
		INT(a);
		INT(b);
		--a;
		--b;
		if (a > b) swap(a, b);
		es.insert(MP(a, b));
	}
	INT(md);
	REP(j,md) {
		INT(a);
		INT(b);
		--a;
		--b;
		if (a > b) swap(a, b);
		ed.insert(MP(a, b));
	}
	FORE(it,ed) if (es.find(*it) == es.end()) add.insert(*it);
	FORE(it,es) if (ed.find(*it) == ed.end()) rem.insert(*it);
	gs.resize(n);
	FORE(it,es) {
		gs[it->FT].PB(it->SD);
		gs[it->SD].PB(it->FT);
	}
	gd.resize(n);
	FORE(it,ed) {
		gd[it->FT].PB(it->SD);
		gd[it->SD].PB(it->FT);
	}
	goAdd(0);
	FORE(it,add) if (it->FT) op.PB(Op('+', it->FT, it->SD));
	FORE(it,rem) if (it->FT) op.PB(Op('-', it->FT, it->SD));
	goRem(0);
	printf("%d\n", SIZE(op));
	FORE(it,op) printf("%c %d %d\n", it->t, it->a + 1, it->b + 1);
}