51nod1757 大灾变
能想到二分答案+最大流判断是否符合。但是不知道如何建图qaq。参考的是http://blog.csdn.net/fsss_7/article/details/52132046的建图方法
#include<cstdio>
#include<cstring>
#include<cctype>
#include<algorithm>
#include<vector>
using namespace std;
#define rep(i,s,t) for(int i=s;i<=t;i++)
#define dwn(i,s,t) for(int i=s;i>=t;i--)
#define clr(x,c) memset(x,c,sizeof(x))
#define qwq(x) for(edge *o=head[x];o;o=o->next)
int read(){
int x=0;char c=getchar();
while(!isdigit(c)) c=getchar();
while(isdigit(c)) x=x*10+c-'0',c=getchar();
return x;
}
const int nmax=2e3+5;
const int maxn=2e5+5;
const int inf=0x7f7f7f7f;
struct edge{
int to,cap;edge *next,*rev;
};
edge es[maxn<<3],*pt=es,*head[maxn];
void add(int u,int v,int d){
pt->to=v;pt->cap=d;pt->next=head[u];head[u]=pt++;
pt->to=u;pt->cap=0;pt->next=head[v];head[v]=pt++;
head[u]->rev=head[v];head[v]->rev=head[u];
} vector<int>a[nmax];
int dis[50][nmax],in[nmax],on[nmax];
void dfs(int p,int x,int fa){
int u;
rep(i,0,a[x].size()-1) if((u=a[x][i])!=fa)
dis[p][u]=dis[p][x]+1,dfs(p,u,x);
} edge *cur[maxn],*p[maxn];
int cnt[maxn],h[maxn];
int maxflow(int s,int t,int n){
clr(cnt,0);cnt[0]=n;clr(h,0);
int flow=0,a=inf,x=s;edge *e;
while(h[s]<n){
for(e=cur[x];e;e=e->next) if(e->cap>0&&h[x]==h[e->to]+1) break;
if(e){
a=min(a,e->cap);cur[x]=p[e->to]=e;x=e->to;
if(x==t){
while(x!=s) p[x]->cap-=a,p[x]->rev->cap+=a,x=p[x]->rev->to;
flow+=a;a=inf;
}
}else{
if(!--cnt[h[x]]) break;
h[x]=n;
for(e=head[x];e;e=e->next) if(e->cap>0&&h[x]>h[e->to]+1) h[x]=h[e->to]+1,cur[x]=e;
cnt[h[x]]++;
if(x!=s) x=p[x]->rev->to;
}
}
return flow;
} int check(int x,int n,int m){
int s=0,t=n+m*x+1;
pt=es;clr(head,0);
rep(i,1,n) if(!in[i]) add(s,i,1);
rep(i,1,m) {
rep(j,1,x) add(n+(i-1)*x+j,t,1);
rep(j,1,x-1) add(n+(i-1)*x+j,n+(i-1)*x+j+1,inf);
}
rep(i,1,m){
rep(j,1,n) if(!in[j]&&dis[i][j]<=x) add(j,n+(i-1)*x+dis[i][j],1);
}
return maxflow(s,t,t+1);
}
int main(){
int n=read(),m=read(),u,v;
rep(i,1,n-1) u=read(),v=read(),a[u].push_back(v),a[v].push_back(u);
rep(i,1,m) u=read(),in[u]=1,on[i]=u;
rep(i,1,m) dfs(i,on[i],0);
int l=0,r=n,mid,ans=0;
while(l<=r){
mid=(l+r)>>1;
if(check(mid,n,m)==n-m) ans=mid,r=mid-1;
else l=mid+1;
}
printf("%d\n",ans);
return 0;
}
第1行两个整数n(n<=2000)和m(m<=40)表示节点个数和洞口个数
接下来n-1行每行两个整数表示树上的每一条边
第n+1行m个整数表示所有洞口的编号,保证洞口是叶子节点
一个整数t表示让所有种族躲进地下避难所的最少时间
6 2
1 2
1 3
1 4
1 5
5 6
3 6
3
51nod1757 大灾变的更多相关文章
- 51nod 1171 大灾变
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1757 二分答案mid 避难所拆为mid个点 每个避难所的第一个点向第二个 ...
- FJNU 1155 Fat Brother’s prediction(胖哥的预言)
FJNU 1155 Fat Brother’s prediction(胖哥的预言) Time Limit: 1000MS Memory Limit: 257792K [Description] [ ...
- [补档][COGS 426]血帆海盗
[COGS 426]血帆海盗 题目 传送门:http://cogs.pro/cogs/problem/problem.php?pid=426 随着资本的扩大,藏宝海湾贸易亲王在卡利姆多和东部王国大陆各 ...
- CJOJ 血帆海盗
Description 随着资本的扩大,藏宝海湾贸易亲王在卡利姆多和东部王 国大陆各建立了N/2 个港口.大灾变发生以后,这些港口之间失去了联系,相继脱离了藏宝海湾贸易亲王的管辖,各自为政.利益的驱动 ...
- WOW
WOW http://bbs.ngacn.cc/read.php?tid=4992959 http://ngasave.us/popcn/?t=gems 地精科技:国服最流行 http://bbs ...
- [daily][archlinux][game] 几个linux下还不错的游戏
春节在家放松, 装了几个游戏在archlinux上,玩起来还不错. 假期结束开工了, 玩的时间也会比较少,准备删掉, 记录如下: 1. 0ad 即时战略类, 类似于冷兵器时代的红警. 画面还不错, 可 ...
- bzoj AC倒序
Search GO 说明:输入题号直接进入相应题目,如需搜索含数字的题目,请在关键词前加单引号 Problem ID Title Source AC Submit Y 1000 A+B Problem ...
- softwares我的软件列表(转载)
[TOC] 个人在archlinux下的日常使用经验列出,故而在archlinux及其衍生发行版中,以下所列软件几乎可以从archlinux官方源或者aur中搜索下载安装,所列出名字一般即是其包名,使 ...
- [科普向] Roguelike游戏到底是什么?
简单的说 Roguelike 是 RPG(角色扮演游戏)的一个分支,也是最重要的一个分支.这个名字源于 1980 年发布的著名电子游戏<Rogue>.按字面上理解,Roguelike 就是 ...
随机推荐
- PHP合并数组array_merge函数运算符加号与的区别
两个的区别是:1.数组键名为数字键名时,要合并的两个数组中有同名数字KEY的时候,使用array_merge()不会覆盖掉原来的值,而使用“+”合并数组则会把最先出现的值作为最终结果返回,而把后面的数 ...
- 13test05:亲密数
/*#include<iostream> using namespace std; int main() {int sum[3000]={0}; for(int i=1;i<3000 ...
- photoshop基础
在Photoshop中,对图像的某个部分进行色彩调整,就必须有一个指定的过程.这个指定的过程称为选取.选取后形成选区. 现在先明确两个概念: 选区是封闭的区域,可以是任何形状,但一定是封闭的.不存在开 ...
- iOS后台运行
http://www.cocoachina.com/bbs/read.php?tid=149564 文一 我从苹果文档中得知,一般的应用在进入后台的时候可以获取一定时间来运行相关任务,也就是说可以在后 ...
- POJ 2136
#include <iostream> #include <string> #define MAXN 26 using namespace std; int _m[MAXN]; ...
- editplus bat语法高亮
editplus bat语法高亮 今天需要在Windows上写批处理,因为没写过,避免关键字错误,就需要语法高亮了,editplus默认没有bat语法文件,赶紧解决. 1:到 http://www.e ...
- Codeforces Round #263 (Div. 2) A B C
题目链接 A. Appleman and Easy Task time limit per test:2 secondsmemory limit per test:256 megabytesinput ...
- light oj 1068 - Investigation 数位DP
思路:典型的数位DP!!! dp[i][j][k]:第i位,对mod取余为j,数字和对mod取余为k. 注意:由于32位数字和小于95,所以当k>=95时,结果肯定为0. 这样数组就可以开小点, ...
- linux下关于gz和bz2压缩格式的常用操作技巧
.gz和.bz2都是linux下压缩文件的格式,有点类似windows下的.zip和.rar文件..bz2和.gz的区别在于,前者比后者压缩率更高,后者比前者花费更少的时间. 也就是说同一个文件,压缩 ...
- ruby 学习 -- string --1
# define french_string = "il \xc3\xa9tait une fois" long_string = <<EOF Here is a lo ...