求欧拉回路

圈套圈。该模板是uoj那道题的。

 1 //#include<iostream>
 2 #include<cstring>
 3 #include<cstdio>
 4 #include<time.h>
 5 //#include<math.h>
 6 //#include<set>
 7 #include<queue>
 8 //#include<bitset>
 9 //#include<vector>
10 #include<algorithm>
11 #include<stdlib.h>
12 using namespace std;
13 
14 #define LL long long
15 LL qread()
16 {
17     char c; LL s=0; int f=1; while ((c=getchar())<'0' || c>'9') (c=='-') && (f=-1);
18     do s=s*10+c-'0'; while ((c=getchar())>='0' && c<='9'); return s*f;
19 }
20 
21 //Pay attention to '-' , LL and double of qread!!!!
22 
23 int n,t,m;
24 #define maxn 100011
25 #define maxm 400011
26 struct Edge{int to,next;}edge[maxm]; int first[maxn],le=2;
27 void in(int x,int y) {Edge &e=edge[le]; e.to=y; e.next=first[x]; first[x]=le++;}
28 void insert(int x,int y) {in(x,y); in(y,x);}
29 
30 //int ufs[maxn],size[maxn];
31 //int find(int x) {return x==ufs[x]?x:(ufs[x]=find(ufs[x]));}
32 //void Union(int x,int y) {x=find(x); y=find(y); if (x!=y) ufs[x]=y,size[y]+=size[x];}
33 
34 int du[maxn],indu[maxn],odu[maxn];
35 
36 int sta[maxm],top=0,boss; bool vis[maxm];
37 void dfs(int x,int fa)
38 {
39 //    cout<<x<<endl;
40     for (int &i=first[x];i;i=edge[i].next)
41     {
42         if ((i^1)==fa) continue;
43         if (~fa && vis[i>>1]) continue;
44         if (fa==-1 && vis[i]) continue;
45         int j=i; Edge &e=edge[j]; if (~fa) vis[j>>1]=1; else vis[j]=1;
46         if (e.to==boss) {if (~fa) sta[++top]=(j&1)?-(j>>1):(j>>1); else sta[++top]=j-1; boss=0; continue;}
47         if (boss==0) x=boss;
48         dfs(e.to,fa==-1?-1:j); if (~fa) sta[++top]=(j&1)?-(j>>1):(j>>1); else sta[++top]=j-1;
49     }
50 }
51 
52 void solve1()
53 {
54     for (int i=1;i<=n;i++) if (du[i]&1) {puts("NO"); return;}
55     for (int i=1;i<=n;i++) if (du[i]) {dfs((boss=i),0); break;}
56     if (top!=m) {puts("NO"); return;}
57     puts("YES");
58     for (int i=top;i;i--) printf("%d ",sta[i]);
59 }
60 
61 void solve2()
62 {
63     for (int i=1;i<=n;i++) if (indu[i]!=odu[i]) {puts("NO"); return;}
64     for (int i=1;i<=n;i++) if (indu[i]) {dfs((boss=i),-1); break;}
65     if (top!=m) {puts("NO"); return;}
66     puts("YES");
67     for (int i=top;i;i--) printf("%d ",sta[i]);
68 }
69 
70 int main()
71 {
72     t=qread(); n=qread(); m=qread();
73     if (m==0) {puts("YES"); return 0;}
74     for (int i=1,x,y;i<=m;i++)
75     {x=qread(); y=qread(); if (t==1) insert(x,y),du[x]++,du[y]++; else in(x,y),indu[y]++,odu[x]++;}
76     if (t==1) solve1(); else solve2();
77     return 0;
78 }
View Code

顺便加个题51nod1967 路径定向

这可以用网络流,但T死。

实际把奇数度数点连起来做欧拉回路就好了。

posted @ 2018-07-11 22:14  Blue233333  阅读(148)  评论(0编辑  收藏  举报