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
// Karol Kosinski 2024
#include <bits/stdc++.h>
#define FOR(i,a,b) for(int i=(a),_b=(b);i<_b;++i)
#define FR_(i,a,b) for(int i=(a),_b=(b);i<=_b;++i)
#define FD_(i,b,a) for(int i=(b),_a=(a);i>=_a;--i)
#define ALL(c) (c).begin(),(c).end()
#define SIZE(c) int((c).size())
#define X first
#define Y second
#define endl '\n'
#define NAM(x) #x,'=',x
using namespace std;
using LL = long long;
using ULL = unsigned long long;
using PII = pair<int, int>;
using TIII = tuple<int, int, int>;
template<class...T> void _cout(T...a){(cout<<...<<a);}

#ifndef ENABLE_DEBUG
#define DEB(k,p,f,x...)
#else
#define DEB(k,p,f,x...) {if(k)_cout("------",setw(4),__LINE__," : ",__FUNCTION__,endl);if(p)f(x);}
#endif
#define DEBF(f,x...)    DEB(1,1,f,x)
#define DEBL            DEBF(void,0)
#define DEBC(p,x...)    DEB(0,p,_cout,x)
#define DEBUG(x...)     DEBC(1,x)

constexpr int NX = 30'005;
constexpr int MX = 50'005;
constexpr int RX = 200'005;

vector<int> GA[NX], GB[NX];
PII A[ MX * 2 ], B[ MX * 2 ], RA[RX], RB[RX];
int n, ma, mb, ran, rbn, D[NX];

void bfs(int s, vector<int> *G, PII* Res, int& r, PII* C, int& mc)
{
    queue<int> Q;
    FR_(i,1,n) D[i] = -1;
    D[s] = 0;
    Q.push(s);
    while ( not Q.empty() )
    {
        int u = Q.front(); Q.pop();
        for ( int v : G[u] ) if ( D[v] == -1 )
        {
            D[v] = D[u] + 1;
            DEBUG(NAM(v), " --> ", NAM(D[v]), endl);
            if ( D[v] > 1 )
            {
                Res[ r ++ ] = PII{ s, v };
                C[ mc ++ ] = PII{ s, v };
            }
            Q.push(v);
        }
    }
}

void merge()
{
    int ia = 0, ib = 0;
    while ( ia < ma and ib < mb )
    {
        if ( A[ia] < B[ib] )
        {
            RA[ ran ++ ] = A[ia];
            ++ ia;
        }
        else if ( A[ia] > B[ib] )
        {
            RB[ rbn ++ ] = B[ib];
            ++ ib;
        }
        else
        {
            ++ ia, ++ ib;
        }
    }
    while ( ia < ma )
    {
        RA[ ran ++ ] = A[ia];
        ++ ia;
    }
    while ( ib < mb )
    {
        RB[ rbn ++ ] = B[ib];
        ++ ib;
    }
}

void testcase()
{
    cin >> n >> ma;
    FOR(i,0,ma)
    {
        int a1, a2;
        cin >> a1 >> a2;
        GA[a1].push_back(a2);
        GA[a2].push_back(a1);
        if ( a1 > a2 ) swap(a1, a2);
        A[i] = PII{ a1, a2 };
    }
    cin >> mb;
    FOR(i,0,mb)
    {
        int a1, a2;
        cin >> a1 >> a2;
        GB[a1].push_back(a2);
        GB[a2].push_back(a1);
        if ( a1 > a2 ) swap(a1, a2);
        B[i] = PII{ a1, a2 };
    }
    DEBL;
    bfs(1, GA, RB, rbn, A, ma);
    DEBL;
    bfs(1, GB, RA, ran, B, mb);
    DEBL;
    sort( A, A + ma );
    sort( B, B + mb );
    merge();
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    testcase();
    cout << ran + rbn << endl;
    FOR(i,0,rbn) cout << "+ " << RB[i].X << ' ' << RB[i].Y << endl;
    FD_(i,ran-1,0) cout << "- " << RA[i].X << ' ' << RA[i].Y << endl;
    return 0;
}