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
178
#include <bits/stdc++.h>

using namespace std;

int n, m1, m2;
pair<int, int> a;
vector<int> graf[30000];
set<pair<int, int>> poczatek;
set<pair<int, int>> doDodania, doNiczego, doUsuniecia;
stack<pair<int, int>> doUsunieciaS;
bitset<30000> odw;
queue<pair<char, pair<int, int>>> doWypisania;

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

    cin >> n >> m1;
    for (int i = 0; i < m1; i++)
    {
        cin >> a.first >> a.second;
        if (a.first > a.second) swap(a.first, a.second);
        a.first--;
        a.second--;
        poczatek.insert(a);
    }
    cin >> m2;
    for (int i = 0; i < m2; i++)
    {
        cin >> a.first >> a.second;
        if (a.first > a.second) swap(a.first, a.second);
        a.first--;
        a.second--;
        if (poczatek.find(a) == poczatek.end()) doDodania.insert(a);
        else doNiczego.insert(a);
    }
    for (auto para : poczatek)
    {
        if (doDodania.find(para) == doDodania.end() && doNiczego.find(para) == doNiczego.end())
            doUsuniecia.insert(para);
    }
/*
    cout << '\n';
    for (auto para : poczatek)
        cout << para.first << ' ' << para.second << '\n';
    cout << '\n';
    for (auto para : doDodania)
        cout << para.first << ' ' << para.second << '\n';
    cout << '\n';
    for (auto para : doNiczego)
        cout << para.first << ' ' << para.second << '\n';
    cout << '\n';
    for (auto para : doUsuniecia)
        cout << para.first << ' ' << para.second << '\n';
    cout << '\n';
*/
    for (auto para : poczatek)
    {
        graf[para.first].push_back(para.second);
        graf[para.second].push_back(para.first);
    }

    for (auto para : doDodania)
    {
        for (int i = 0; i < n; i++) odw[i] = 0;
        queue<pair<int, vector<int>>> q;
        q.push({ para.first, {} });
        odw[para.first] = true;
        pair<int, vector<int>> curr;
        while (true)
        {
            curr = q.front();
            if (curr.first == para.second) break;
            q.pop();
            curr.second.push_back(curr.first);
            for (int sasiad : graf[curr.first])
            {
                if (!odw[sasiad])
                    q.push({ sasiad, curr.second });
                odw[sasiad] = true;
            }
        }
        for (int i = 2; i < curr.second.size(); i++)
        {
            graf[para.first].push_back(curr.second[i]);
            graf[curr.second[i]].push_back(para.first);
            doUsunieciaS.push({ para.first, curr.second[i] });
            //cout << "+ " << para.first + 1 << ' ' << curr.second[i] + 1 << '\n';
            doWypisania.push({ '+', { para.first + 1, curr.second[i] + 1 } });
        }
        graf[para.first].push_back(para.second);
        graf[para.second].push_back(para.first);
        //cout << "+ " << para.first + 1 << ' ' << para.second + 1 << '\n';
        doWypisania.push({ '+', { para.first + 1, para.second + 1 } });
    }

    for (auto para : doUsuniecia)
    {
        for (int i = 0; i < n; i++) odw[i] = 0;
        queue<pair<int, vector<int>>> q;
        q.push({ para.first, {} });
        odw[para.first] = true;
        pair<int, vector<int>> curr;
        while (true)
        {
            curr = q.front();
            //cout << "visiting " << curr.first << '\n';
            if (curr.first == para.second) break;
            q.pop();
            curr.second.push_back(curr.first);
            for (int sasiad : graf[curr.first])
            {
                if (sasiad == para.first && curr.first == para.second || sasiad == para.second && curr.first == para.first)
                    continue;
                if (!odw[sasiad])
                    q.push({ sasiad, curr.second });
                odw[sasiad] = true;
            }
        }
        /*for (int i = 0; i < n; i++)
        {
            cout << i << ' ';
            for (int sasiad : graf[i])
            {
                cout << sasiad << ' ';
            }
            cout << '\n';
        }*/
        //cout << curr.second[0] << ' ' << curr.second[1] << ' ' << curr.second[2] << '\n';
        for (int i = 2; i < curr.second.size(); i++)
        {
            graf[para.first].push_back(curr.second[i]);
            graf[curr.second[i]].push_back(para.first);
            doUsunieciaS.push({ para.first, curr.second[i] });
            //cout << "+ " << para.first + 1 << ' ' << curr.second[i] + 1 << '\n';
            doWypisania.push({ '+', { para.first + 1, curr.second[i] + 1 } });
        }
        doUsunieciaS.push({ para.first, para.second });
    }

    while (!doUsunieciaS.empty())
    {
        auto usun = doUsunieciaS.top();
        doUsunieciaS.pop();
        //cout << "- " << usun.first + 1 << ' ' << usun.second + 1 << '\n';
        doWypisania.push({ '-', { usun.first + 1, usun.second + 1 } });
    }

    cout << doWypisania.size() << '\n';
    while (!doWypisania.empty())
    {
        auto wypisz = doWypisania.front();
        doWypisania.pop();
        cout << wypisz.first << ' ' << wypisz.second.first << ' ' << wypisz.second.second << '\n';
    }

    return 0;
}

/*
6
7
1 2
2 3
3 4
4 5
5 6
2 5
3 6
6
1 2
2 3
3 4
4 5
5 6
6 1
*/