HDU 3472 HS BDC (混合图的欧拉路径判断)
HS BDC
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 748 Accepted Submission(s): 290
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.
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.
For each test case, if love8909 can remember all the words, s will be “Well done!”, otherwise it’s “Poor boy!”
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
Case 2: Well done!
Case 3: Poor boy!
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 :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 (混合图的欧拉路径判断)的更多相关文章
- hdu 3472 HS BDC(混合路的欧拉路径)
这题是混合路的欧拉路径问题. 1.判断图的连通性,若不连通,无解. 2.给无向边任意定向,计算每个结点入度和出度之差deg[i].deg[i]为奇数的结点个数只能是0个或2个,否则肯定无解. 3.(若 ...
- hdu 4738 Caocao's Bridges 图--桥的判断模板
Caocao's Bridges Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- HDU 3472 混合图欧拉回路 + 网络流
九野的博客,转载请注明出处:http://blog.csdn.net/acmmmm/article/details/13799337 题意: T个测试数据 n串字符 能否倒过来用(1表示能倒着用) 问 ...
- POJ 1637 混合图的欧拉回路判定
题意:一张混合图,判断是否存在欧拉回路. 分析参考: 混合图(既有有向边又有无向边的图)中欧拉环.欧拉路径的判定需要借助网络流! (1)欧拉环的判定:一开始当然是判断原图的基图是否连通,若不连通则一定 ...
- POJ 1637 Sightseeing tour (混合图欧拉路判定)
Sightseeing tour Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 6986 Accepted: 2901 ...
- UVa 10735 (混合图的欧拉回路) Euler Circuit
题意: 给出一个图,有的边是有向边,有的是无向边.试找出一条欧拉回路. 分析: 按照往常的思维,遇到混合图,我们一般会把无向边拆成两条方向相反的有向边. 但是在这里却行不通了,因为拆成两条有向边的话, ...
- POJ 1637 - Sightseeing tour - [最大流解决混合图欧拉回路]
嗯,这是我上一篇文章说的那本宝典的第二题,我只想说,真TM是本宝典……做的我又痛苦又激动……(我感觉ACM的日常尽在这张表情中了) 题目链接:http://poj.org/problem?id=163 ...
- POJ1637:Sightseeing tour(混合图的欧拉回路)
Sightseeing tour Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 10581 Accepted: 4466 ...
- Sightseeing tour 【混合图欧拉回路】
题目链接:http://poj.org/problem?id=1637 Sightseeing tour Time Limit: 1000MS Memory Limit: 10000K Total ...
随机推荐
- Anaconda+django写出第一个web app(八)
今天来实现网站的登入和登出功能. 首先我们需要在urls.py中添加路径,注意此处的路径和在导航栏中设置的文字路径保持一致: from django.urls import path from . i ...
- Hive笔记之Fetch Task
在使用Hive的时候,有时候只是想取表中某个分区的前几条的记录看下数据格式,比如一个很常用的查询: select * from foo where partition_column=bar limit ...
- Jquery ajax json 不执行success的原因 坑爹
最近在看jQuery的API文档,在使用到jQuery的ajax时,如果指定了dataType为json,老是不执行success回调,而是执行了error回调函数,极度郁闷.后面改为1.2.6版本可 ...
- 浅介HTML DOM
什么是DOM? DOM是Document Object Model(文档对象模型)的缩写. DOM是W3C(万维网联盟)的标准. DOM定义了访问HTML和XML文档的标准: “W3C文档对象模型(D ...
- linux终端自定义设置
2014年1月4日 19:21:16 1. ls命令显示文件列表时不再花花绿绿 vi ~/.bashrc alias ls='ls -a --color=never' alias ll='ls -lh ...
- yolov2在CUDA8.0+cudnn8.0下安装、训练、检测经历
这次用yolov2做检测时遇到个大坑,折腾了我好几天,特以此文记录之. 一.安装cuda+cudnn 它们的版本必须要匹配,否则训练后检测不出目标! 1.下载cuda8.0.61_375.26_lin ...
- java 异常使用指南
应该在以下情况使用异常: 1.)在恰当的级别处理问题.(在知道如何处理的情况下才捕获异常) 2.)解决问题并且重新调用异常的方法 3.)进行少许的修补,然后绕过异常发生的地方继续执行 4.)用别的数据 ...
- python日常
1.远程访问远程访问Jupyter Notebook,本地浏览器不能打开,先查了防火墙的状态,然后将设置的端口进行allow,网址,仍然拒绝链接,而后通过远程访问Jupyter Notebook,然后 ...
- Android 隐藏、显示软键盘方法
隐藏软键盘的终极方法: public class SoftKeyboardUtil { /** * 隐藏软键盘(只适用于Activity,不适用于Fragment) */ public static ...
- 如何用prometheus监控k8s集群中业务pod的metrics
一般,我们从网上看到的帖子和资料, 都是用prometheus监控k8s的各项资源, 如api server, namespace, pod, node等. 那如果是自己的业务pod上的自定义metr ...