生活随笔
收集整理的這篇文章主要介紹了
                                
CF508D Tanya and Password(欧拉回路)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.                        
 
                                
                            
                            
                            解析
 
之前模擬考過的一道題
 把字符串當成前后綴之間的連邊即可
 注意即使圖的度數符合要求,也可能由于不連通而無解,需要再特判一下
 
#include<bits/stdc++.h>
using namespace std
;
#define ll long long
#define ull unsigned long long
const int N
=2e5+100;
ll 
read() {ll x
=0,f
=1;char c
=getchar();while(!isdigit(c
)) {if(c
=='-')f
=-1;c
=getchar();}while(isdigit(c
)) {x
=x
*10+(c
^48);c
=getchar();}return x
*f
;
}
int n
,m
;
int key
=123;
struct node{int to
,nxt
;
}p
[N
<<1];
int fi
[N
],cnt
;
inline void addline(int x
,int y
){p
[++cnt
]=(node
){y
,fi
[x
]};fi
[x
]=cnt
;return;
}
int in
[N
],out
[N
];
int zhan
[N
],top
;
char s
[4];
void dfs(int x
){for(int i
=fi
[x
];~i
;i
=fi
[x
]){int to
=p
[i
].to
;fi
[x
]=p
[i
].nxt
;dfs(to
);}zhan
[++top
]=x
;
}
int main(){memset(fi
,-1,sizeof(fi
));cnt
=-1;n
=read();for(int i
=1;i
<=n
;i
++){scanf(" %s",s
+1);int a
=s
[1]*key
+s
[2],b
=s
[2]*123+s
[3];in
[b
]++;out
[a
]++;addline(a
,b
);}int id(0),num(0);for(int i
=1;i
<=15252;i
++){if(!in
[i
]&&!out
[i
]) continue;if(abs(out
[i
]-in
[i
])>1){printf("NO\n");return 0;}else if(out
[i
]>in
[i
]){num
++;id
=i
;}else if(!id
) id
=i
;}if(num
>1){printf("NO\n");return 0;}dfs(id
);if(top
!=n
+1) printf("NO\n");else{printf("YES\n");printf("%c%c",zhan
[top
]/key
,zhan
[top
]%key
);top
--;while(top
){putchar(zhan
[top
]%key
);top
--;}}return 0;
}
                            總結
                            
                                以上是生活随笔為你收集整理的CF508D Tanya and Password(欧拉回路)的全部內容,希望文章能夠幫你解決所遇到的問題。
                            
                            
                                如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。