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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#include <iostream>
#include <vector>
#include <queue>
#include <set>
#include <algorithm>
#include <sstream>
#include <string>

#define pb push_back

using namespace std;

vector<bool> v;
vector<vector<int>> start_friends;
vector<vector<int>> end_friends;
vector<int> d;
vector<int> add;
vector<int> rem;

void add_start_friendship(int a, int b) {
    start_friends[a].pb(b);
    start_friends[b].pb(a);
}

void add_end_friendship(int a, int b) {
    end_friends[a].pb(b);
    end_friends[b].pb(a);
}

void bfs(int best) {
    queue<int> q;
    q.push(best);
    v[best] = true;

    while (!q.empty()) {
        int f = q.front();
        q.pop();

        for (auto i = start_friends[f].begin(); i != start_friends[f].end(); i++) {
            if (!v[*i]) {
                add.pb(*i);
                q.push(*i);
                v[*i] = true;
            }
        }
    }
}

void reverse_bfs(int best) {
    queue<int> q;
    q.push(best);
    v[best] = true;

    while (!q.empty()) {
        int f = q.front();
        q.pop();

        for (auto i = end_friends[f].begin(); i != end_friends[f].end(); i++) {
            if (!v[*i]) {
                rem.pb(*i);
                q.push(*i);
                v[*i] = true;
            }
        }
    }
}

int main()
{
    int n, m;
    cin >> n >> m;
    stringstream output;
    int operations = 0;

    start_friends.resize(n + 1);
    end_friends.resize(n + 1);

    v.resize(n + 1);  // visited
    d.resize(n + 1);  // distance from best

    int a, b;

    for (int i = 0; i < m; i++) {
        cin >> a >> b;
        add_start_friendship(a, b);
    }

    cin >> m;

    for (int i = 0; i < m; i++) {
        cin >> a >> b;
        add_end_friendship(a, b);
    }

    for (int i = 1; i <= n; i++) {
        sort(start_friends[i].begin(), start_friends[i].end());
        sort(end_friends[i].begin(), end_friends[i].end());
    }

    int max_rank = 0;
    int best = -1;

    for (int i = 1; i <= n; i++) {
        if (max_rank <= start_friends[i].size()) {
            max_rank = start_friends[i].size();
            best = i;
        }
    }

    // add direct (bfs)
    bfs(best);
    for (int i = 0; i < add.size(); i++) {
        // not a friend in the start state, add
        if (find(start_friends[best].begin(), start_friends[best].end(), add[i]) == start_friends[best].end()) {
            output << "+ " << best << " " << add[i] << endl;
            operations++;
        }
    }

    vector<pair<int, int>> to_add;
    vector<pair<int, int>> to_remove;

    // construct missing friends
    for (int i = 1; i <= n; i++) {
        vector<int> to_remove_local;
        vector<int> to_add_local;
        set_difference(end_friends[i].begin(), end_friends[i].end(),
            start_friends[i].begin(), start_friends[i].end(), inserter(to_add_local, to_add_local.end()));
        set_difference(start_friends[i].begin(), start_friends[i].end(), 
            end_friends[i].begin(), end_friends[i].end(), inserter(to_remove_local, to_remove_local.end()));

        for (int j = 0; j < to_add_local.size(); j++) {
            if (i < to_add_local[j])
                to_add.pb(make_pair(i, to_add_local[j]));
        }

        for (int j = 0; j < to_remove_local.size(); j++) {
            if (i < to_remove_local[j])
            to_remove.pb(make_pair(i, to_remove_local[j]));
        }

    }

    // add indirect
    for (int i = 0; i < to_add.size(); i++) {
        if (to_add[i].first == best || to_add[i].second == best)
            continue; // best has already befriended everyone

        output << "+ " << to_add[i].first << " " << to_add[i].second << endl;
        operations++;
    }

    // remove indirect
    for (int i = 0; i < to_remove.size(); i++) {
        if (to_remove[i].first == best || to_remove[i].second == best)
            continue; // best unfriends last

        output << "- " << to_remove[i].first << " " << to_remove[i].second << endl;
        operations++;
    }

    // remove direct - reverse bfs on end state
    v.assign(v.size(), false);
    reverse_bfs(best);
    reverse(rem.begin(), rem.end());

    for (int i = 0; i < rem.size(); i++) {
        // not a friend in the end state, remove
        if (find(end_friends[best].begin(), end_friends[best].end(), rem[i]) == end_friends[best].end()) {
            output << "- " << best << " " << rem[i] << endl;
            operations++;
        }
    }

    cout << operations << endl;
    cout << output.str();
}