#include <bits/stdc++.h>
using namespace std;
vector<vector<int>> A, B;
int n;
vector<vector<int>> ruchy;
set<pair<int,int>> mA, mB;
vector<bool> odw;
void dfsLacz(int x, vector<vector<int>> &v, int d, bool dodaj) {
if(d >= 2 && dodaj && mA.count({0,x}) <= 0) ruchy.push_back({0, x, dodaj});
odw[x] = true;
for(auto &sas : v[x]) {
if(!odw[sas]) dfsLacz(sas, v, d+1, dodaj);
}
if(d >= 2 && !dodaj && mB.count({0,x}) <= 0) ruchy.push_back({0, x, dodaj});
}
void startDfs(vector<vector<int>> &v, bool dodaj) {
odw = vector<bool>(n);
dfsLacz(0, v, 0, dodaj);
}
int main(){
std::ios_base::sync_with_stdio(false);
std::cin.tie(nullptr);
cin>>n;
A = vector<vector<int>>(n);
B = vector<vector<int>>(n);
int m1,m2,a,b;
cin>>m1;
for(int i=0;i<m1;i++){
cin>>a>>b;
A[a-1].push_back(b-1);
A[b-1].push_back(a-1);
mA.insert({min(a-1,b-1), max(b-1,a-1)});
}
cin>>m2;
for(int i=0;i<m2;i++){
cin>>a>>b;
B[a-1].push_back(b-1);
B[b-1].push_back(a-1);
mB.insert({min(a-1,b-1), max(b-1,a-1)});
}
// Lacze 1 i wszystkie w A
startDfs(A, true);
// dodaje brakujace
for(auto &powinno : mB) {
if(powinno.first == 0 || powinno.second == 0) continue;
if(mA.count(powinno) > 0) continue;
ruchy.push_back({powinno.first, powinno.second, true});
}
// usuwam niepotrzebne
for(auto &powinno : mA) {
if(powinno.first == 0 || powinno.second == 0) continue;
if(mB.count(powinno) > 0) continue;
ruchy.push_back({powinno.first, powinno.second, false});
}
// Usuwam 1 i niepotrzebne w B
startDfs(B, false);
cout<<ruchy.size()<<"\n";
for(auto &r : ruchy) cout << (r[2] ? '+' : '-') << " " << (r[0]+1) << " " << (r[1]+1) << "\n";
}
/*
3
2
1 2
2 3
3
1 2
2 3
1 3
3
3
1 2
2 3
1 3
3
1 2
2 3
1 3
4
3
1 2
2 3
3 4
3
1 2
2 3
2 4
4
4
1 2
2 3
3 4
1 4
3
1 2
2 3
2 4
4
3
1 2
2 3
3 4
3
4 1
1 2
2 3
*/
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 | #include <bits/stdc++.h> using namespace std; vector<vector<int>> A, B; int n; vector<vector<int>> ruchy; set<pair<int,int>> mA, mB; vector<bool> odw; void dfsLacz(int x, vector<vector<int>> &v, int d, bool dodaj) { if(d >= 2 && dodaj && mA.count({0,x}) <= 0) ruchy.push_back({0, x, dodaj}); odw[x] = true; for(auto &sas : v[x]) { if(!odw[sas]) dfsLacz(sas, v, d+1, dodaj); } if(d >= 2 && !dodaj && mB.count({0,x}) <= 0) ruchy.push_back({0, x, dodaj}); } void startDfs(vector<vector<int>> &v, bool dodaj) { odw = vector<bool>(n); dfsLacz(0, v, 0, dodaj); } int main(){ std::ios_base::sync_with_stdio(false); std::cin.tie(nullptr); cin>>n; A = vector<vector<int>>(n); B = vector<vector<int>>(n); int m1,m2,a,b; cin>>m1; for(int i=0;i<m1;i++){ cin>>a>>b; A[a-1].push_back(b-1); A[b-1].push_back(a-1); mA.insert({min(a-1,b-1), max(b-1,a-1)}); } cin>>m2; for(int i=0;i<m2;i++){ cin>>a>>b; B[a-1].push_back(b-1); B[b-1].push_back(a-1); mB.insert({min(a-1,b-1), max(b-1,a-1)}); } // Lacze 1 i wszystkie w A startDfs(A, true); // dodaje brakujace for(auto &powinno : mB) { if(powinno.first == 0 || powinno.second == 0) continue; if(mA.count(powinno) > 0) continue; ruchy.push_back({powinno.first, powinno.second, true}); } // usuwam niepotrzebne for(auto &powinno : mA) { if(powinno.first == 0 || powinno.second == 0) continue; if(mB.count(powinno) > 0) continue; ruchy.push_back({powinno.first, powinno.second, false}); } // Usuwam 1 i niepotrzebne w B startDfs(B, false); cout<<ruchy.size()<<"\n"; for(auto &r : ruchy) cout << (r[2] ? '+' : '-') << " " << (r[0]+1) << " " << (r[1]+1) << "\n"; } /* 3 2 1 2 2 3 3 1 2 2 3 1 3 3 3 1 2 2 3 1 3 3 1 2 2 3 1 3 4 3 1 2 2 3 3 4 3 1 2 2 3 2 4 4 4 1 2 2 3 3 4 1 4 3 1 2 2 3 2 4 4 3 1 2 2 3 3 4 3 4 1 1 2 2 3 */ |
English