#include <cstdio> #include <vector> #include <set> #include <algorithm> #define MAKSN 30010 using namespace std; vector<int> G[MAKSN]; bool bylo[MAKSN]; vector<int> G2[MAKSN]; bool bylo2[MAKSN]; set< pair<int,int> > S1, S2; vector<int> kolejnosc; vector< pair<int,int> > ruchy; void dfs(int v) { bylo[v]=true; if(v!=1 && S1.find(make_pair(1,v))==S1.end()) { ruchy.push_back(make_pair(1,v)); S1.insert(make_pair(1,v)); } for(int u: G[v]) { if(bylo[u])continue; dfs(u); } } void dfs2(int v) { bylo2[v]=true; kolejnosc.push_back(v); for(int u: G2[v]) { if(bylo2[u])continue; dfs2(u); } } int main() { int n,m1,m2; scanf("%d",&n); scanf("%d",&m1); for(int i=0;i<m1;i++) { int a,b; scanf("%d %d",&a,&b); G[a].push_back(b); G[b].push_back(a); if(a>b)swap(a,b); S1.insert(make_pair(a,b)); } dfs(1); scanf("%d",&m2); for(int i=0;i<m2;i++) { int a,b; scanf("%d %d",&a,&b); G2[a].push_back(b); G2[b].push_back(a); if(a>b)swap(a,b); S2.insert(make_pair(a,b)); if(S1.find(make_pair(a,b))==S1.end()) { ruchy.push_back(make_pair(a,b)); } } for(const pair<int,int> &p: S1) { int a=p.first; int b=p.second; if(a==1)continue; if(S2.find(p)==S2.end())ruchy.push_back(make_pair(-a,-b)); } dfs2(1); for(int i=kolejnosc.size()-1; i>0; i--) { int v=kolejnosc[i]; if(S2.find(make_pair(1,v))==S2.end())ruchy.push_back(make_pair(-1,-v)); } printf("%d\n",ruchy.size()); for(int i=0;i<ruchy.size();i++) { int a=ruchy[i].first; int b=ruchy[i].second; if(a<0)printf("- %d %d\n",-a,-b); else printf("+ %d %d\n",a,b); } }
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 | #include <cstdio> #include <vector> #include <set> #include <algorithm> #define MAKSN 30010 using namespace std; vector<int> G[MAKSN]; bool bylo[MAKSN]; vector<int> G2[MAKSN]; bool bylo2[MAKSN]; set< pair<int,int> > S1, S2; vector<int> kolejnosc; vector< pair<int,int> > ruchy; void dfs(int v) { bylo[v]=true; if(v!=1 && S1.find(make_pair(1,v))==S1.end()) { ruchy.push_back(make_pair(1,v)); S1.insert(make_pair(1,v)); } for(int u: G[v]) { if(bylo[u])continue; dfs(u); } } void dfs2(int v) { bylo2[v]=true; kolejnosc.push_back(v); for(int u: G2[v]) { if(bylo2[u])continue; dfs2(u); } } int main() { int n,m1,m2; scanf("%d",&n); scanf("%d",&m1); for(int i=0;i<m1;i++) { int a,b; scanf("%d %d",&a,&b); G[a].push_back(b); G[b].push_back(a); if(a>b)swap(a,b); S1.insert(make_pair(a,b)); } dfs(1); scanf("%d",&m2); for(int i=0;i<m2;i++) { int a,b; scanf("%d %d",&a,&b); G2[a].push_back(b); G2[b].push_back(a); if(a>b)swap(a,b); S2.insert(make_pair(a,b)); if(S1.find(make_pair(a,b))==S1.end()) { ruchy.push_back(make_pair(a,b)); } } for(const pair<int,int> &p: S1) { int a=p.first; int b=p.second; if(a==1)continue; if(S2.find(p)==S2.end())ruchy.push_back(make_pair(-a,-b)); } dfs2(1); for(int i=kolejnosc.size()-1; i>0; i--) { int v=kolejnosc[i]; if(S2.find(make_pair(1,v))==S2.end())ruchy.push_back(make_pair(-1,-v)); } printf("%d\n",ruchy.size()); for(int i=0;i<ruchy.size();i++) { int a=ruchy[i].first; int b=ruchy[i].second; if(a<0)printf("- %d %d\n",-a,-b); else printf("+ %d %d\n",a,b); } } |