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
#include <bits/stdc++.h>
using namespace std;

constexpr int maxn = 30002;

int n,m1,m2, w1[maxn], w2[maxn], a,b, w1mx, w2mx, res;
vector <int> con1[maxn], con2[maxn], w1ls[maxn], w2ls[maxn];
vector <pair<int,int>> ls1, ls2, dod, us;
map <pair<int,int>, bool> mp1,mp2;
queue <int> q;

int main()
{
	ios_base::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);

	cin >> n >> m1;
	for(int i=1; i<=m1; i++)
	{
		cin >> a >> b;
		if(a > b) swap(a,b);
		if(a != 1)
		{
			mp1[{a,b}] = 1;
			ls1.push_back({a,b});
		}
		con1[a].push_back(b);
		con1[b].push_back(a);
	}

	cin >> m2;
	for(int i=1; i<=m2; i++)
	{
		cin >> a >> b;
		if(a > b) swap(a,b);
		if(a != 1)
		{
			mp2[{a,b}] = 1;
			ls2.push_back({a,b});
		}
		con2[a].push_back(b);
		con2[b].push_back(a);
	}

	for(auto ed : ls2)
		if(mp1[ed] != 1) dod.push_back(ed); 

	for(auto ed : ls1)
		if(mp2[ed] != 1) us.push_back(ed);

	for(int i=2; i<=n; i++) w1[i] = w2[i] = 1e9;

	q.push(1);
	while(!q.empty())
	{
		int v = q.front(); q.pop();
		for(auto u : con1[v])
		{
			if(w1[u] == 1e9)
			{
				w1[u] = w1[v]+1;
				q.push(u);
			}
		}
	}

	q.push(1);
	while(!q.empty())
	{
		int v = q.front(); q.pop();
		for(auto u : con2[v])
		{
			if(w2[u] == 1e9)
			{
				w2[u] = w2[v]+1;
				q.push(u);
			}
		}
	}

	for(int i=1; i<=n; i++)
	{
		w1mx = max(w1mx, w1[i]);
		w2mx = max(w2mx, w2[i]);
		w1ls[w1[i]].push_back(i);
		w2ls[w2[i]].push_back(i);
	}

	res = n-1-(int)con1[1].size();
	res += (int)dod.size();
	res += (int)us.size();
	res += (n-1-(int)con2[1].size());
	cout << res << '\n';

	for(int w=2; w<=w1mx; w++)
		for(auto u : w1ls[w])
			cout << "+ 1 " << u << '\n';

	for(auto ed : dod) cout << "+ " << ed.first << ' ' << ed.second << '\n';
	for(auto ed : us) cout << "- " << ed.first << ' ' << ed.second << '\n';

	for(int w=w2mx; w>=2; w--)
		for(auto u : w2ls[w])
			cout << "- 1 " << u << '\n';
}