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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#include <bits/stdc++.h>

using namespace std;

#define ll long long

#define rng(i,a,b) for(int i=int(a);i<int(b);i++)
#define rep(i,b) rng(i,0,b)

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;

typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<vvl> vvvl;

typedef pair<int,int> ii;

template<class t> using vc=vector<t>;
template<class t> using vvc=vc<vc<t>>;

const int MOD = 998244353;

ll read(){
    ll i;
    cin>>i;
    return i;
}

vi readvi(int n,int off=0,int shift=0){
    vi v(n+shift);
    rep(i,shift)v[i]=0;
    rep(i,n)v[i+shift]=read()+off;
    return v;
}

void YesNo(bool condition, bool do_exit=true) {
    if (condition)
        cout << "Yes" << endl;
    else
        cout << "No" << endl;
    if (do_exit)
        exit(0);
}

vvi read_graph(int n) {
    vvi neighbors(n);
    int m;

    cin >> m;
    rep (i,m) {
        int u, v;
        cin >> u >> v;
        --u; --v;
        neighbors[u].push_back(v);
        neighbors[v].push_back(u);
    }
    return neighbors;
}

int pair_to_int(int i, int j) {
    if (i > j)
        swap(i,j);
    return 100'000*i + j;
}

unordered_set<int> neighbors_to_hash(vvi & neighbors) {
    unordered_set<int> hash;
    rep(i, neighbors.size())
        for (int j : neighbors[i])
            hash.insert(pair_to_int(i, j));
    return hash;
}

int main(void ) {
    ios::sync_with_stdio(false);
    cin.tie(NULL);

    int n;

    cin >> n;

    vvi s_neighbors = read_graph(n);
    vvi d_neighbors = read_graph(n);

    unordered_set<int> s_edges = neighbors_to_hash(s_neighbors);
    unordered_set<int> d_edges = neighbors_to_hash(d_neighbors);


    // faza 1: dodajemy pomocnicze kraw do korzenia <= 30.000

    vector<bool> visited(n, false);

    visited[0] = true;
    stack<int> s;
    s.push(0);

    vector<string> out;

    while (not s.empty()) {
        int v = s.top();
        s.pop();
        for (int x : s_neighbors[v])
            if (not visited[x]) {
                visited[x] = true;
                s.push(x);

                if (s_edges.find(pair_to_int(0, x)) == s_edges.end())
                    out.push_back("+ 1 " + to_string(x + 1) + "\n");
            }
    }

    // faza 2: dodajemy E_d - E_s (<= 50.000)

    rep(i,n)
        for (int j : d_neighbors[i])
            if (i < j)
                if (i != 0 and s_edges.find(pair_to_int(i, j)) == s_edges.end())
                    out.push_back("+ " + to_string(i+1) + " " + to_string(j+1) + "\n");

    // faza 3: usuwamy E_s - E_d, z wyjątkiem krawędzi do korzenia (0), <= 50.000

    rep(i,n)
        for (int j : s_neighbors[i])
            if (i < j and i != 0)
                if (i != 0 and d_edges.find(pair_to_int(i, j)) == d_edges.end())
                    out.push_back("- " + to_string(i+1) + " " + to_string(j+1) + "\n");

    // faza 4: usuwamy pomocnicze kraw do korzenia <= 30.000

    std::fill(visited.begin(), visited.end(), false);

    visited[0] = true;
    s.push(0);

    vector<ii> to_remove;

    while (not s.empty()) {
        int v = s.top();
        s.pop();
        for (int x : d_neighbors[v])
            if (not visited[x]) {
                visited[x] = true;
                s.push(x);

                if (d_edges.find(pair_to_int(0, x)) == d_edges.end())
                    to_remove.push_back({0, x});
            }
    }

    std::reverse(to_remove.begin(), to_remove.end());
    for (auto [i,j] : to_remove)
        out.push_back("- " + to_string(i+1) + " " + to_string(j + 1) + "\n");

    cout << out.size() << "\n";
    for (string str : out)
        cout << str;

    return 0;
}