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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ALL(x) (x).begin(), (x).end()
#define REP(i, n) for(int i = 0; i < n; i++)
#define VI vector<int>
#define PII pair<int,int>
#define SZ(x) (int)((x).size())
#define PB push_back
#define MP make_pair
#define st first
#define nd second

template<class C> void mini(C& _a4, C _b4) { _a4 = min(_a4, _b4); }
template<class C> void maxi(C& _a4, C _b4) { _a4 = max(_a4, _b4); }

template<class TH> void _dbg(const char *sdbg, TH h){ cerr<<sdbg<<'='<<h<<endl; }
template<class TH, class... TA> void _dbg(const char *sdbg, TH h, TA... a) {
  while(*sdbg!=',')cerr<<*sdbg++;cerr<<'='<<h<<','; _dbg(sdbg+1, a...);
}

template<class T> ostream& operator<<(ostream& os, vector<T> V) {
  os << "["; for (auto& vv : V) os << vv << ","; os << "]";
  return os;
}

#ifdef LOCAL
#define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
#else
#define debug(...) (__VA_ARGS__)
#define cerr if(0)cout
#endif

enum OperationType{
    ADD,
    REMOVE
};

struct Operation{
    OperationType type;
    int u, v, m;
};

struct BfsInfo{
    int node;
    int parent;
};

struct Graph{
    Graph(){}
    void read_graph(int n){
        this->n = n;
        int m;
        cin >> m;
        edges.resize(n);
        REP(i, m){
            int t1, t2;
            cin >> t1 >> t2;
            t1--;
            t2--;
            edges[t1].insert(t2);
            edges[t2].insert(t1);
            if(t1 > t2){
                swap(t1, t2);
            }
            all_edges.insert({t1, t2});
        }
    }
    int n;
    vector<set<int>> edges;
    set<PII> all_edges;
    vector<BfsInfo> bfs_order(int root){
        vector<BfsInfo> order = {{root, -1}};
        VI visited(n);
        visited[root] = 1;
        REP(i, SZ(order)){
            int u = order[i].node;
            for(int v: edges[u]){
                if(!visited[v]){
                    visited[v] = 1;
                    order.push_back({v, u});
                }
            }
        }
        return order;
    }
    bool edge_exists(int u, int v){
        return edges[u].count(v);

    }

    optional<Operation> add_edge(int u, int v, int m){
        debug("add", u, v, m, edge_exists(u, v));
        if(edge_exists(u, v)){
            return std::nullopt;
        }
        if(u > v){
            swap(u, v);
        }
        assert(edge_exists(u, m) && edge_exists(v, m));
        edges[u].insert(v);
        edges[v].insert(u);
        all_edges.insert({u, v});
        return Operation{ADD, u, v, m};
    }

    optional<Operation> remove_edge(int u, int v, int m){
        debug("remove", u, v, m, edge_exists(u, v));
        if(!edge_exists(u, v)){
            return std::nullopt;
        }
        if(u > v){
            swap(u, v);
        }
        assert(edge_exists(u, m) && edge_exists(v, m));
        edges[u].erase(v);
        edges[v].erase(u);
        all_edges.erase({u, v});
        return Operation{REMOVE, u, v, m};
    }
};

ostream & operator<<(ostream &os, const Operation &o){
    if(o.type == ADD){
        os << "+ " << o.u + 1 << " " << o.v + 1;
    }else{
        os << "- " << o.u + 1 << " " << o.v + 1;
    }
    return os;
}

ostream & operator<<(ostream &os, const BfsInfo &b){
    os << b.node << " " << b.parent;
    return os;
}
template<class C> ostream & operator<<(ostream &os, const set<C> &s){
    os << "{";
    for(auto a: s){
        os << a << ", ";
    }
    os << "}";
    return os;
}


ostream & operator<<(ostream &os, const Graph &g){
    os << "Graph(" << g.n << ")\n";
    REP(i, g.n){
        os << i << ": " << g.edges[i] << "\n";
    }
    return os;
}



int32_t main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int n;
    cin >> n;
    Graph g_current, g_target;
    g_current.read_graph(n);
    g_target.read_graph(n);
    // first create a central node;
    int root = 0;
    auto current_order = g_current.bfs_order(root);
    vector<Operation> all_operations;
    for(auto a: current_order){
        if(a.node == root || a.parent == root){
            continue;
        }
        auto r = g_current.add_edge(a.node, root, a.parent);
        if(r.has_value()){
            all_operations.push_back(r.value());
        }
    }
    // now we have a central node that is connected to all other nodes
    // first we add all edges that are in the target graph but not in the current graph
    auto v = g_target.all_edges;
    for(auto e: v){
        if(!g_current.edge_exists(e.st, e.nd)){
            auto r = g_current.add_edge(e.st, e.nd, root);
            if(r.has_value()){
                all_operations.push_back(r.value());
            }
        }
    }
    // now the edges are a superset of the target graph
    // we need to remove edges that are in the current graph but not in the target graph
    auto target_order = g_target.bfs_order(root);
    reverse(ALL(target_order));
    for(auto a: target_order){
        // debug(a);
        if(a.node == root){
            continue;
        }
        // first we remove all edges except the one to the root
        auto edges = g_current.edges[a.node];
        debug(a, edges);
        for(int v: edges){
            if(v != root && !g_target.edge_exists(a.node, v)){
                auto r = g_current.remove_edge(a.node, v, root);
                if(r.has_value()){
                    all_operations.push_back(r.value());
                }
            }
        }
        // now if the edge to the root is not in the target graph, we remove it
        
        if(!g_target.edge_exists(a.node, root)){
            auto r = g_current.remove_edge(a.node, root, a.parent);
            if(r.has_value()){
                all_operations.push_back(r.value());
            }
        }
    }
    // check if the graphs are equal
    assert(g_current.all_edges == g_target.all_edges);
    cout << SZ(all_operations) << "\n";
    for(auto o: all_operations){
        if(o.type == ADD){
            cout << "+ " << o.u + 1 << " " << o.v + 1;
        }else{
            cout << "- " << o.u + 1 << " " << o.v + 1;
        }
        cout << "\n";
    }
}