[UVALive 3683] A Scheduling Problem
图片加载可能有点慢,请跳过题面先看题解,谢谢





题目给出了一个信息:答案是有向边最长路 \(k\) 的值或者是 \(k+1\) 的值
那么题目就变成了:求是否有一种给无向边定向的方案,使得在以有向边最长路的起点为根的树(其实也并不是一棵树,只是当成一棵树来处理)内,点的深度在 \([1,k]\) 内
如果存在这样的方案,答案为 \(k\) ,否则答案为 \(k+1\)
\(k\) 的值和有向边最长路的起点我们可以暴力 \(dfs\) 在 \(O(n)\) 的时间内求出来
关键是怎么求方案?
$
$
这样设:\((bool)f[x][dep]\) 为,到 \(x\) 这个点深度为 \(dep\) 时,以 \(x\) 为根的子树是否可行
对于与 \(x\) 相连的边我们可以这样转移:
- 初值,\(f[x][dep]=1\);
- \(x\)->\(son\) 或者 \(x\)--\(son\),--->\(f[son][dep+j]\);
- \(son\)->\(x\) 或者 \(x\)--\(son\),--->\(f[son][dep-j]\);
其中,\(1\leq j,dep-j\) ;\(dep+j\leq k\)
\(f[x][dep]=f[x][dep]\wedge(f[son]有一个为 1 )\)
$
$
//made by Hero_of_Someone
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#define il inline
#define RG register
using namespace std;
int n,u,k,Dis[210];
bool vis[210][210],f[210][210];
int num,head[210],nxt[410],to[410],w[410];
il void add(int u,int v,int d){ //d: 0,u->v;1,u--v;2,v->u;
nxt[++num]=head[u];to[num]=v;w[num]=d;head[u]=num;
}
il void init(){
num=0; memset(head,0,sizeof(head));
n=u; char s[10];
do{
n=max(n,u);
while(scanf("%s",s)==1){
if(s[0]=='0') break;
RG int v=0,len=strlen(s);
RG char ch='n';
for(int i=0;i<len;i++)
if(s[i]=='u'||s[i]=='d') ch=s[i];
else v*=10,v+=s[i]-'0';
n=max(n,v);
if(ch=='n') add(u,v,1),add(v,u,1);
if(ch=='d') add(u,v,0),add(v,u,2);
if(ch=='u') add(u,v,2),add(v,u,0);
}
scanf("%d",&u);
}while(u);
}
il void pre(int x){
if(Dis[x]) return ; Dis[x]=1;
for(int i=head[x];i;i=nxt[i]){
if(w[i]) continue; pre(to[i]);
Dis[x]=max(Dis[x],Dis[to[i]]+1);
}
}
il bool dfs(int fa,int x,int dep){
if(vis[x][dep]) return f[x][dep];
vis[x][dep]=f[x][dep]=1;
for(int i=head[x];i;i=nxt[i]){
RG int v=to[i]; if(v==fa) continue;
RG bool flag=0;
if(!w[i]||w[i]==1)
for(int j=1;(!flag)&&dep+j<=k;j++)
flag=dfs(x,v,dep+j);
if(w[i]==2||w[i]==1)
for(int j=1;(!flag)&&(dep-j);j++)
flag=dfs(x,v,dep-j);
f[x][dep]=f[x][dep]&flag;
}
return f[x][dep];
}
il void work(){
memset(Dis,0,sizeof(Dis));
for(int i=1;i<=n;i++) pre(i);
RG int Max=1; for(int i=2;i<=n;i++) if(Dis[i]>Dis[Max]) Max=i;
k=Dis[Max]; memset(vis,0,sizeof(vis));
if(dfs(0,Max,1)) printf("%d\n",k);
else printf("%d\n",k+1);
}
int main(){ while(scanf("%d",&u)&&u){ init(); work(); } return 0; }
[UVALive 3683] A Scheduling Problem的更多相关文章
- 【暑假】[深入动态规划]UVa 1380 A Scheduling Problem
UVa 1380 A Scheduling Problem 题目: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=41557 ...
- 【UVA 1380】 A Scheduling Problem (树形DP)
A Scheduling Problem Description There is a set of jobs, say x1, x2,..., xn <tex2html_verbatim_ ...
- UVALive 7457 Discrete Logarithm Problem (暴力枚举)
Discrete Logarithm Problem 题目链接: http://acm.hust.edu.cn/vjudge/contest/127401#problem/D Description ...
- Gym 101194A / UVALive 7897 - Number Theory Problem - [找规律水题][2016 EC-Final Problem A]
题目链接: http://codeforces.com/gym/101194/attachments https://icpcarchive.ecs.baylor.edu/index.php?opti ...
- UVALive 6909 Kevin's Problem 数学排列组合
Kevin's Problem 题目连接: https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid ...
- uva1380 A Scheduling Problem
按紫书来注意这道题的题目给了很大的方便,就相当于验证k是不是答案,不是的话就是k+1 #include<iostream> #include<string> #include& ...
- UVA 1380 A Scheduling Problem
题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...
- UVALive - 7041 G - The Problem to Slow Down You
题意:求两个串的公共回文子串个数 题解:建两个回文自动机,从0和1各跑一边就是答案了,因为对于回文自动机来说,从头开始dfs就能找出该字符串的所有回文串 //#pragma GCC optimize( ...
- UVALive - 3521 Joseph's Problem (整除分块)
给定$n,k$$(1\leqslant n,k\leqslant 10^9)$,计算$\sum\limits _{i=1}^nk\: mod\:i$ 通过观察易发现$k\%i=k-\left \lfl ...
随机推荐
- Unity在OpenGL模式下Shader编译报错
报错信息 GLSL compilation failed: 0(21) : error C7528: OpenGL reserves names containing '__' 双击报错VS自动打开V ...
- XSS-DVWA
1.反射型 LOW: 没有过滤,直接键入PAYLOAD 查看源码 这里没有任何过滤,使用htmlspecialchars()过滤 结果不弹窗 MEDIUM: LOW等级的方法不奏效了 观察输出可能是过 ...
- 第七周psp例行报告
本周psp 本周进度条 代码累积折线图 博文字数累积折线图 饼状图
- Java jdbc链接 mySQL 写的crud
1.JDBC(Java Data Base Connectivity java数据库连接)概念: 是一种用于执行SQL语句的Java API,可以为多种关系数据库提供统一访问,它由一组用Java语言编 ...
- 再学HTML之一
Html 超文本标记语言 什么是html? HTML 是用来描述网页的一种语言. HTML 指的是超文本标记语言 (Hyper Text Markup Language) HTML 不是一种编程语言, ...
- C语言:一个能自动生成小学四则运算题目的程序
完成这个程序,半个小时内完成了,这个程序,可以自动生成小学简易的四则运算,提供菜单让用户选择,然后判断加减乘除,判断答对答错的题目个数,用户同时也可以重新选择继续答题或重新选择或退出程序. 源程序: ...
- Unity删除或更换天空盒
Unity版本:5.6.2 点击菜单栏Window-->Lighting-->Settings 在弹出的窗口中,设置Skybox Material选项,从原来的默认天空盒更换成别的,或者选 ...
- 今年暑假要AC
今年暑假要AC 在这个大学的第一个的暑假,谁不想回去high呢~ 但是,这是不行的,还没有AC,你能回去吗?高三那年的暑假怎么玩的,现在补回来吧...有规模有计划有氛围的学习就是:优点多效率好激情足~ ...
- 蜗牛慢慢爬 LeetCode 20. Valid Parentheses [Difficulty: Easy]
题目 Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the i ...
- oracle greatest(),least( ) ,coalesce()
--场景1: select pt, greatest(wm), least(wm) from (select s.producttype pt, wm_concat(s.productid) wm f ...