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
#include "bits/stdc++.h"

using namespace std;

vector<string> res;

void dfs_add(const vector<unordered_set<int>> &G, int v, vector<bool> &vis){
    vis[v] = true;
    if(v != 0 && !G[0].count(v)) 
        res.push_back("+ 1 " + to_string(v + 1) + '\n');     

    for(auto w : G[v])
        if(!vis[w])
            dfs_add(G, w, vis);
}

void dfs_delete(const vector<unordered_set<int>> &G, int v, vector<bool> &vis){
    vis[v] = true;
    for(auto w : G[v])
        if(!vis[w])
            dfs_delete(G, w, vis);

    if(v != 0 && !G[0].count(v)) 
        res.push_back( "- 1 " + to_string(v + 1) + '\n');     
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    
    int n; cin >> n;
    vector<unordered_set<int>> G(n);
    int m; cin >> m;
    while(m--){
        int a, b; cin >> a >> b; a--; b--;
        G[a].insert(b);
        G[b].insert(a);
    }

    vector<unordered_set<int>> G_docelowy(n);
    cin >> m;
    while(m--){
        int a, b; cin >> a >> b; a--; b--;
        G_docelowy[a].insert(b);
        G_docelowy[b].insert(a);
    }

    vector<bool> tmp(n);
    dfs_add(G, 0, tmp);

    for(int i=1; i<n; i++){
        for(auto a : G[i])
            if(a != 0 && a < i)
                if(!G_docelowy[i].count(a)) 
                    res.push_back("- " + to_string(i + 1) + ' ' + to_string(a + 1) + '\n');

        for(auto a : G_docelowy[i])
            if(a != 0 && a < i)
                if(!G[i].count(a)) 
                    res.push_back("+ " + to_string(i + 1) + ' ' + to_string(a + 1) + '\n');
    }

    tmp = vector<bool> (n);
    dfs_delete(G_docelowy, 0, tmp);

    cout << res.size() << '\n';
    for(auto r : res) cout << r;
    return 0;
}