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
#include <iostream>
#include <set>
#include <vector>
#include <sstream>
#define nl '\n'

using namespace std;

int n, op_count = 0;
vector<string> ops;

vector<set<int>> G, Gd; 
vector<int> vis, vis2;

void addEdge(int u, int v){
	op_count++;
	stringstream ss;
	ss<<"+ "<<u<<' '<<v<<nl;
	ops.push_back(ss.str());
	G[u].insert(v);
	G[v].insert(u);
}

void delEdge(int u, int v){
	op_count++;
	stringstream ss;
	ss<<"- "<<u<<' '<<v<<nl;
	ops.push_back(ss.str());
	G[u].erase(v);
	G[v].erase(u);
}

void dfs(int v){
	vis[v] = 1;
	for(auto i:G[v]){
		if(vis[i]) continue;
		if(!G[1].count(i)){ addEdge(1, i); }
		dfs(i);
	}
}

void dfs2(int v){
	vis2[v] = 1;
	for(auto i:Gd[v]){
		if(vis2[i]) continue;
		dfs2(i);
		if(!Gd[1].count(i)){ delEdge(1, i); }
	}
}

int main()
{
	cin.tie(0)->sync_with_stdio(0);
	cin>>n;
	vis.resize(n+1, 0);
	vis2.resize(n+1, 0);
	G.resize(n+1, {});
	Gd.resize(n+1, {});
	int m1, m2;
	cin>>m1;
	while(m1--){
		int u, v;
		cin>>u>>v;
		G[u].insert(v);
		G[v].insert(u);
	}
	cin>>m2;
	while(m2--){
		int u, v;
		cin>>u>>v;
		Gd[u].insert(v);
		Gd[v].insert(u);
	}
	dfs(1);
	for(int i=1; i<=n; i++){
		for(auto v : Gd[i]){
			if(!G[i].count(v)){ addEdge(i, v); }
		}
	}
	for(int i=2; i<=n; i++){
		vector<int> to_del;
		for(auto v : G[i]){
			if(!Gd[i].count(v) && v != 1){ to_del.push_back(v); }
		}
		for(auto v: to_del){
			delEdge(i, v);
		}
	}
	dfs2(1);
	cout<<op_count<<nl;
	for(auto i:ops){
		cout<<i;
	}
	return 0;
}