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
// clang-format off
#include<bits/stdc++.h>
using namespace std;
using LL=long long;
#define FOR(i,l,r) for(auto i=(l);i<=(r);++i)
#define REP(i,n) FOR(i,0,(n)-1)
#define ssize(x) int(x.size())
template<class A,class B>auto&operator<<(ostream&o,pair<A,B>p){return o<<"("<<p.first<<", "<<p.second<<")";}
template<class T>auto operator<<(ostream&o,T x)->decltype(x.end(),o){o<<"{";int i=0;for(auto e:x)o<<(", ")+2*!i++<<e;return o<<"}";}
#ifdef DEBUG
#define debug(x...) cerr<<"["#x"]: ",[](auto...$){((cerr<<$<<"; "),...);}(x),cerr<<'\n'
#else
#define debug(...) {}
#endif
// clang-format on

#define ENCODE(a, b) (min(a, b) * nverts + max(a, b))
#define DECODEA(enc) ((enc) / nverts)
#define DECODEB(enc) ((enc) % nverts)


// Global variables
int nverts, dag_nverts = 0;
const int max_nverts = 30000;
vector<vector<int>> adj[2];
vector<bitset<max_nverts>> mat[2];
vector<bool> visited;

vector<pair<int, int>> ans[2];
unordered_set<int> not_in[2];


inline void
add_edge(const int& id, const int& a, const int& b)
{
    const int& enc = ENCODE(a, b);
    not_in[id].erase(enc);
    mat[id][a][b] = mat[id][b][a] = true;
    const int& other              = !(bool)id;
    if (!mat[other][a][b]) not_in[other].insert(enc);
}


void
make_star(const int& id, const int& start)
{
    vector<int> dq{start};
    while (!dq.empty()) {
        int v = dq.back();
        dq.pop_back();
        for (const auto& neigh : adj[id][v]) {
            if (visited[neigh]) continue;
            visited[neigh] = true;
            if (mat[id][0][neigh] == false) {
                ans[id].emplace_back(0, neigh);
                add_edge(id, 0, neigh);
            }
            dq.emplace_back(neigh);
        }
    }
}


int
main()
{
    cin.tie(0)->sync_with_stdio(0);
    cin >> nverts;
    REP (id, 2) adj[id].resize(nverts), mat[id].resize(nverts);
    REP (id, 2) {
        int nedges, a, b;
        cin >> nedges;
        while (nedges--) {
            cin >> a >> b;
            add_edge(id, --a, --b);
            adj[id][a].emplace_back(b);
            adj[id][b].emplace_back(a);
        }
    }

    // Create a star so that c always exists
    REP (id, 2) {
        visited    = vector<bool>(nverts);
        visited[0] = true;
        for (const auto& start : adj[id][0]) {
            if (visited[start]) continue;
            visited[start] = true;
            make_star(id, start);
        }
    }
    visited.clear();
    visited.shrink_to_fit();

    // Add more edges so that graphs are equal
    REP (id, 2) {
        for (const auto& enc : not_in[id]) {
            const int &a = DECODEA(enc), &b = DECODEB(enc);
            ans[id].emplace_back(a, b);
        }
    }

    cout << ssize(ans[0]) + ssize(ans[1]) << "\n";
    for (auto it = ans[0].begin(); it != ans[0].end(); ++it)
        cout << "+ " << it->first + 1 << " " << it->second + 1 << "\n";
    for (auto it = ans[1].rbegin(); it != ans[1].rend(); ++it)
        cout << "- " << it->first + 1 << " " << it->second + 1 << "\n";
    return 0;
}