九野的博客,转载请注明出处:http://blog.csdn.net/acmmmm/article/details/13799337

题意:

T个测试数据

n串字符 能否倒过来用(1表示能倒着用)

问能否把所有字符串 首尾相接

欧拉回路是图G中的一个回路,经过每条边有且仅一次,称该回路为欧拉回路。具有欧拉回路的图称为欧拉图,简称E图。

混合图就是边集中有有向边和无向边同时存在。这时候需要用网络流建模求解。

不能倒着用就是有向边,能倒着用就是无向边

http://yzmduncan.iteye.com/blog/1149049

.

欧拉回路要求出度=入度 ,因此若出度与入度 差为奇数,一定没有欧拉回路 ch[i]%1 必须==0

用并查集判断每个字母的祖先是否相同((不相同则图不连通)

对于所有的点,总入度 + 总出度 = 0 【1】

ch[i]表示字母i+‘a' 的入度-出度的值

由【1】式推出 所有 ch[i] < 0的点 + ch[i]>0 的点 = 0

所以 让所有ch[i]<0 点与源点建边 ,权值为 -ch[i]

ch[i]>0 与汇点建边,权值为 ch[i]

若dinic满流,表示上述式子成立,则存在回路

#include <stdio.h>
#include <string.h>
#include <queue>
#define inf 10000
#define ll short
#define end End using namespace std; struct Edge{
short from, to, cap, nex;
}edge[1007];
short head[28], edgenum;
void addedge(short u, short v, short cap){
Edge E ={u, v, cap, head[u]};
edge[edgenum] = E;
head[u] = edgenum++;
Edge E_ = {v,u,0,head[v]};
edge[edgenum] = E_;
head[v] = edgenum++;
} short sign[28];
bool BFS(short from, short to){
memset(sign, -1, sizeof(sign));
sign[from] = 0; queue<short>q;
q.push(from);
while( !q.empty() ){
int u = q.front(); q.pop();
for(short i = head[u]; i!=-1; i = edge[i].nex)
{
short v = edge[i].to;
if(sign[v]==-1 && edge[i].cap)
{
sign[v] = sign[u] + 1, q.push(v);
if(sign[to] != -1)return true;
}
}
}
return false;
}
short Stack[4], top, cur[4];
short dinic(short from, short to){
short ans = 0;
while( BFS(from, to) )
{
memcpy(cur, head, sizeof(head));
short u = from; top = 0;
while(1)
{
if(u == to)
{
short flow = inf, loc;//loc 表示 Stack 中 cap 最小的边
for(short i = 0; i < top; i++)
if(flow > edge[ Stack[i] ].cap)
{
flow = edge[Stack[i]].cap;
loc = i;
} for(short i = 0; i < top; i++)
{
edge[ Stack[i] ].cap -= flow;
edge[Stack[i]^1].cap += flow;
}
ans += flow;
top = loc;
u = edge[Stack[top]].from;
}
for(short i = cur[u]; i!=-1; cur[u] = i = edge[i].nex)//cur[u] 表示u所在能增广的边的下标
if(edge[i].cap && (sign[u] + 1 == sign[ edge[i].to ]))break;
if(cur[u] != -1)
{
Stack[top++] = cur[u];
u = edge[ cur[u] ].to;
}
else
{
if( top == 0 )break;
sign[u] = -1;
u = edge[ Stack[--top] ].from;
}
}
}
return ans;
}
ll n;
char ss[22];
short ch[27], f[27];
bool use[27];
short start, end; short find(short x){return x==f[x]?x:(f[x]=find(f[x]));}
void Union(short x, short y){
short fx = find(x), fy = find(y);
short temp = fx; if(fx>fy){fx=fy;fy=temp;}
f[fx] = fy;
}
int main(){
short T, i, j, Cas = 1; scanf("%d",&T);
while(T--)
{
memset(ch, 0, sizeof(ch));
memset(use,0, sizeof(use));
for(i=0;i<27;i++)f[i] = i;
memset(head, -1,sizeof(head)), edgenum = 0; scanf("%d",&n);
for(i = 0; i < n; i++)
{
scanf("%s %d",ss,&j);
int a = ss[0]-'a', b = ss[strlen(ss)-1] - 'a';
ch[a]++, ch[b]--;
use[a] = use[b] = true;
Union(a,b); if(j)addedge(a,b,1);
}
printf("Case %d: ",Cas++);
bool ok = true;
for(i=0;i<26;i++)
if(use[i])
{
j = i;
for(i++;i<26;i++)
if(use[i] && find(j)!=find(i))ok = false;
break;
}
short num = 0;
for(i=0;i<26;i++)if(use[i] && ch[i]%2)
{
num++;
if(ch[i]<0)start = i;
else end = i;
}
if(num == 1 || num>2)ok = false;
if(!ok){ printf("Poor boy!\n"); continue;}
if(num == 2)addedge(end, start, 1);
start = 26, end = 27;
short sum = 0;
for(i=0;i<26;i++)if(ch[i] && use[i] && i!=end && i!=start)
{
if(ch[i]<0)
addedge(start,i,-ch[i]/2), sum-=ch[i]/2;
else
addedge(i,end, ch[i]>>1);
}
if(sum != dinic(start, end))
printf("Poor boy!\n");
else
printf("Well done!\n"); }
}

最新文章

  1. mobiscroll之treelist使用
  2. MySQL字符编码
  3. HBase 的存储结构
  4. 【JDBC】向数据表插入数据时,自动获取生成的主键
  5. error X3025:global variables are implicitly constant, enable compatibility mode to allow modification
  6. SQL拾遗
  7. 《Linear Algebra and Its Applications》-chaper1-线性方程组-线性相关性
  8. python条件判断与循环
  9. 实现Canvas2D绘图 使元素绕中心居中旋转
  10. Meltdown Attack
  11. CentOS下添加Root权限用户(超级用户)方法
  12. 【转】福利大放送--不止是Android,Github超高影响力开源大放送,学习开发必备教科书
  13. mysql用户管理和pymysql模块
  14. mongo 更新字段值,若不存在则自动创建
  15. Essential pro angular and asp.net core 笔记
  16. ubuntu16.04下安装配置pl-svo
  17. dubbo 配置属性
  18. unix架构
  19. PHP性能调优---php-fpm中启用慢日志配置(用于检测执行较慢的PHP脚本)
  20. 解决Django中在.js文件中用ajax请求后端,找不到CSRF问题

热门文章

  1. 201671010127 2016—2017—2 Java怎样解决Java程序中中文乱码的问题。
  2. poj2456(二分+贪心)
  3. Json解析数据的简单使用
  4. 【校招面试 之 剑指offer】第10-2题 青蛙跳台阶问题
  5. [leetcode]131. Palindrome Partitioning字符串分割成回文子串
  6. ubuntu系统:插入优盘read-only file system
  7. springmvc使用数组接收页面商品列表批量删除传过来的参数,并完成批量删除的操作。
  8. struts框架总结
  9. db2 sql调优
  10. c++11多线程学习笔记之一 thread基础使用