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
#include <bits/stdc++.h>
#define fi first
#define sc second
#define pb push_back
#define rep(i,p,k) for(int i=(p);i<(k);++i)
#define forn(i,p,k) for(int i=(p);i<=(k);++i)
#define forr(i,k,p) for(int i=(k)-1;i>=(p);--i)
#define each(a,b) for(auto&a:b)
#define all(v)  begin(v), end(v)
#define RET(smth)   return void(cout<<(smth)<<'\n');
#define sz(v) (int)v.size()
using namespace std;
using pii = pair<int,int>;
using ll = long long;
using Vi = vector<int>;
#ifdef DEBUG
#include "debug.h"
#else
#define debug(...) ;
#endif


int main() {
#ifndef DEBUG
    cin.tie(0) -> sync_with_stdio(0);
#endif
    int n; cin >> n;
    vector<Vi> G(n);
    int m; cin >> m;
    while(m--) {
        int a,b; cin >> a >> b; --a; --b;
        G[a].push_back(b);
        G[b].push_back(a);
    }
    vector<pii> ans;
    Vi vis(n);
    {
        auto dfs = [&](int w, auto && self) -> void {
            vis[w] = 1;
            for(auto v : G[w]) if(!vis[v]) {
                ans.emplace_back(0,v);
                self(v,self);
            }
        };
        vis[0] = 1;
        for(auto v : G[0]) vis[v] = 1;
        for(auto v : G[0]) dfs(v, dfs);
    }
    rep(w,1,n) for(auto v : G[w]) if(w < v && w) ans.emplace_back(-w,-v);
    rep(i,0,n) G[i].clear();
    cin >> m;
    while(m--) {
        int a,b; cin >> a >> b; --a; --b;
        if(a && b)  ans.emplace_back(a,b);
        G[a].push_back(b);
        G[b].push_back(a);
    }
    Vi n0(n);
    n0[0] = 1;
    for(auto v : G[0]) n0[v] = 1;
    fill(all(vis),0);
    auto dfs = [&](int w, int p, auto && self) -> void {
        vis[w] = 1;
        for(auto v : G[w]) if(!vis[v]) {
            self(v,w,self);
        }
        if(!n0[w]) ans.emplace_back(0,-w);
    };
    dfs(0,0,dfs);
    cout << ans.size() << '\n';
    for(auto [a,b] : ans) {
        cout << (a >= 0 && b >= 0 ? '+' : '-');
        cout << ' ' << abs(a) + 1 << ' ' << abs(b) + 1 << '\n';
    }
    return 0;
}