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
#include <iostream>
#include <set>
#include <vector>

std::set<int> current[30010];
std::set<int> goal[30010];
bool visited[30010];
std::vector<std::pair<int, std::pair<int, int>>> res;

int n, m, a, b;

void dfs_pre(int x) {
    if (x != 1 && current[x].find(1) == current[x].end()) {
        current[x].insert(1);
        current[1].insert(x);
        res.emplace_back(1, std::pair(1, x));
    }

    visited[x] = true;

    for (int it : current[x]) if (!visited[it]) dfs_pre(it);
}

void dfs_post(int x) {
    visited[x] = true;

    for (int it : goal[x]) if (!visited[it]) dfs_post(it);

    if (x != 1 && goal[x].find(1) == goal[x].end()) {
        current[x].erase(1);
        current[1].erase(x);
        res.emplace_back(-1, std::pair(1, x));
    }
}

int main() {
    std::cin >> n;
    std::cin >> m;

    for (int i = 0; i < m; i++) {
        std::cin >> a >> b;
        current[a].insert(b);
        current[b].insert(a);
    }

    std::cin >> m;

    for (int i = 0; i < m; i++) {
        std::cin >> a >> b;
        goal[a].insert(b);
        goal[b].insert(a);
    }

    dfs_pre(1);

    for (int i = 0; i <= n; i++) {
        visited[i] = 0;
    }

    for (int i = 2; i <= n; i++) {
        for (auto it = goal[i].begin(); it != goal[i].end(); ++it) {
            if (current[i].find(*it) == current[i].end()) {
                current[i].insert(*it);
                current[*it].insert(i);
                res.emplace_back(1, std::pair(i, *it));
            }
        }

        for (auto it = current[i].begin(); it != current[i].end(); ) {
            if (goal[i].find(*it) == goal[i].end() && *it != 1) {
                res.emplace_back(-1, std::pair(i, *it));
                current[*it].erase(i);
                it = current[i].erase(it);
            }
            else {
                ++it;
            }
        }
    }

    dfs_post(1);

    std::cout << res.size() << "\n";
    for (auto & re : res) {
        if (re.first == -1) {
            std::cout << "- ";
        } else {
            std::cout << "+ ";
        }
        std::cout << re.second.first << " " << re.second.second << "\n";
    }

    return 0;
}