UVa 1380 A Scheduling Problem
题目:
http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=41557
思路:
给出一个任务调度树,单向边u->v表示u必须在v之前完成,双向边u-v表示无所谓方向。
题目给出定理,首先dfs求得忽略无向边后的最长链点数k,那么问题就是判断是否可以通过无向边定向从而使得最长链点数不超过k。用dp的判断。
设f[i]表示以i为根的子树中所有的边定向后最长链点数不超过k条件下出边中最长链的最小值,g[i]表示以i为根的子树中所有的边定向后最长链点数不超过k条件下入边中最长链的最小值(最小值是最低限度,如果最小值都不可行那么问题不可行)。
两种情况(w为子节点):
- 与w的边中没有双向边:得出w.f_max与w.g_max判断与k的大小关系 如果超过k 回值INF否则回值w.f_max与w.g_max(max代表最长链)。没有选择的情况,已经定形。
- 与w的边中有双向边:定向双向边使满足k的限定下f与g尽量小。批量定向,求解f[u]的时候,将w按照f从小到大排序,依此枚举p,对于p将p之前的定向为出边,计算f[u]。同理求解g[u]。最后判断与k的关系。根据双向边定向选择最优结果。
代码:
// UVa1380 A Scheduling Problem
// Rujia Liu
#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 == ) //u->v
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] && !edges[i].empty()) { root = i; break; }
}
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; //ch != fa
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, ...}
maxg[s-] = sons[s-].g;
for(int k = s-; k >= ; k--)
maxg[k] = max(sons[k].g, maxg[k+]);
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+);
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 ;
}
Code from Rujia