Codeforces Round #288 (Div. 2)-D. Tanya and Password(欧拉路径及其打印)
来源:互联网 发布:for循环遍历二维数组 编辑:程序博客网 时间:2024/06/01 08:45
原题链接
#include <bits/stdc++.h>#define maxn 200005using namespace std;typedef long long ll;char pp[4005][3];int mp[205][205];vector<int> v[4005];int vis[4005], cnt = 0, k1[4005], k2[4005], kk = 0;char str[4], ans[200010];void dfs(int j){//记录欧拉路径 while(vis[j] < v[j].size()){ dfs(v[j][vis[j]++]); } ans[kk++] = pp[j][1];}int find(int b){int j = b;while(j != vis[j]) j = vis[j];int ss = b;while(vis[ss] != j){b = vis[ss];vis[ss] = j;ss = b;}return j;}int main(){//freopen("in.txt", "r", stdin);int n, from, to;scanf("%d", &n);for(int i = 1; i <= 4000; i++) vis[i] = i; for(int i = 0; i < n; i++){ scanf("%s", str); if(mp[str[0]][str[1]] == 0){ mp[str[0]][str[1]] = ++cnt; pp[cnt][0] = str[0]; pp[cnt][1] = str[1]; from = cnt; } else from = mp[str[0]][str[1]]; if(mp[str[1]][str[2]] == 0){ mp[str[1]][str[2]] = ++cnt; pp[cnt][0] = str[1]; pp[cnt][1] = str[2]; to = cnt; } else to = mp[str[1]][str[2]]; v[from].push_back(to); k2[from]++; k1[to]++; int b1 = find(from), b2 = find(to); if(b1 != b2) vis[b1] = b2; } int c = 0; for(int i = 1; i <= cnt; i++) if(vis[i] == i){ c++; } if(c > 1){ puts("NO"); return 0; } int s1 = -1, s2 = -1, f = 0; for(int i = 1; i <= cnt; i++){ if(abs(k1[i] - k2[i]) > 1){ puts("NO"); return 0; } if(k1[i] > k2[i]){ s1 = i; f++; } if(k2[i] > k1[i]){ s2 = i; f++; } } if(f > 2){ puts("NO"); return 0; } if(s1 == -1 && s2 == -1){ s2 = 1; puts("YES"); } else if(s1 != -1 && s2 != -1) puts("YES"); memset(vis, 0, sizeof(vis)); dfs(s2); printf("%c", pp[s2][0]); for(int i = kk - 1; i >= 0; i--) printf("%c", ans[i]); puts(""); return 0;}
0 0
- Codeforces Round #288 (Div. 2)-D. Tanya and Password(欧拉路径及其打印)
- Codeforces Round #288 (Div. 2) D.Tanya and Password(欧拉路径)
- Codeforces Round #288 (Div. 2)D.Tanya and Password——欧拉通路
- Codeforces Round #288 (Div. 2) D. Tanya and Password (欧拉通路)
- Codeforces Round #288 (Div. 2) D. Tanya and Password 欧拉回路
- codeforces Round#288D Tanya and Password 欧拉通路
- Codeforces Round #288 (Div. 2)---D. Tanya and Password
- Codeforces Round #288 (Div. 2) D. Tanya and Password
- Codeforces Round #288 (Div. 2) D Tanya and Password
- Codeforces Round #288 (Div. 2)---D. Tanya and Password
- Tanya and Password - CodeForces 508 D 欧拉路径
- CodeForces 508D Tanya and Password(欧拉路径)
- CodeForces 508D Tanya and Password欧拉路径
- CodeForces 508D(Codeforces Round #288 (Div. 2))Tanya and Password
- 【欧拉回路】 Codeforces 288 D Tanya and Password
- Codeforces 508D Tanya and Password 欧拉通路Euler
- Codeforces 508D - Tanya and Password (欧拉道路)
- codeforces - 508D - Tanya and Password(欧拉通路)
- Leetcode 131. Palindrome Partitioning
- 1072. Gas Station (30)
- leetcode 48. Rotate Image
- Java语言中字符串的基本操作:
- OA---function
- Codeforces Round #288 (Div. 2)-D. Tanya and Password(欧拉路径及其打印)
- Git中的merge命令实现和工作方式
- 编译Android内核 For nexus 5 以及绕过Android的反调试
- Lock与synchronized 的区别
- 操作系统笔记<一>之操作系统概述
- Linux时间系统:时钟源clocsource
- OJ 7219 复杂的整数划分问题__动态规划
- LeetCode 70. Climbing Stairs
- eAndriod学习一:IDE、ADT、SDK、JDK