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
179
180
181
182
183
184
185
// Alchemik.cpp : This file contains the 'main' function. Program execution begins and ends there.
//

#include <iostream>
#include <set>
#include <bits/stdc++.h>
#include <algorithm>
#include <vector>
#include <queue>
#include <map>
using namespace std;

struct pairGreater
{
    bool operator()(const pair<int, int>& p1, const  pair<int, int>& p2) const
    {
        return p1.first == p2.first ? p1.second > p2.second : p1.first > p2.first ;
    }
};


//dodane krawedzie
//uwaga po tym wektor wierzcholkow bedzie bezuzyteczny
vector<pair<int, int>> pathConnect(vector<int>& wierzcholki, map<int, vector<int>>& edges, set<pair<int,int>, pairGreater>& toAdd, set<pair<int, int >, pairGreater> &toRemove)
{
    vector<int> buffer;
    buffer.reserve(wierzcholki.size());
    vector<pair<int, int>> toReturn;
    toReturn.reserve(2 * wierzcholki.size());
    while (wierzcholki.size() > 2)
    {
        buffer.push_back(wierzcholki[0]);
        for (int i = 2; i < wierzcholki.size()-1 ; i += 2)
        {
            toReturn.push_back(pair<int, int>(wierzcholki[i - 2], wierzcholki[i]));
            edges[wierzcholki[i - 2]].push_back(wierzcholki[i]);
            edges[wierzcholki[i]].push_back(wierzcholki[i-2]);
            pair<int, int> np(min(wierzcholki[i - 2], wierzcholki[i - 1]), max(wierzcholki[i - 2], wierzcholki[i - 1]));
            if (toAdd.find(np) == toAdd.end())
            {
                toRemove.insert(np);
            }
            buffer.push_back(wierzcholki[i]);
        }
        buffer.push_back(wierzcholki[wierzcholki.size() - 1]);
        if (wierzcholki.size() & 1)
        {
            toReturn.push_back(pair<int, int>(buffer[buffer.size()-2], buffer[buffer.size()-1]));
            int a = buffer[buffer.size() - 2];
            int b = buffer[buffer.size() - 1];
            edges[a].push_back(b);
            edges[b].push_back(a);
        }
        wierzcholki = buffer;
        buffer.clear();
    }
    return toReturn;
}
void print(vector<pair<int, int>> p)
{
    for (auto& pair : p)
    {
        cout << "+ " << pair.first << " " << pair.second<<"\n";
    }
}
int bfscolor = 0;
vector<int> lasts;
vector<int> generatePath(int node1, int target)
{
    vector<int> path;
    while (node1 != target)
    {
        path.push_back(node1);
        node1 = lasts[node1];
    }
    path.push_back(target);
    return path;
}
struct Info
{
    int last;
    int color;
};
vector<pair<int, int>> specialBFS(int node1, int node2, vector<int>& color, map<int, vector<int>>& edges, set<pair<int, int>, pairGreater>& toAdd, set<pair<int, int >, pairGreater>& toRemove)
{
    bfscolor+=2;
    color[node1] = bfscolor;
    color[node2] = bfscolor+1;
    queue<int> kolejka;
    kolejka.push(node1);
    kolejka.push(node2);
    vector<int> fetch;
    while (!kolejka.empty())
    {
        int top = kolejka.front();
        kolejka.pop();
        fetch = edges[top];
        for (auto& node : fetch)
        {
            if (abs(color[node] - color[top]) == 1 && color[node] >= bfscolor && color[top] >= bfscolor)
            {
                vector<int> left;
                vector<int> right;
                //znaleziono
                if (color[top] == bfscolor)
                {
                    //top przyszzedl od node1
                    left = generatePath(top, node1);
                    right = generatePath(node, node2);
                }
                else
                {
                    left = generatePath(top, node2);
                    right = generatePath(node, node1);
                }
                reverse(right.begin(), right.end());
                right.insert(right.end(), left.begin(), left.end());
                return pathConnect(right, edges, toAdd, toRemove);
            }
            if (color[node] == bfscolor || color[node] == bfscolor+1)
                continue;
            lasts[node] = top;
            color[node] = color[top];
            kolejka.push(node);
        }

    }
    return vector<pair<int,int>>();
}


int main()
{
    int n, k1, k2;
    set<pair<int, int>, pairGreater> A;
    set<pair<int, int>, pairGreater> B;
    cin >> n >> k1;
    map<int, vector<int>> edges;
    vector<int> colors(n+1, 0);
    lasts = vector<int>(n+1, -1);
    int arg1, arg2;
    pair<int, int> krawedz;
    for (int i = 0; i < k1; ++i)
    {
        cin >> arg1 >> arg2;
        if (arg1 < arg2)
            A.insert(pair<int, int>(arg1, arg2));
        else 
            A.insert(pair<int, int>(arg2, arg1));
        edges[arg1].push_back(arg2);
        edges[arg2].push_back(arg1);
    }
    cin >> k2;
    for (int i = 0; i < k2; ++i)
    {
        cin >> arg1 >> arg2;
        if (arg1 < arg2)
            B.insert(pair<int, int>(arg1, arg2));
        else
            B.insert(pair<int, int>(arg2, arg1));
    }
    
    
    //to co zostanie w A - do wyrzucenia, to co zostanie w B - do zbudowania

    set<pair<int, int>, pairGreater> toRemove;
    set<pair<int, int>, pairGreater> toAdd;
    set_difference(A.begin(), A.end(), B.begin(), B.end(), inserter(toRemove, toRemove.begin()), pairGreater());
    set_difference(B.begin(), B.end(), A.begin(), A.end(), inserter(toAdd, toAdd.begin()), pairGreater());
 
    for (auto& i : toAdd)
    {
        print(specialBFS(i.first, i.second, colors, edges, toAdd, toRemove));
    }
   
    for (auto& i : toRemove)
    {
        cout << "- " << i.first << " " << i.second << endl;
    }
  
    //int node1, node2;
    //cin >> node1 >> node2;
    //print(specialBFS(node1, node2, colors, edges));
   
}