#include <bits/stdc++.h>
using namespace std;
int main()
{
int n,m1;
cin>>n;
cin>>m1;
int a,b;
vector<vector<int>>s(n+1),s1(n+1);
vector<pair<int,pair<int,int>>>wyn;
map<pair<int,int>,int>mp,mp1;
vector<pair<int,int>>ls;
for(int i=0; i<m1; i++)
{
cin>>a>>b;
s[a].push_back(b);
s[b].push_back(a);
mp[{min(a,b),max(a,b)}]=1;
ls.push_back({min(a,b),max(a,b)});
}
int m2;
cin>>m2;
vector<pair<int,int>>ls1;
for(int i=0; i<m2; i++)
{
cin>>a>>b;
s1[a].push_back(b);
s1[b].push_back(a);
mp1[{min(a,b),max(a,b)}]=1;
ls1.push_back({min(a,b),max(a,b)});
}
queue<int>q;
vector<int>o(n+1);
o[1]=1;
for(auto i:s[1])
{
o[i]=1;
q.push(i);
}
while(!q.empty())
{
int w=q.front();
q.pop();
for(auto i:s[w])
if(o[i]==0)
{
o[i]=1;
q.push(i);
wyn.push_back({1,{1,i}});
}
}
for(auto i:ls1)
if(i.first!=1&&mp[i]==0)
wyn.push_back({1,i});
for(auto i:ls)
if(i.first!=1&&mp1[i]==0)
wyn.push_back({0,i});
vector<int>o1(n+1);
function<void(int)>dfs=[&](int w)
{
o1[w]=1;
for(auto i:s1[w])
if(o1[i]==0)
dfs(i);
if(w!=1&&mp1[{1,w}]==0)
wyn.push_back({0,{1,w}});
};
dfs(1);
cout<<wyn.size()<<"\n";
for(auto i:wyn)
if(i.first)
cout<<"+ "<<i.second.first<<" "<<i.second.second<<"\n";
else
cout<<"- "<<i.second.first<<" "<<i.second.second<<"\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 | #include <bits/stdc++.h> using namespace std; int main() { int n,m1; cin>>n; cin>>m1; int a,b; vector<vector<int>>s(n+1),s1(n+1); vector<pair<int,pair<int,int>>>wyn; map<pair<int,int>,int>mp,mp1; vector<pair<int,int>>ls; for(int i=0; i<m1; i++) { cin>>a>>b; s[a].push_back(b); s[b].push_back(a); mp[{min(a,b),max(a,b)}]=1; ls.push_back({min(a,b),max(a,b)}); } int m2; cin>>m2; vector<pair<int,int>>ls1; for(int i=0; i<m2; i++) { cin>>a>>b; s1[a].push_back(b); s1[b].push_back(a); mp1[{min(a,b),max(a,b)}]=1; ls1.push_back({min(a,b),max(a,b)}); } queue<int>q; vector<int>o(n+1); o[1]=1; for(auto i:s[1]) { o[i]=1; q.push(i); } while(!q.empty()) { int w=q.front(); q.pop(); for(auto i:s[w]) if(o[i]==0) { o[i]=1; q.push(i); wyn.push_back({1,{1,i}}); } } for(auto i:ls1) if(i.first!=1&&mp[i]==0) wyn.push_back({1,i}); for(auto i:ls) if(i.first!=1&&mp1[i]==0) wyn.push_back({0,i}); vector<int>o1(n+1); function<void(int)>dfs=[&](int w) { o1[w]=1; for(auto i:s1[w]) if(o1[i]==0) dfs(i); if(w!=1&&mp1[{1,w}]==0) wyn.push_back({0,{1,w}}); }; dfs(1); cout<<wyn.size()<<"\n"; for(auto i:wyn) if(i.first) cout<<"+ "<<i.second.first<<" "<<i.second.second<<"\n"; else cout<<"- "<<i.second.first<<" "<<i.second.second<<"\n"; } |
English