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

using namespace std;

struct Event {
    bool type;
    int v, w;
};

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

    // input
    int V;
    cin >> V;

    int E1;
    cin >> E1;
    vector<set<int>> adj1(V);
    for (int i = 0; i < E1; i++) {
        int v, w;
        cin >> v >> w;
        v--, w--;

        adj1[v].insert(w);
        adj1[w].insert(v);
    }

    int E2;
    cin >> E2;
    vector<set<int>> adj2(V);
    for (int i = 0; i < E2; i++) {
        int v, w;
        cin >> v >> w;
        v--, w--;

        adj2[v].insert(w);
        adj2[w].insert(v);
    }

    vector<Event> result;

    // find bfs order in graph1
    vector<int> order;
    order.reserve(V);
    queue<int> toVisit;
    toVisit.push(0);
    vector<bool> visited(V, false);
    visited[0] = true;
    while (not toVisit.empty()) {
        int v = toVisit.front();
        toVisit.pop();

        order.push_back(v);

        for (int w: adj1[v])
            if (not visited[w]) {
                visited[w] = true;
                toVisit.push(w);
            }
    }

    // add star
    for (int i = 1; i < V; i++) {
        auto find = adj1[0].find(order[i]);
        if (find == adj1[0].end())
            result.push_back({true, 0, order[i]});
    }

    // add edges
    for (int v = 1; v < V; v++)
        for (int w: adj2[v]) {
            if (w == 0 or w < v)
                continue;

            auto find = adj1[v].find(w);
            if (find == adj1[v].end())
                result.push_back({true, v, w});
        }

    // remove edges
    for (int v = 1; v < V; v++)
        for (int w: adj1[v]) {
            if (w == 0 or w < v)
                continue;

            auto find = adj2[v].find(w);
            if (find == adj2[v].end())
                result.push_back({false, v, w});
        }

    // find bfs order in graph2
    order.clear();
    order.reserve(V);
    toVisit.push(0);
    visited.assign(V, false);
    visited[0] = true;
    while (not toVisit.empty()) {
        int v = toVisit.front();
        toVisit.pop();

        order.push_back(v);

        for (int w: adj2[v])
            if (not visited[w]) {
                visited[w] = true;
                toVisit.push(w);
            }
    }

    // remove star
    for (int i = V - 1; i >= 1; i--) {
        auto find = adj2[0].find(order[i]);
        if (find == adj2[0].end())
            result.push_back({false, 0, order[i]});
    }

    // output
    cout << result.size() << "\n";
    for (auto &event: result)
        cout << (event.type ? "+ " : "- ") << event.v + 1 << " " << event.w + 1 << "\n";

    return 0;
}