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
#include <bits/stdc++.h>

using namespace std;

vector<vector<int>> edges;
vector<vector<int>> cEdges;
vector<bool> visited;
vector<int> order;
set<pair<int, int>> edg, hasToExist;
vector<string> moves;
int origin;

void dfs(int cn){
    visited[cn] = 1;

    for(int node : edges[cn]){
        if(visited[node]) continue;
        if(edg.find({origin, node}) == edg.end() and edg.find({node, origin}) == edg.end()){
            moves.push_back("+ "+to_string(origin+1)+" "+to_string(node+1));
            edg.insert({origin, node});
            edges[node].push_back(origin);
            edges[origin].push_back(node);
        }
        dfs(node);
    }
}

void dfs2(int cn){
    visited[cn] = 1;

    for(int node : cEdges[cn]){
        if(visited[node]) continue;
        dfs2(node);
    }
    order.push_back(cn);
}

void addEdges(int n1, int n2){
    hasToExist.insert({n1, n2});
    if(edg.find({n1, n2}) != edg.end() or edg.find({n2, n1}) != edg.end()) return;
    moves.push_back("+ "+to_string(n1+1)+" "+to_string(n2+1));
    edg.insert({n1, n2});
    edges[n1].push_back(n2);
    edges[n2].push_back(n1);
}

void removeEdges(int cn){
    for(int node : edges[cn]){
        if(hasToExist.find({node, cn}) != hasToExist.end() or hasToExist.find({cn, node}) != hasToExist.end()){
            continue;
        }
        if(edg.find({node, cn})==edg.end() and edg.find({cn, node})==edg.end()){
            continue;
        }
        moves.push_back("- "+to_string(node+1)+" "+to_string(cn+1));
        edg.erase({node, cn});
        edg.erase({cn, node});
    }
}

int main(){
    cin.tie(0);
    cout.tie(0);
    ios_base::sync_with_stdio(0);
    int n, m1, m2;
    cin>>n;

    cin>>m1;

    int a, b;
    edges.assign(n, {});
    visited.assign(n, 0);
    for(int i = 0; i<m1; i++){
        cin>>a>>b;
        a--; b--;
        edg.insert({a, b});
        edges[a].push_back(b);
        edges[b].push_back(a);
    }
    origin = 0;

    dfs(origin);
    visited.assign(n, 0);
    cEdges.assign(n, {});
    cin>>m2;
    for(int i = 0; i<m2; i++){
        cin>>a>>b;
        a--; b--;
        cEdges[a].push_back(b);
        cEdges[b].push_back(a);
        addEdges(a, b);
    }

    dfs2(origin);

    for(int i = 0; i<n; i++){
        sort(edges[i].rbegin(), edges[i].rend());
    }

    for(int node : order){
        removeEdges(node);
    }

    cout<<moves.size()<<"\n";
    for(string move : moves){
        cout<<move<<"\n";
    }
    return 0;
}