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
/**
 * Patryk Kisielewski
 * 
 * Potyczki Algorytmiczne 2024
 * Zadanie: ALC - Alchemik Bajtazar [B]
*/
#include <vector>
#include <utility>
#include <cstdio>
#include <iostream>
#include <sstream>

using namespace std;

vector<vector<bool>> edges1;
vector<vector<bool>> edges2;
vector<vector<int>> graph1;
vector<vector<int>> graph2;
vector<bool> visited;
int n, m;
ostringstream buf;

int counter = 0;

void build(int a) {
    if (visited[a]) {
        return;
    }
    visited[a] = true;
    if (!edges1[1][a]) {
        buf << "+ 1 " << a << '\n';
        ++counter;
        edges1[1][a] = true;
        graph1[1].push_back(a);
    }
    for (auto& b : graph1[a]) {
        build(b);
    }
}

int main() {

    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    vector<pair<int, int>> v(11, make_pair(0, 0));

    cin >> n;

    edges1 = vector<vector<bool>>(n+1, vector<bool>(n+1, false));
    edges2 = vector<vector<bool>>(n+1, vector<bool>(n+1, false));
    graph1 = vector<vector<int>>(n+1, vector<int>());
    graph2 = vector<vector<int>>(n+1, vector<int>());
    visited = vector<bool>(n+1, false);

    int a, b;

    cin >> m;

    for (int i = 0; i < m; ++i) {
        cin >> a >> b;
        if (a > b) swap(a, b);
        edges1[a][b] = true;
        graph1[a].push_back(b);
    }

    cin >> m;

    for (int i = 0; i < m; ++i) {
        cin >> a >> b;
        if (a > b) swap(a, b);
        edges2[a][b] = true;
        graph2[a].push_back(b);
    }

    visited[1] = true;

    build(graph1[1].front());

    for (int i = 1; i <= n; ++i) {
        for (auto& a : graph2[i]) {
            if (edges1[i][a] == false) {
                buf << "+ " << i << " " << a << '\n';
                ++counter;
                edges1[i][a] = true;
            }
        }
    }

    for (int i = 1; i <= n; ++i) {
        for (auto& a : graph1[i]) {
            if (edges2[i][a] == false) {
                buf << "- " << i << " " << a << '\n';
                ++counter;
            }
        }
    }

    cout << counter << '\n' << buf.str();

    return 0;
}