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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// Online C++ compiler to run C++ program online
#include <iostream>
#include <vector>
#include <unordered_set>
#include <queue>

using namespace std;

vector<vector<int>> res;

void dodaj(int a, int b) {
    res.push_back({1, a+1, b+1});
}
void odejmij(int a, int b) {
    res.push_back({-1, a+1, b+1});
}

void zrob_gwiazde(int root, vector<unordered_set<int>>& v) {
    vector<bool> vis(v.size(), false);
    vis[root] = true;
    
    queue<pair<int, int>> q; // current node, parent
    q.push(make_pair(root, -1));
    
    while(!q.empty()) {
        pair<int,int> top = q.front();
        q.pop();
        
        int node = top.first;
        int prev = top.second;
        
        if(prev != -1 && prev != root) {
            if(v[root].find(node) == v[root].end()) {
                v[root].insert(node);
                v[node].insert(root);
                dodaj(root, node);
            }
        }
        
        for(int neigh : v[node]) if(!vis[neigh]) {
            q.push(make_pair(neigh, node));
            vis[neigh] = true;
        }
    }
}

void dfs(int root, int node, vector<unordered_set<int>>& v, vector<unordered_set<int>>& v2, vector<bool>& vis) {
    if(node == root) {
      for(int neigh : v2[root]) if(!vis[neigh]) {
      vis[neigh] = true;
      dfs(root, neigh, v, v2, vis);
      }
    } else {
        for(int neigh : v[node]) if(!vis[neigh]) {
        vis[neigh] = true;
        dfs(root, neigh, v, v2, vis);
    }
    }


    
    if(v[node].find(root) != v[node].end() &&
        v2[node].find(root) == v2[node].end()) {
        v2[node].erase(root);
        v2[root].erase(node);
        odejmij(node, root);
    }
}

int main() {
    int n;
    cin >> n;
    
    vector<unordered_set<int>> v(n, unordered_set<int>());
    int m;
    cin >> m;
    for(int i = 0; i < m; i++) {
        int a, b;
        cin >> a >> b;
        v[a-1].insert(b-1);
        v[b-1].insert(a-1);
    }
    
    int m2;
    cin >> m2;
    vector<unordered_set<int>> v2(n, unordered_set<int>());
    for(int i = 0; i < m2; i++) {
        int a, b;
        cin >> a >> b;
        v2[a-1].insert(b-1);
        v2[b-1].insert(a-1);
    }
    
    int root = 0;
    
    zrob_gwiazde(root, v);
    // star
    // remove edges
    
    vector<unordered_set<int>> new_v(n, unordered_set<int>());
    
    for(int i = 0; i < n; i++) {
        for(int j : v[i]) {
            if(i == root || j == root || v2[i].find(j) != v2[i].end()) {
            
            new_v[i].insert(j);
            } else {
            odejmij(i,j);
            v[j].erase(i);
            }
        }
    }
    v = new_v;
    
    // add edges 
    for(int i = 0; i < n; i++) {
        for(int j : v2[i]) {
            
            if(v[i].find(j) == v[i].end()) {
                dodaj(i, j);
                v[i].insert(j);
                v[j].insert(i);
            }
        }
    }
    
    vector<bool> vis(n, false);
    vis[root] = true;
    dfs(root, root, v, v2, vis);
    
    cout << res.size() << endl;
    for(auto& r : res) {
        if(r[0] == 1) {
            cout << "+ ";
        } else {
            cout << "- ";
        }
        cout << r[1] << " " << r[2] << endl;
    }
    
    return 0;
}