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
// Author: Bartek Knapik

#include <cstdio>
#include <queue>
#include <set>
#include <unordered_set>

using namespace std;

const int MAX_N = 30000 + 9;

struct Edge
{
    int a,b;
    
    bool operator<(const Edge &oth) const {
        if (a != oth.a) return a < oth.a;
        return b < oth.b;
    }
};

int n, ms, md, a, b, n_aux;
set<Edge> edges_s, edges_d, edges_add, edges_rem;
set<int> adj_s[MAX_N], adj_d[MAX_N], aux_nodes;
deque<int> q;
deque<Edge> buffer;
int dist[MAX_N];

void bfs_s()
{
    for (int i = 0; i < n + 1; ++i) dist[i] = -1;
    
    q.push_back(1);
    
    dist[1] = 0;
    
    while (!q.empty())
    {
        int node = q.front();
        q.pop_front();
        
        for (auto it = adj_s[node].begin(); it != adj_s[node].end(); ++it)
        {
            if (dist[*it] != -1) continue;
            dist[*it] = dist[node] + 1;
            q.push_back(*it);
            if (dist[node] >= 1) aux_nodes.insert(*it);
        }
    }
}

void bfs_d(int isolated)
{
    for (int i = 0; i < n + 1; ++i) dist[i] = -1;
    
    q.push_back(isolated);
    
    dist[isolated] = 0;
    
    while (!q.empty())
    {
        int node = q.front();
        q.pop_front();
        
        for (auto it = adj_d[node].begin(); it != adj_d[node].end(); ++it)
        {
            if (dist[*it] != -1) continue;
            dist[*it] = dist[node] + 1;
            q.push_back(*it);
            if (dist[node] >= 1 && !edges_s.contains({ isolated, *it})) aux_nodes.insert(*it);
        }
    }
}

int get_isolated()
{
    unordered_set<int> candidates;
    for (int i = 1; i <= n; ++i) candidates.insert(i);
    
    for (auto it = edges_rem.begin(); it != edges_rem.end(); ++it)
    {
        candidates.erase(it->a);
        candidates.erase(it->b);
    }
    
    return candidates.size() ? *candidates.begin() : 1;
}

int _max(Edge e) { return e.a > e.b ? e.a : e.b; }

int main()
{
    scanf("%d", &n);
    scanf("%d", &ms);
    
    for (int i = 0; i < ms; ++i)
    {
        scanf("%d%d", &a, &b);
        if (a > b) { int tmp = a; a = b; b = tmp; }
        edges_s.insert({ a, b });
        adj_s[a].insert(b);
        adj_s[b].insert(a);
    }
    
    scanf("%d", &md);
    
    for (int i = 0; i < md; ++i)
    {
        scanf("%d%d", &a, &b);
        if (a > b) { int tmp = a; a = b; b = tmp; }
        edges_d.insert({ a, b });
        adj_d[a].insert(b);
        adj_d[b].insert(a);
    }
    
    for (auto it = edges_d.begin(); it != edges_d.end(); ++it)
        if (!edges_s.contains(*it)) edges_add.insert(*it);
    
    for (auto it = edges_s.begin(); it != edges_s.end(); ++it)
        if (!edges_d.contains(*it)) edges_rem.insert(*it);
    
    if (edges_add.size())
    {
        bfs_s();
        
        for (auto it = aux_nodes.begin(); it != aux_nodes.end(); ++it) buffer.push_back({1, *it});
        
        for (auto it = edges_add.begin(); it != edges_add.end(); ++it)
        {
            Edge to_add = *it;
            
            if (to_add.a != 1 && to_add.b != 1)
                buffer.push_back(to_add);
            else
                aux_nodes.erase(_max(to_add));
        }
        
        for (auto it = aux_nodes.rbegin(); it != aux_nodes.rend(); ++it) buffer.push_back({-1, -(*it)});
    }
    
    if (edges_rem.size())
    {
        int isolated = get_isolated();
        bfs_d(isolated);
        
        for (auto it = aux_nodes.begin(); it != aux_nodes.end(); ++it) buffer.push_back({isolated, *it});
        
        for (auto it = edges_rem.begin(); it != edges_rem.end(); ++it) buffer.push_back({-it->a, -it->b});
        
        for (auto it = aux_nodes.rbegin(); it != aux_nodes.rend(); ++it) buffer.push_back({-isolated, -(*it)});
    }
    
    printf("%d\n", buffer.size());
    while (!buffer.empty())
    {
        Edge to_print = buffer.front();
        buffer.pop_front();
        
        if (to_print.a > 0) printf("+ %d %d\n", to_print.a, to_print.b);
        else printf("- %d %d\n", -to_print.a, -to_print.b);
    }
    return 0;
}