#include<bits/stdc++.h>
using namespace std;
vector<vector<int>>g;
vector<vector<int>>g2;
set<pair<int, int>>s;
set<pair<int, int>>odp;
vector<bool>odw;
vector<bool>odw2;
queue<int>add;
queue<int>odjac;
void dfs(int x)
{
odw[x]=1;
if(x!=1&&s.find({1, x})==s.end())
{
add.push(1);
add.push(x);
s.insert({1, x});
}
for(auto e:g[x])
{
if(!odw[e])
dfs(e);
}
}
void dfsdel(int x)
{
odw2[x]=1;
for(auto e:g2[x])
{
if(!odw2[e])
dfsdel(e);
}
if(x!=1&&odp.find({1, x})==odp.end())
{
odjac.push(1);
odjac.push(x);
}
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n, m1, m2;
cin>>n>>m1;
g.resize(n+1);
g2.resize(n+1);
odw.resize(n+1, 0);
odw2.resize(n+1, 0);
int a, b;
for(int i=0; i<m1; i++)
{
cin>>a>>b;
g[a].push_back(b);
g[b].push_back(a);
if(b<a)
swap(a, b);
s.insert({a, b});
//s.insert({b, a});
}
cin>>m2;
for(int i=0; i<m2; i++)
{
cin>>a>>b;
g2[a].push_back(b);
g2[b].push_back(a);
if(b<a)
swap(a, b);
odp.insert({a, b});
}
dfs(1);
for(auto x:odp)
{
if(x.first!=1&&s.find(x)==s.end())
{
add.push(x.first);
add.push(x.second);
}
}
for(auto x:s)
{
if(x.first!=1&&odp.find(x)==odp.end())
{
odjac.push(x.first);
odjac.push(x.second);
}
}
dfsdel(1);
cout<<(add.size()+odjac.size())/2<<"\n";
while(!add.empty())
{
a=add.front();
add.pop();
b=add.front();
add.pop();
cout<<"+ "<<a<<" "<<b<<"\n";
}
while(!odjac.empty())
{
a=odjac.front();
odjac.pop();
b=odjac.front();
odjac.pop();
cout<<"- "<<a<<" "<<b<<"\n";
}
}
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 | #include<bits/stdc++.h> using namespace std; vector<vector<int>>g; vector<vector<int>>g2; set<pair<int, int>>s; set<pair<int, int>>odp; vector<bool>odw; vector<bool>odw2; queue<int>add; queue<int>odjac; void dfs(int x) { odw[x]=1; if(x!=1&&s.find({1, x})==s.end()) { add.push(1); add.push(x); s.insert({1, x}); } for(auto e:g[x]) { if(!odw[e]) dfs(e); } } void dfsdel(int x) { odw2[x]=1; for(auto e:g2[x]) { if(!odw2[e]) dfsdel(e); } if(x!=1&&odp.find({1, x})==odp.end()) { odjac.push(1); odjac.push(x); } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, m1, m2; cin>>n>>m1; g.resize(n+1); g2.resize(n+1); odw.resize(n+1, 0); odw2.resize(n+1, 0); int a, b; for(int i=0; i<m1; i++) { cin>>a>>b; g[a].push_back(b); g[b].push_back(a); if(b<a) swap(a, b); s.insert({a, b}); //s.insert({b, a}); } cin>>m2; for(int i=0; i<m2; i++) { cin>>a>>b; g2[a].push_back(b); g2[b].push_back(a); if(b<a) swap(a, b); odp.insert({a, b}); } dfs(1); for(auto x:odp) { if(x.first!=1&&s.find(x)==s.end()) { add.push(x.first); add.push(x.second); } } for(auto x:s) { if(x.first!=1&&odp.find(x)==odp.end()) { odjac.push(x.first); odjac.push(x.second); } } dfsdel(1); cout<<(add.size()+odjac.size())/2<<"\n"; while(!add.empty()) { a=add.front(); add.pop(); b=add.front(); add.pop(); cout<<"+ "<<a<<" "<<b<<"\n"; } while(!odjac.empty()) { a=odjac.front(); odjac.pop(); b=odjac.front(); odjac.pop(); cout<<"- "<<a<<" "<<b<<"\n"; } } |
English