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

int n,ms,md;
int dist[30003];
int order[30003];

unordered_map<int, unordered_set<int>> src;
unordered_map<int, unordered_set<int>> dest;

void connect(unordered_map<int, unordered_set<int>> &m, int a, int b) {
    auto it = m.find(a);
    if(it != m.end()) {
        it->second.insert(b);
    } else {
        unordered_set<int> s;
        s.insert(b);
        m[a] = s;
    }

    it = m.find(b);
    if(it != m.end()) {
        it->second.insert(a);
    } else {
        unordered_set<int> s;
        s.insert(a);
        m[b] = s;
    }
}

bool srt(int a, int b) {
    return dist[a] < dist[b];
}

void orderByDistance(unordered_map<int, unordered_set<int>> &m) {
    for(int i=0;i<=n;i++) {
        dist[i] = -1;
        order[i] = i+1;
    }
    list<int> stck;
    dist[1] = 0;
    stck.push_back(1);
    while(stck.size() > 0) {
        int nr = stck.back();
        stck.pop_back();
        auto &it = m.find(nr)->second;
        for(auto &nbr: it) {
            if(dist[nbr] == -1) {
                dist[nbr] = dist[nr]+1;
                stck.push_back(nbr);
            }
        }
    }
    sort(order, order+n, srt);
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    stringstream ss;
    int ssSize = 0;
    int a, b;

    cin >> n;
    cin >> ms;
    for(int i=0;i<ms;i++) {
        cin >> a >> b;
        connect(src, a, b);
    }
    cin >> md;
    for(int i=0;i<md;i++) {
        cin >> a >> b;
        connect(dest, a, b);
    }
    orderByDistance(src);
    auto &oneNeighbours = src.find(1)->second;
    for(int i=0;i<n;i++) {
        int nr = order[i];
        if(nr == 1) continue;
        if(oneNeighbours.find(nr) == oneNeighbours.end()) {
            ss << "+ 1 " << nr << "\n";
            ssSize++;
        }
    }

    for (auto &p: src) {
        int nr = p.first;
        if(nr == 1) continue;
        auto &destSet = dest.find(nr)->second;
        for(auto &nbr: p.second) {
            if(nbr == 1) continue;
            if(nbr > nr) {
                if(destSet.find(nbr) == destSet.end()) {
                    ss << "- " << nr << " " << nbr << "\n";
                    ssSize++;
                }
            }
        }
    }

    for (auto &p: dest) {
        int nr = p.first;
        if(nr == 1) continue;
        auto &srcSet = src.find(nr)->second;
        for(auto &nbr: p.second) {
            if(nbr == 1) continue;
            if(nbr > nr) {
                if(srcSet.find(nbr) == srcSet.end()) {
                    ss << "+ " << nr << " " << nbr << "\n";
                    ssSize++;
                }
            }
        }
    }

    orderByDistance(dest);
    oneNeighbours = dest.find(1)->second;
    for(int i=n-1;i>=0;i--) {
        int nr = order[i];
        if(nr == 1) continue;
        if(oneNeighbours.find(nr) == oneNeighbours.end()) {
            ss << "- 1 " << nr << "\n";
            ssSize++;
        }
    }

    cout << ssSize << "\n" << ss.str();
    return 0;
}