uva1380 A Scheduling Problem
按紫书来
注意这道题的题目给了很大的方便,就相当于验证k是不是答案,不是的话就是k+1
#include<iostream>
#include<string>
#include<cstring>
#include<sstream>
#include<vector>
#include<algorithm>
using namespace std; const int maxn = + ;
const int INF = ; struct Edge {
int u, v, d; // d=1 means u->v, d=2 means v->u, d=0 means u-v
Edge(int u=, int v=, int d=):u(u),v(v),d(d){}
}; vector<Edge> edges[maxn];
int n, root, maxlen, f[maxn], g[maxn], have_father[maxn]; // maximal length of a DIRECTED path starting from u
int dfs(int u) {
int ans = ;
for(int i = ; i < edges[u].size(); i++) {
int v = edges[u][i].v;
if(edges[u][i].d == )
ans = max(ans, dfs(v)+);
}
return ans;
} bool read_data() {
bool have_data = false;
int a, b;
n = ;
for(int i = ; i < maxn; i++) edges[i].clear();
memset(have_father, , sizeof(have_father)); while(cin >> a && a){
string str;
have_data = true;
if(a > n) n = a;
while(cin >> str && str != ""){ //一组数据
int len = str.length();
char dir = str[len-];
if(dir == 'd' || dir == 'u') str = str.substr(, len-);
stringstream ss(str);
ss >> b; // b is a's son
if(b > n) n = b;
have_father[b] = ;
if(dir == 'd'){
edges[a].push_back(Edge(a, b, )); // forward
edges[b].push_back(Edge(b, a, )); // backward
}else if(dir == 'u'){
edges[a].push_back(Edge(a, b, ));
edges[b].push_back(Edge(b, a, ));
}else{
edges[a].push_back(Edge(a, b, )); // it's a rooted tree, so we don't store edge to father
}
}
}
if(have_data) {
for(int i = ; i <= n; i++)
if(!have_father[i]) { root = i; break; } //找一个root //if(!have_father[i] && !edges[i].empty())
}
return have_data;
} struct UndirectedSon {
int w, f, g;
UndirectedSon(int w=, int f=, int g=):w(w),f(f),g(g){}
}; bool cmp_f(const UndirectedSon& w1, const UndirectedSon& w2) {
return w1.f < w2.f;
} bool cmp_g(const UndirectedSon& w1, const UndirectedSon& w2) {
return w1.g < w2.g;
} // calculate f[i] and g[i]
// return true iff f[i] < INF
// f[i] is the minimal length of the longest "->u" path if all subtree paths have length <= maxlen
// g[i] is the minimal length of the longest "u->" path if all subtree paths have length <= maxlen
// f[i] = g[i] = INF if "all subtree paths have length <= maxlen" cannot be satisfied
bool dp(int i, int fa) {
if(edges[i].empty()) {
f[i] = g[i] = ;
return true;
}
vector<UndirectedSon> sons;
int f0 = , g0 = ; // f'[i] and g'[i] for directed sons // let f'[i] = max{f[w] | w->i}+1, g'[i] = max{g[w] | i->w}+1
// then we should change some undirected edges to ->u or u-> edges so that f'[i]+g'[i] <= maxlen
// then f[i] is the minimal f'[i] under this condition, and g[i] is the minimal g'[i]
for(int k = ; k < edges[i].size(); k++) {
int w = edges[i][k].v;
if(w == fa) continue;
dp(w, i);
int d = edges[i][k].d;
if(d == ) sons.push_back(UndirectedSon(w, f[w], g[w]));
else if(d == ) g0 = max(g0, g[w]+);
else f0 = max(f0, f[w]+);
}
// If there is no undirected edges, we're done
if(sons.empty()) {
f[i] = f0; g[i] = g0;
if(f[i] + g[i] > maxlen) { f[i] = g[i] = INF; }
return f[i] < INF;
} f[i] = g[i] = INF; // to calculate f[i], we sort f[w] of undirected sons in increasing order and make first p edges to w->i
// then we calculate f'[i] and g'[i], check for f'[i]+g'[i] <= maxlen and update answer
int s = sons.size(); sort(sons.begin(), sons.end(), cmp_f);
int maxg[maxn]; // maxg[i] is max{sons[i].g, sons[i+1].g, ...} //按f从小到大排
maxg[s-] = sons[s-].g;
for(int k = s-; k >= ; k--)
maxg[k] = max(sons[k].g, maxg[k+]); //记录k之后最长的g[w],用于排序
for(int p = ; p <= sons.size(); p++) {
int ff = f0, gg = g0; //原来最长的有向边
if(p > ) ff = max(ff, sons[p-].f+);
if(p < sons.size()) gg = max(gg, maxg[p]+);
if(ff + gg <= maxlen) f[i] = min(f[i], ff);
} // g[i] is similar
sort(sons.begin(), sons.end(), cmp_g);
int maxf[maxn]; // maxf[i] is max{sons[i].f, sons[i+1].f, ...}
maxf[s-] = sons[s-].f;
for(int k = s-; k >= ; k--)
maxf[k] = max(sons[k].f, maxf[k+]);
for(int p = ; p <= sons.size(); p++) {
int ff = f0, gg = g0;
if(p > ) gg = max(gg, sons[p-].g+); //比p小的f(w)变成w->i,不会让f[i]变大,但可能让g[i]变小
if(p < sons.size()) ff = max(ff, maxf[p]+);
if(ff + gg <= maxlen) g[i] = min(g[i], gg);
} return f[i] < INF;
} int main() {
while(read_data()) {
maxlen = ;
for(int i = ; i <= n; i++) maxlen = max(maxlen, dfs(i));
// Note: the problem asks for the number of nodes in path, but all the "lengths" above mean "number of edges"
if(dp(root, -)) cout << maxlen+ << "\n";
else cout << maxlen+ << "\n";
}
return ;
}
uva1380 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_ ...
- UVA 1380 A Scheduling Problem
题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...
- [UVALive 3683] A Scheduling Problem
图片加载可能有点慢,请跳过题面先看题解,谢谢 题目给出了一个信息:答案是有向边最长路 \(k\) 的值或者是 \(k+1\) 的值 那么题目就变成了:求是否有一种给无向边定向的方案,使得在以有向边最长 ...
- 题解 AT4170 【[ABC103A] Task Scheduling Problem】
翻译 有 \(3\) 个正整数 \(a\).\(b\).\(c\),请你输出这 \(3\) 个数中的最大值 \(-\) 最小值的差. 分析 求最大值 \(-\) 最小值的差,我们自然可以使用 for ...
- POJ 1325 Machine Schedule——S.B.S.
Machine Schedule Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 13731 Accepted: 5873 ...
- *HDU1150 二分图
Machine Schedule Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- Dynamic Programming
We began our study of algorithmic techniques with greedy algorithms, which in some sense form the mo ...
- poj 1325 Machine Schedule
Time Limit: 1000 MS Memory Limit: 10000 KB 64-bit integer IO format: %I64d , %I64u Java class name ...
随机推荐
- Spring Data JPA 和MyBatis比较
现在Dao持久层的解决方案中,大部分是采用Spring Data JPA或MyBatis解决方案,并且传统企业多用前者,互联网企业多用后者. Spring Data JPA 是Spring Data ...
- 【转】 IntelliJ IDEA 中 Project 和 Module 的概念及区别
原文地址:https://blog.csdn.net/qq_35246620/article/details/65448689 在 IntelliJ IDEA 中,没有类似于 Eclipse 工作空间 ...
- 更改ssh远程登录端口.sh
#!/bin/bash #liu_dong sed -i "s/\#Port 22/Port 31961/g" /etc/ssh/sshd_config sed -i " ...
- 斯坦福CS231n—深度学习与计算机视觉----学习笔记 课时10
课时10 神经网络训练细节part1(上) 没有大量的数据也不会有太多影响,只需要找一个经过预训练的卷积神经网络然后进行调整 从数据集中抽样一小批数据, 将数据运入卷积神经网络中来计算损失值 通过反向 ...
- Linux2.6 内核中结构体初始化(转载)
转自:http://hnniyan123.blog.chinaunix.net/uid-29917301-id-4989879.html 在Linux2.6版本的内核中,我们经常可以看到下面的结构体的 ...
- sql server编写一个语句脚本自动清空各表数据以初始化数据库
问题:有时已有项目要移植,例如原来在广州地区使用的某系统,突然说惠州那边也要用这套一样的系统.或者,在demo环境下弄了一些测试数据.然后要清空全部表数据.如果表比较多的话,逐个表手工编写脚本就太麻烦 ...
- 洛谷P2585 [ZJOI2006]三色二叉树(树形dp)
传送门 设$dp[u][i]$表示点$u$颜色为$i$时最多(最少)的绿点个数(这里用$0$表示绿点) 然后直接用树形dp就可以了 记得把情况讨论清楚 //minamoto #include<b ...
- 解决eNSP路由器AR启动失败错误代码40,交换机正常的问题
问题描述 eNSP昨晚正常使用,今天上午出现问题:AR路由器启动失败,错误代码40.但交换机可正常启动. eNSP版本:eNSP V100R002C00B510 Setup.exe 操作系统:Wind ...
- 使用pytesseract识别验证码,报错WindowsError: [Error 2]
问题现象: 按照网上的方式进行代码编写,使用pytesseract模块,然后导入指定图片进行解析,报错WindowsError: [Error 2] 问题原因: 源代码里面的路径设置错误,这里有一个坑 ...
- 如何访问google?
1:浏览器打开 uzer.me 网站(建议:火狐,不行就谷歌) 2:点击注册,注册一个账号 3:登录账号,进入如下界面,选择火狐浏览器 4:这样就可以进行国外的网站访问了