hdu3472 混合欧拉
题意:
给你一些字符串,有的字符串反过来也有意义,题目问给的这n个字符串是否可以首尾相连,组成一个串。
思路:
算是混合欧拉的基础题目了,混合欧拉就是专门处理这类问题的,先说下混合欧拉的大体步骤。
(1) 判断整个图是否连通,如果不连通直接不行(方法随意,并查集搜索什么的都行)
(2) 看度数差为奇数的有多少个,只能有0个或者两个,其他的都不行。
(3) 如果度数差有奇数的有两个,那么一定一个是正的v1,一个是负的v2,add(v1 ,v2 ,1);
(4) 如果方向随意的边,那么我们随意建立一条就行add(a ,b ,1),方向固定的不用管
(5) 虚拟超级远点s,超级汇点e,然后所有度数为负数的add(s ,i ,-du[i]/2),所有为 正的add(i ,e ,du[i]/2);
(6) 最后一遍最大流,看是否满流,如果满流,那么就是有解,否则无解。
至于为什么费用留可以这样求,等比赛回来再来详细补充这个问题。
#include<queue>
#include<stdio.h>
#include<string.h>
#define N_node 30
#define N_edge 10000
#define INF 100000000
using namespace std;
typedef struct
{
int to ,next ,cost;
}STAR;
typedef struct
{
int x ,t;
}DEP;
STAR E[N_edge];
DEP xin ,tou;
int list[N_node] ,listt[N_node] ,tot;
int du[N_node] ,deep[N_node];
int mer[N_node];
void add(int a, int b ,int c)
{
E[++tot].to = b;
E[tot].cost = c;
E[tot].next = list[a];
list[a] = tot;
E[++tot].to = a;
E[tot].cost = 0;
E[tot].next = list[b];
list[b] = tot;
}
int minn(int x ,int y)
{
return x < y ? x : y;
}
int finds(int x)
{
return x == mer[x] ? x : mer[x] = finds(mer[x]);
}
bool BFS_Deep(int s ,int t ,int n)
{
memset(deep ,255 ,sizeof(deep));
queue<DEP>q;
xin.x = s ,xin.t = 0;
q.push(xin);
deep[xin.x] = xin.t;
while(!q.empty())
{
tou = q.front();
q.pop();
for(int k = list[tou.x] ;k ;k = E[k].next)
{
xin.x = E[k].to;
xin.t = tou.t + 1;
if(deep[xin.x] != -1 || !E[k].cost) continue;
deep[xin.x] = xin.t;
q.push(xin);
}
}
for(int i = 0 ;i <= n ;i ++)
listt[i] = list[i];
return deep[t] != -1;
}
int DFS_Flow(int s ,int t ,int flow)
{
if(s == t) return flow;
int nowflow = 0;
for(int k = listt[s] ;k ;k = E[k].next)
{
int to = E[k].to;
int c = E[k].cost;
listt[s] = k;
if(deep[to] != deep[s] + 1 || !c) continue;
int tmp = DFS_Flow(to ,t ,minn(c ,flow - nowflow));
nowflow += tmp;
E[k].cost -= tmp;
E[k^1].cost += tmp;
if(nowflow == flow) break;
}
if(!nowflow) deep[s] = 0;
return nowflow;
}
int DINIC(int s ,int t ,int n)
{
int Ans = 0;
while(BFS_Deep(s ,t ,n))
{
Ans =+ DFS_Flow(s ,t ,INF);
}
return Ans;
}
int main ()
{
int n ,i ,j ,a ,b ,c;
int mark[30];
int t ,cas = 1;
char str[30];
scanf("%d" ,&t);
while(t--)
{
scanf("%d" ,&n);
memset(list ,0 ,sizeof(list)) ,tot = 1;
memset(du ,0 ,sizeof(du));
memset(mark ,0 ,sizeof(mark));
for(i = 1 ;i <= 26 ;i ++) mer[i] = i;
for(i = 1 ;i <= n ;i ++)
{
scanf("%s %d" ,str ,&c);
a = str[0] - 'a' + 1;
b = str[strlen(str)-1] - 'a' + 1;
du[a] -- ,du[b] ++;
mark[a] = mark[b] = 1;
mer[finds(a)] = finds(b);
if(c) add(a ,b ,1);
}
int sum = 0;
for(i = 1 ;i <= 26 && sum <= 2;i ++)
if(finds(i) == i && mark[i]) sum ++;
printf("Case %d: " ,cas ++);
if(sum != 1)
{
puts("Poor boy!");
continue;
}
sum = 0;
int v1 ,v2;
for(i = 1 ;i <= 26 ;i ++)
{
if(du[i] % 2 && du[i] < 0) v1 = i ,sum ++;
if(du[i] % 2 && du[i] > 0) v2 = i ,sum ++;
}
if(sum != 0 && sum != 2)
{
puts("Poor boy!");
continue;
}
if(sum == 2)
{
add(v1 ,v2 ,1);
du[v1] -- ,du[v2] ++;
}
sum = 0;
for(i = 1 ;i <= 26 ;i ++)
{
if(!mark[i]) continue;
if(du[i] < 0) add(0 ,i ,-du[i]/2) ,sum -= du[i]/2;
else add(i ,27 ,du[i]/2);
}
DINIC(0 ,27 ,27) == sum ? puts("Well done!"):puts("Poor boy!");
}
return 0;
}
hdu3472 混合欧拉的更多相关文章
- hdu4067 费用流(混合欧拉的宽展和延伸)
题意: 给以一个图,每个有向边都有两个权值,a,b其中a是保留这条边的花费,b是删除这条边的花费,让你删去一些边使图满足一下要求: (1)只有一个起点和一个终点 (2)所有的边都是又向的 ...
- hdu3472 混合图判断欧拉通路
对于欧拉回路,先判断出度入度的差是否为偶数,然后最大流一次. 此题是判断有无欧拉通路,前提要判断图是否连通,然后欧拉通路的条件:要么出入度差没有奇数,或者只有2个点. 所以先统计差为奇数的个数,如果不 ...
- hdu2588 GCD (欧拉函数)
GCD 题意:输入N,M(2<=N<=1000000000, 1<=M<=N), 设1<=X<=N,求使gcd(X,N)>=M的X的个数. (文末有题) 知 ...
- BZOJ 2705: [SDOI2012]Longge的问题 [欧拉函数]
2705: [SDOI2012]Longge的问题 Time Limit: 3 Sec Memory Limit: 128 MBSubmit: 2553 Solved: 1565[Submit][ ...
- BZOJ 2818: Gcd [欧拉函数 质数 线性筛]【学习笔记】
2818: Gcd Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 4436 Solved: 1957[Submit][Status][Discuss ...
- Euler-Maruyama discretization("欧拉-丸山"数值解法)
欧拉法的来源 在数学和计算机科学中,欧拉方法(Euler method)命名自它的发明者莱昂哈德·欧拉,是一种一阶数值方法,用以对给定初值的常微分方程(即初值问题)求解.它是一种解决常微分方程数值积分 ...
- COGS2531. [HZOI 2016]函数的美 打表+欧拉函数
题目:http://cogs.pw/cogs/problem/problem.php?pid=2533 这道题考察打表观察规律. 发现对f的定义实际是递归式的 f(n,k) = f(0,f(n-1,k ...
- poj2478 Farey Sequence (欧拉函数)
Farey Sequence 题意:给定一个数n,求在[1,n]这个范围内两两互质的数的个数.(转化为给定一个数n,比n小且与n互质的数的个数) 知识点: 欧拉函数: 普通求法: int Euler( ...
- 51Nod-1136 欧拉函数
51Nod: http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1136 1136 欧拉函数 基准时间限制:1 秒 空间限制: ...
随机推荐
- 微信小程序去除页面滚动条
::-webkit-scrollbar { width: 0; height: 0; color: transparent; display: none; } 父级元素(滚动的元素) width:10 ...
- 剑指 Offer 58 - I. 翻转单词顺序 + 双指针
剑指 Offer 58 - I. 翻转单词顺序 Offer_58_1 题目描述 方法一:使用Split函数 package com.walegarrett.offer; /** * @Author W ...
- PAT-1140(Look-and-say Sequence)字符串处理
Look-and-say Sequence PAT-1140 #include<iostream> #include<cstring> #include<string&g ...
- golang——net/rpc/jsonrpc包学习
1.jsonrpc包 该实现了JSON-RPC的ClientCodec和ServerCodec接口,可用于rpc包. 可用于跨语言使用go rpc服务. 2.常用方法 (1)func Dial(net ...
- ijkplayer接入使用
1.ijkplayer简介 ijkplayer是一个基于FFmpeg的轻量级Android/iOS视频播放器.FFmpeg的是全球领先的多媒体框架,能够解码,编码, 转码,复用,解复用,流,过滤器和播 ...
- MyBatis(七):MyBatis缓存详解(一级缓存/二级缓存)
一级缓存 MyBatis一级缓存上SqlSession缓存,即在统一SqlSession中,在不执行增删改操作提交事务的前提下,对同一条数据进行多次查询时,第一次查询从数据库中查询,完成后会存入缓 ...
- 2019 GDUT Rating Contest III : Problem A. Out of Sorts
题面: 传送门 A. Out of Sorts Input file: standard input Output file: standard output Time limit: 1 second M ...
- Vue.js 学习笔记之七:使用现有组件
5.3 使用现有组件 在之前的五个实验中,我们所演示的基本都是如何构建自定义组件的方法,但在具体开发实践中,并非项目中所有的组件都是需要程序员们自己动手来创建的.毕竟在程序设计领域,"不要重 ...
- python-实现链式栈
7 """ 8 用一个类来实现一个节点 9 """ 10 class Node(object): 11 def __init__(self, ...
- Blind Super-Resolution Kernel Estimation using an Internal-GAN 论文解读
背景与思路来源 目前 SR 模型中合成 LR 使用的模糊核问题 目前大多数 SR 的 model 都是用的合成下采样图片来进行训练的,而这些合成的图片常常使用的是 MATLAB 里面的 imresiz ...