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
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <sstream>

#define SS ostringstream

using namespace std;

void read_graph(int m, vector<vector<int>> &G, vector<bool> &conn)
{
    int a, b;
    while (m--)
    {
        cin >> a >> b;
        G[a].push_back(b);
        G[b].push_back(a);

        if (a == 1) conn[b] = true;
        else if (b == 1) conn[a] = true;
    }
}

void build_support(int v, const vector<vector<int>> &G, const vector<bool> &conn, vector<bool> &visited, int &moves, SS &out)
{
    visited[v] = true;

    if (!conn[v])
    {
        ++moves;
        out << "+ 1 " << v << "\n";
    }

    for (int u : G[v])
    {
        if (visited[u]) continue;
        build_support(u, G, conn, visited, moves, out);
    }
}

void remove_support(int v, const vector<vector<int>> &G, const vector<bool> &conn, vector<bool> &visited, int &moves, SS &out)
{
    visited[v] = true;

    for (int u : G[v])
    {
        if (visited[u]) continue;
        remove_support(u, G, conn, visited, moves, out);
    }

    if (!conn[v])
    {
        ++moves;
        out << "- 1 " << v << "\n";
    }
}

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

    int n, m;
    cin >> n >> m;

    vector<vector<int>> G(n + 1);
    vector<vector<int>> H(n + 1);
    vector<bool> before(n + 1);
    vector<bool> after(n + 1);

    before[1] = true;
    after[1] = true;

    read_graph(m, G, before);
    cin >> m;
    read_graph(m, H, after);

    int moves = 0;
    SS out;

    vector<bool> visited(n + 1);
    build_support(1, G, before, visited, moves, out);

    for (int v = 2; v <= n; ++v) sort(G[v].begin(), G[v].end());
    for (int v = 2; v <= n; ++v) sort(H[v].begin(), H[v].end());

    for (int v = 2; v < n; ++v)
    {
        auto itr = G[v].begin();
        auto jtr = H[v].begin();

        while (itr != G[v].end() && *itr < v) ++itr;
        while (jtr != H[v].end() && *jtr < v) ++jtr;

        while (itr != G[v].end() && jtr != H[v].end())
        {
            if (*itr < *jtr)
            {
                ++moves;
                out << "- " << v << " " << *(itr++) << "\n";
            }
            else if (*itr > *jtr)
            {
                ++moves;
                out << "+ " << v << " " << *(jtr++) << "\n";
            }
            else
            {
                ++itr;
                ++jtr;
            }
        }

        while (itr != G[v].end())
        {
            ++moves;
            out << "- " << v << " " << *(itr++) << "\n";
        }
        while (jtr != H[v].end())
        {
            ++moves;
            out << "+ " << v << " " << *(jtr++) << "\n";
        }
    }

    visited.assign(n + 1, false);
    remove_support(1, H, after, visited, moves, out);

    cout << moves << "\n" << out.str();
}