#include<bits/stdc++.h>
using namespace std;
constexpr int N = 30007;
vector<int> wynikowy[N];
vector<int> wstepny[N];
vector<int> preorder;
vector<int> postorder;
vector<pair<int,int>> wstV;
vector<pair<int,int>> wynV;
int n,m1,m2;
int a,b;
bool odw[N];
void dfsbud(int x)
{
for(int y:wstepny[x])
{
if(odw[y])
continue;
odw[y]=1;
preorder.push_back(y);
dfsbud(y);
}
}
void dfsruj(int x)
{
for(int y:wynikowy[x])
{
if(odw[y])
continue;
odw[y]=1;
dfsruj(y);
postorder.push_back(y);
}
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n;
cin >> m1;
for(int i=0;i<m1;i++)
{
cin >> a >> b;
wstepny[a].push_back(b);
wstepny[b].push_back(a);
wstV.push_back({min(a,b),max(a,b)});
}
cin >> m2;
for(int i=0;i<m2;i++)
{
cin >> a >> b;
wynikowy[a].push_back(b);
wynikowy[b].push_back(a);
wynV.push_back({min(a,b),max(a,b)});
}
odw[1]=1;
for(int y:wstepny[1])
odw[y]=1;
for(int y:wstepny[1])
{
dfsbud(y);
}
for(int i=0;i<=n;i++)
odw[i]=0;
odw[1]=1;
for(int y:wynikowy[1])
odw[y]=1;
for(int y:wynikowy[1])
{
dfsruj(y);
}
cout << m1+m2+n-1+n-1-wstepny[1].size()-wynikowy[1].size()-wstepny[1].size()-wynikowy[1].size() << "\n";
for(auto x:preorder)
{
cout << "+ 1 " << x << "\n";
}
for(auto x:wstV)
{
if(x.first==1)
continue;
cout << "- " << x.first << " " << x.second << "\n";
}
for(auto x:wynV)
{
if(x.first==1)
continue;
cout << "+ " << x.first << " " << x.second << "\n";
}
for(auto x:postorder)
{
cout << "- 1 " << x << "\n";
}
return 0;
}
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 | #include<bits/stdc++.h> using namespace std; constexpr int N = 30007; vector<int> wynikowy[N]; vector<int> wstepny[N]; vector<int> preorder; vector<int> postorder; vector<pair<int,int>> wstV; vector<pair<int,int>> wynV; int n,m1,m2; int a,b; bool odw[N]; void dfsbud(int x) { for(int y:wstepny[x]) { if(odw[y]) continue; odw[y]=1; preorder.push_back(y); dfsbud(y); } } void dfsruj(int x) { for(int y:wynikowy[x]) { if(odw[y]) continue; odw[y]=1; dfsruj(y); postorder.push_back(y); } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n; cin >> m1; for(int i=0;i<m1;i++) { cin >> a >> b; wstepny[a].push_back(b); wstepny[b].push_back(a); wstV.push_back({min(a,b),max(a,b)}); } cin >> m2; for(int i=0;i<m2;i++) { cin >> a >> b; wynikowy[a].push_back(b); wynikowy[b].push_back(a); wynV.push_back({min(a,b),max(a,b)}); } odw[1]=1; for(int y:wstepny[1]) odw[y]=1; for(int y:wstepny[1]) { dfsbud(y); } for(int i=0;i<=n;i++) odw[i]=0; odw[1]=1; for(int y:wynikowy[1]) odw[y]=1; for(int y:wynikowy[1]) { dfsruj(y); } cout << m1+m2+n-1+n-1-wstepny[1].size()-wynikowy[1].size()-wstepny[1].size()-wynikowy[1].size() << "\n"; for(auto x:preorder) { cout << "+ 1 " << x << "\n"; } for(auto x:wstV) { if(x.first==1) continue; cout << "- " << x.first << " " << x.second << "\n"; } for(auto x:wynV) { if(x.first==1) continue; cout << "+ " << x.first << " " << x.second << "\n"; } for(auto x:postorder) { cout << "- 1 " << x << "\n"; } return 0; } |
English