UVA10129 Play on Words —— 欧拉回路
2024-08-25 04:10:37
题目链接:https://vjudge.net/problem/UVA-10129
代码如下:
// UVa10129 Play on Words
// Rujia Liu
// 题意:输入n个单词,是否可以排成一个序列,使得每个单词的第一个字母和上一个单词的最后一个字母相同
// 算法:把字母看作结点,单词看成有向边,则有解当且仅当图中有欧拉路径。注意要先判连通
#include<cstdio>
#include<cstring>
#include<vector>
using namespace std;
const int maxn = 10000 + 5; int pa[256], used[256], deg[256]; // 是否出现过;度数
char word[maxn];
int n, cc; int findset(int x) { return ( pa[x] != x ? pa[x] = findset(pa[x]) : x ); } void init()
{
memset(used, 0, sizeof(used));
memset(deg, 0, sizeof(deg)); for(int ch = 'a'; ch <= 'z'; ch++)
pa[ch] = ch; // 初始化并查集 cc = 26; // 连通块个数
} void solve()
{
scanf("%d", &n);
for(int i = 0; i < n; i++)
{
scanf("%s", word);
char c1 = word[0];
char c2 = word[strlen(word)-1];
deg[c1]++;
deg[c2]--;
used[c1] = used[c2] = 1; int s1 = findset(c1);
int s2 = findset(c2);
if(s1 != s2)
{
pa[s1] = s2;
cc--;
}
} vector<int> d;
for(int ch = 'a'; ch <= 'z'; ch++)
{
if(!used[ch])
cc--; // 没出现过的字母 else if(deg[ch] != 0)
d.push_back(deg[ch]);
} int ok = false;
if(cc == 1 && (d.empty() || (d.size() == 2 && (d[0] == 1 || d[0] == -1))))
ok = true; if(ok)
printf("Ordering is possible.\n");
else
printf("The door cannot be opened.\n");
} int main()
{
int T;
scanf("%d", &T);
while(T--)
{
init();
solve();
}
return 0;
}
最新文章
- C#语言基础
- 关于npm
- io流对文件读写操作
- windows server2012 r2 上 安装 IIS8.5
- [2]. jekyll安装与应用
- struts2+jquery 实现ajax登陆
- 关于模板中的动态取值 ---反射与javascript脚本编译
- poj--1517
- C#动态表达式计算(续1)
- Golang: pprof
- cygwin 运行窗口程序
- If I were you
- WebApi接口返回值不困惑:返回值类型详解
- React系列文章:无状态组件生成真实DOM结点
- #HTTP协议学习# (一)request 和response 解析
- Singleton单例对象的使用
- http后台json解析实例
- Linux运维学习笔记-网络技术知识体系总结
- ASP.NET 预编译笔记
- 如何使用奥特歌词制作双语LRC字幕
热门文章
- 洛谷——P1294 高手去散步
- cef 下载地址
- IntelliJ IDEA出现:java: Compilation failed: internal java compiler error的问题解决
- 通过HTML5 FileReader实现上传图片预览功能
- C#使用反射机制获取类信息[转]
- 网页Tab控件
- C中的预编译编译链接
- Windows 7 &;amp; Ubuntu 14.04完美双系统安装及系统引导配置----校园网Mentohust配置
- JS 模板引擎 Handlebars.js 中文说明
- asp.net core 初探 二