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
143
144
145
146
147
#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
#include <unordered_set>

constexpr int maxn = 3e4 + 7, maxm = 5e4 + 7;
int n, m1, m2;
std::pair<int, int> startEdges[maxm], endEdges[maxm];
std::vector<std::pair<int, int>> edgesToAdd, edgesToDelete;
std::vector<int> kolejnoscDodawania;
std::vector<int> graf[maxn], graf2[maxn];
bool connectedTo1[maxn], connectedTo12[maxn];

std::vector<std::pair<bool, std::pair<int, int>>> ans;

void input() {
    scanf("%d", &n);

    scanf("%d", &(m1));
    int a, b;
    for(int i = 0; i < m1; i++) {
        scanf("%d%d", &a, &b);
        graf[a].push_back(b);
        graf[b].push_back(a);
        if(std::min(a, b) == 1)
            connectedTo1[std::max(a, b)] = true;
        startEdges[i] = {std::min(a, b), std::max(a, b)};
    }

    scanf("%d", &(m2));
    for(int i = 0; i < m2; i++) {
        scanf("%d%d", &a, &b);
        graf2[a].push_back(b);
        graf2[b].push_back(a);
        if(std::min(a, b) == 1)
            connectedTo12[std::max(a, b)] = true;
        endEdges[i] = {std::min(a, b), std::max(a, b)};
    }
}

void calcEdgesToAddDelete() {
    std::sort(startEdges, startEdges + m1);
    std::sort(endEdges, endEdges + m2);
    int i = 0, j = 0;
    while(i < m1 && j < m2) {
        if(startEdges[i] == endEdges[j]) {
            i++;
            j++;
            continue;
        }
        if(startEdges[i] < endEdges[j]) {
            edgesToDelete.push_back(startEdges[i]);
            i++;
        }
        else {
            edgesToAdd.push_back(endEdges[j]);
            j++;
        }
    }
    while(i < m1) {
        edgesToDelete.push_back(startEdges[i]);
        i++;
    }
    while(j < m2) {
        edgesToAdd.push_back(endEdges[j]);
        j++;
    }
}

bool connected[maxn];
void resetConnected() {
    for(int i = 1; i <= n; i++)
        connected[i] = false;
}

void connectEverywhere(int V, bool actuallyAdd) {
    std::queue<int> Q;
    connected[V] = true;
    for(const auto& u : graf[V]) {
        connected[u] = true;
        Q.push(u);
    }
    while(!Q.empty()) {
        auto v = Q.front();
        Q.pop();
        for(const auto& u : graf[v]) {
            if(connected[u])
                continue;
            connected[u] = true;
            Q.push(u);
            kolejnoscDodawania.push_back(u);
            if(actuallyAdd)
                ans.push_back({1, {V, u}});
        }
    }
    resetConnected();
}

void disconnectEverywhere(int v) {
    for(int i = kolejnoscDodawania.size() - 1; i >= 0; i--) {
        int u = kolejnoscDodawania[i];
        if(connectedTo12[u])
            continue;
        ans.push_back({false, {v, u}});
    }
}

void processEdgesToAddDelete() {
    for(const auto& [a, b] : edgesToAdd) {
        if(std::min(a, b) == 1)
            continue;
        ans.push_back({true, {a, b}});
    }
    for(const auto& [a, b] : edgesToDelete) {
        if(std::min(a, b) == 1)
            continue;
        ans.push_back({false, {a, b}});
    }
}

void output() {
    printf("%zu\n", ans.size());
    for(const auto& [sign, ab] : ans) {
        const auto& [a, b] = ab;
        printf("%c %d %d\n", sign ? '+' : '-', a, b);
    }
}

int main() {
    input();
    calcEdgesToAddDelete();

    connectEverywhere(1, true);

    processEdgesToAddDelete();

    kolejnoscDodawania.clear();
    std::swap(graf, graf2);
    connectEverywhere(1, false);
    std::swap(graf, graf2);

    disconnectEverywhere(1);

    output();
    return 0;
}