HS BDC

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 748    Accepted Submission(s): 290

Problem Description
IELTS is around the corner! love8909 has registered for the exam, but he still hasn’t got prepared. Now he decides to take actions. But when he takes out the New Oriental IELTS Vocabulary, he finds there are so many words. But love8909 doesn’t get scared, because he has got a special skill. If he can make a list with some meaningful words, he will quickly remember these words and will not forget them. If the last letter of some word Wa is the same as the first letter of some word Wb, then you can connect these two words and make a list of two words. If you can connect a word to a list, you will make a longer list.

While love8909 is making the list, he finds that some words are still meaningful words if you reverse them. For example, if you reverse the word “pat”, you will get another meaningful word “tap”.

After scanning the vocabulary, love8909 has found there are N words, some of them are meaningful if reversed, while others are not. Now he wonders whether he can remember all these words using his special skill.

The N-word list must contain every word once and only once.

 
Input
An integer T (T <= 50) comes on the first line, indicating the number of test cases.

On the first line of each test cases is an integer N (N <= 1000), telling you that there are N words that love8909 wants to remember. Then comes N lines. Each of the following N lines has this format: word type. Word will be a string with only ‘a’~’z’, and type will be 0(not meaningful when reversed) or 1(meaningful when reversed). The length of each word is guaranteed to be less than 20.

 
Output
The format of the output is like “Case t: s”, t is the number of the test cases, starting from 1, and s is a string.
For each test case, if love8909 can remember all the words, s will be “Well done!”, otherwise it’s “Poor boy!”

 
Sample Input
3
6
aloha 0
arachnid 0
dog 0
gopher 0
tar 1
tiger 0
3
thee 1
earn 0
nothing 0
2
pat 1
acm 0
 
Sample Output
Case 1: Well done!
Case 2: Well done!
Case 3: Poor boy!

Hint

In the first case, the word “tar” is still meaningful when reversed, and love8909 can make a list as “aloha-arachnid-dog-gopher-rat-tiger”.
In the second case, the word “thee” is still meaningful when reversed, and love8909 can make a list as “thee-earn-nothing”. In the third case,
no lists can be created.

 
Author
allenlowesy
 
Source
 
 
 
 

几乎就是模板题了。

先要判断连通。

然后转化成网络流判断欧拉回路。

 /* ***********************************************
Author :kuangbin
Created Time :2014-2-3 15:00:40
File Name :E:\2014ACM\专题学习\图论\欧拉路\混合图\HDU3472.cpp
************************************************ */ #include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std; const int MAXN = ;
//最大流部分
const int MAXM = ;
const int INF = 0x3f3f3f3f;
struct Edge
{
int to,next,cap,flow;
}edge[MAXM];
int tol;
int head[MAXN];
int gap[MAXN], dep[MAXN], pre[MAXN], cur[MAXN];
void init()
{
tol = ;
memset(head,-,sizeof(head));
}
void addedge(int u,int v,int w,int rw = )
{
edge[tol].to = v;
edge[tol].cap = w;
edge[tol].next = head[u];
edge[tol].flow = ;
head[u] = tol++;
edge[tol].to = u;
edge[tol].cap = rw;
edge[tol].next = head[v];
edge[tol].flow = ;
head[v] = tol++;
}
int sap(int start,int end,int N)
{
memset(gap,,sizeof(gap));
memset(dep,,sizeof(dep));
memcpy(cur,head,sizeof(head));
int u = start;
pre[u] = -;
gap[] = N;
int ans = ;
while(dep[start] < N)
{
if(u == end)
{
int Min = INF;
for(int i = pre[u];i != -;i = pre[edge[i^].to])
if(Min > edge[i].cap - edge[i].flow)
Min = edge[i].cap - edge[i].flow;
for(int i = pre[u]; i != -;i = pre[edge[i^].to])
{
edge[i].flow += Min;
edge[i^].flow -= Min;
}
u = start;
ans += Min;
continue;
}
bool flag = false;
int v;
for(int i = cur[u]; i != -;i = edge[i].next)
{
v = edge[i].to;
if(edge[i].cap - edge[i].flow && dep[v] + == dep[u])
{
flag = true;
cur[u] = pre[v] = i;
break;
}
}
if(flag)
{
u = v;
continue;
} int Min = N;
for(int i = head[u]; i != -;i = edge[i].next)
if(edge[i].cap - edge[i].flow && dep[edge[i].to] < Min)
{
Min = dep[edge[i].to];
cur[u] = i;
}
gap[dep[u]] --;
if(!gap[dep[u]])return ans;
dep[u] = Min+;
gap[dep[u]]++;
if(u != start) u = edge[pre[u]^].to;
}
return ans;
} int in[],out[];
int F[];
int find(int x)
{
if(F[x] == -)return x;
else return F[x] = find(F[x]);
}
void bing(int u,int v)
{
int t1 = find(u), t2 = find(v);
if(t1 != t2)F[t1] = t2;
}
char str[];
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
int T,n;
scanf("%d",&T);
int iCase = ;
while(T--)
{
iCase++;
scanf("%d",&n);
memset(F,-,sizeof(F));
memset(in,,sizeof(in));
memset(out,,sizeof(out));
init();
int k;
int s = -;
while(n--)
{
scanf("%s%d",str,&k);
int len = strlen(str);
int u = str[] - 'a';
int v = str[len-] - 'a';
out[u]++;
in[v]++;
s = u;
if(k == )
addedge(u,v,);
bing(u,v);
}
bool flag = true;
int cnt = ;
int s1 = -, s2 = -;
for(int i = ;i < ;i++)
if(in[i] || out[i])
{
if(find(i) != find(s))
{
flag = false;
break;
}
if((in[i] + out[i])&)
{
cnt++;
if(s1 == -)s1 = i;
else s2 = i;
}
}
if(cnt != && cnt != )flag = false;
if(!flag)
{
printf("Case %d: Poor boy!\n",iCase);
continue;
}
if(cnt == )
{
out[s1]++;
in[s2]++;
addedge(s1,s2,);
}
for(int i = ;i < ;i++)
{
if(out[i] - in[i] > )
addedge(,i,(out[i] - in[i])/);
else if(in[i] - out[i] > )
addedge(i,,(in[i] - out[i])/);
}
sap(,,);
for(int i = head[];i != -;i = edge[i].next)
if(edge[i].cap > && edge[i].cap > edge[i].flow)
{
flag = false;
break;
}
if(flag)printf("Case %d: Well done!\n",iCase);
else printf("Case %d: Poor boy!\n",iCase);
}
return ;
}

HDU 3472 HS BDC (混合图的欧拉路径判断)的更多相关文章

  1. hdu 3472 HS BDC(混合路的欧拉路径)

    这题是混合路的欧拉路径问题. 1.判断图的连通性,若不连通,无解. 2.给无向边任意定向,计算每个结点入度和出度之差deg[i].deg[i]为奇数的结点个数只能是0个或2个,否则肯定无解. 3.(若 ...

  2. hdu 4738 Caocao's Bridges 图--桥的判断模板

    Caocao's Bridges Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  3. HDU 3472 混合图欧拉回路 + 网络流

    九野的博客,转载请注明出处:http://blog.csdn.net/acmmmm/article/details/13799337 题意: T个测试数据 n串字符 能否倒过来用(1表示能倒着用) 问 ...

  4. POJ 1637 混合图的欧拉回路判定

    题意:一张混合图,判断是否存在欧拉回路. 分析参考: 混合图(既有有向边又有无向边的图)中欧拉环.欧拉路径的判定需要借助网络流! (1)欧拉环的判定:一开始当然是判断原图的基图是否连通,若不连通则一定 ...

  5. POJ 1637 Sightseeing tour (混合图欧拉路判定)

    Sightseeing tour Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 6986   Accepted: 2901 ...

  6. UVa 10735 (混合图的欧拉回路) Euler Circuit

    题意: 给出一个图,有的边是有向边,有的是无向边.试找出一条欧拉回路. 分析: 按照往常的思维,遇到混合图,我们一般会把无向边拆成两条方向相反的有向边. 但是在这里却行不通了,因为拆成两条有向边的话, ...

  7. POJ 1637 - Sightseeing tour - [最大流解决混合图欧拉回路]

    嗯,这是我上一篇文章说的那本宝典的第二题,我只想说,真TM是本宝典……做的我又痛苦又激动……(我感觉ACM的日常尽在这张表情中了) 题目链接:http://poj.org/problem?id=163 ...

  8. POJ1637:Sightseeing tour(混合图的欧拉回路)

    Sightseeing tour Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 10581   Accepted: 4466 ...

  9. Sightseeing tour 【混合图欧拉回路】

    题目链接:http://poj.org/problem?id=1637 Sightseeing tour Time Limit: 1000MS   Memory Limit: 10000K Total ...

随机推荐

  1. Docker学习笔记四 Docker容器

    本文地址:https://www.cnblogs.com/veinyin/p/10439849.html    容器是独立运行的一个或一组应用及他们的运行态环境,对应虚拟机的操作系统和应用. 启动 可 ...

  2. Ubuntu 12.04安装后无法boot

    解决方法是: 在选择启动项时, U盘会有两种启动方式: UEFI和非UEFI的. 用非UEFI进入live CD安装系统后无法启动, 用UEFI重装后问题解决.

  3. MVC Form验证 登陆和退出Cookies的设定和消除

    红色部分为重点 1.webconfig配置  <system.web>节点下添加 <authentication mode="Forms"> <for ...

  4. windows下使用pip安装python模块lxml

    pip install lxml 1 1 会有如下问题:  结果一路解决下去,解决了一个坑还是有一个坑,遂放弃,查找有没有别的解决办法. 亲测使用wheel+pip可以成功安装lxml! wheel本 ...

  5. Springboot分模块开发详解(2):建立子工程

    1.创建base-entity 选中base工程,右键创建一个新的maven工程 自动选择了base这个目录存放子工程 创建后,pom.xml修改成如下内容: <?xml version=&qu ...

  6. ubuntu 12.04网络配置之设置静态iP

    step: 1.输入命令: sudo vi /etc/network/interfaces 看到如下内容: 2.追加以下内容: iface eth0 inet static address 192.1 ...

  7. 2018ACM/ICPC 青岛现场赛 E题 Plants vs. Zombies

    题意: 你的房子在0点,1,2,3,...,n(n<=1e5)点每个点都有一颗高度为0的花,浇一次水花会长a[i]. 你有一个机器人刚开始在你家,最多走m步,每一步只能往前走或者往后走,每走到一 ...

  8. Kubernetes Master节点灾备恢复操作指南---升级版

    本文档简述了Kubernetes主节点灾备恢复的相关步骤,供在发生k8s master崩溃时操作. 就算是在k8s里部署了etcd群集, 主节点控制组件的高可用节点,灾备恢复也是必须要实现的操作,才能 ...

  9. spark streaming限制吞吐

    使用spark.streaming.receiver.maxRate这个属性限制每秒的最大吞吐.官方文档如下: Maximum rate (number of records per second) ...

  10. ubantu下如何完全彻底卸载mysql(转)

    ubantu下如何完全彻底卸载mysql  https://blog.csdn.net/wszll_Alex/article/details/46277681 第1步  依次执行下面的语句 1 sud ...