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

struct Triple {
    int sign, st, nd;

    Triple(int sign, int st, int nd) : sign(sign), st(st), nd(nd) {};
};

void dfs(vector<vector<int>> &graph, vector<bool> &vis, vector<int> &queue, set<pair<int, int>> &expected, int v) {
    if (vis[v]) return;
    vis[v] = true;

    for (auto w : graph[v]) {
        if (v == 1 && expected.find({v, w}) == expected.end()) continue;
        dfs(graph, vis, queue, expected, w);
    }
    if (v != 1) queue.push_back(v);
}

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

    int n, m; cin >> n >> m;
    set<pair<int, int>> s;
    for (int i = 0; i < m; i++) {
        int v, w; cin >> v >> w;
        if (v > w) s.insert({w, v});
        else s.insert({v, w});
    }

    int m1; cin >> m1;
    set<pair<int, int>> expected;
    for (int i = 0; i < m1; i++) {
        int v, w; cin >> v >> w;
        if (v > w) expected.insert({w, v});
        else expected.insert({v, w});
    }

    vector<Triple> ans;
    for (int i = 2; i <= n; i++) {
        if (s.find({1, i}) == s.end()) {
            s.insert({1, i});
            ans.push_back(Triple(1, 1, i));
        }
    }

    for (auto elem : expected) {
        if (s.find(elem) == s.end()) {
            s.insert(elem);
            ans.push_back(Triple(1, elem.first, elem.second));
        }
    }

    vector<pair<int, int>> toRemove;
    for (auto elem : s) {
        if (elem.first != 1 && expected.find(elem) == expected.end()) {
            toRemove.push_back(elem);
            ans.push_back(Triple(0, elem.first, elem.second));
        }
    }
    for (auto elem : toRemove) s.erase(elem);

    vector<vector<int>> graph(n+1);
    for (auto elem : s) {
        graph[elem.first].push_back(elem.second);
        graph[elem.second].push_back(elem.first);
    }

    vector<int> queue;
    vector<bool> vis(n+1, false);
    dfs(graph, vis, queue, expected, 1);
    for (auto elem : queue) {
        if (expected.find({1, elem}) == expected.end())
            ans.push_back(Triple(0, 1, elem));
    }

    cout << ans.size() << "\n";
    for (auto elem : ans) {
        cout << (elem.sign ? "+ " : "- ") << elem.st << " " << elem.nd << "\n"; 
    }
    return 0;
}