有点难的一道题目,看题解+代码终于过了......
对有向边和无向边进行分开存放,然后dfs,找出最长的有向边的长度。接着找出“根节点”(该节点没有父节点,并且该节点存在孩子节点),然后从该节点开始进行动态规划:首先判断该节点的孩子节点是否为零,如果为零,那么该节点的up变量值和down变量值均为零(up和down的定义和紫书的f、g一样)。否则,依次遍历该节点的孩子节点,这里要多加一层判断,注意不能“走回头路”(见代码注释)。同时递归计算该节点的孩子节点的up和down的值,同时注意该节点和孩子节点的边的性质,如果该边是无向边,要注意保存,便于后续的判断。如果该边是指向该节点的注意要更新up的值,如果是该边是指向孩子节点的注意要更新该节点的down的值。在这一轮的操作全部结束的时候,要判断与该节点相连接的无向边的个数,如果与该节点相连的无向边的个数为0,那么就可以保存前面计算出的up和down的值,同时判断up和down之和是否超过了目前已知最长的无向边的长度,如果超过返回false,表示要多用一天的时间,否则直接返回false,同时注意在返回false之前要将up和down的值置为无穷大,便于上一层递归的判断。如果当前的节点的确存在和其他节点的无向边,那么就按照紫书上的思路,将所有无项边连接的子节点按照down从小到大的排序进行一轮处理,然后按照up的值从小到大的排序进行下一轮的处理,得出最终的结果,具体实现见如下代码:
#include<iostream>
#include<vector>
#include<string>
#include<set>
#include<stack>
#include<queue>
#include<map>
#include<algorithm>
#include<cmath>
#include<iomanip>
#include<cstring>
#include<sstream>
#include<cstdio>
#include<deque>
using namespace std;
struct edge{
int u, v, d;//d=0,undirect;d=1,down;d=2,up
edge(int a = 0, int b = 0, int c = 0){
u = a; v = b; d = c;
}
};
struct child{
int id,upp, downn;
child(int a = 0, int b = 0, int c = 0){
id = a, upp = b, downn = c;
}
};
bool compare_upp(const child a,const child b){
return a.upp < b.upp;
}
bool compare_down(const child a, const child b){
return a.downn < b.downn;
}
const int INF = 1000000000;
vector<edge> v[210];
int down[210];
int up[210];
int maxlen,root;
int total;
int parent[210];
bool init(){
for (int i = 0; i < 210; i++) v[i].clear();
memset(parent, 0, sizeof(parent));
memset(down, 0, sizeof(down));
memset(up, 0, sizeof(up));
int a;
bool last = true;
while (cin >> a&&a){
string s;
total = max(total, a);
last = false;
while (cin >> s&&s != "0"){
int l = s.size();
if (s[l - 1] >= 'a'&&s[l - 1] <= 'z'){
string s2 = s.substr(0,l-1);
stringstream is(s2);
int t;
is >> t;
total = max(total,t);
if (s[l - 1] == 'd'){
v[a].push_back(edge(a, t, 1));
v[t].push_back(edge(t, a, 2));
parent[t] = 1;
}
else{
v[a].push_back(edge(a, t, 2));
v[t].push_back(edge(t, a, 1));
parent[t] = 1;
}
}
else{
stringstream is(s);
int t;
is >> t;
total = max(total, t);
v[a].push_back(edge(a,t,0));
parent[t] = 1;
}
}
}
for (int i = 1; i <= total; i++){
if (parent[i] == 0 && v[i].size() != 0){
root = i;
break;
}
}
if (last) return false;
return true;
}
int dfs(int start){
int ans = 0;
for (int i = 0; i < v[start].size(); i++){
int ind = v[start][i].v;
if (v[start][i].d == 1) ans = max(ans, dfs(ind) + 1);
}
return ans;
}
bool dp(int start,int pre){
if (v[start].size() == 0){
up[start] = down[start] = 0;
return true;
}
vector<child> children;
int up_s = 0, down_s = 0;
for (int i = 0; i < v[start].size(); i++){
int ind = v[start][i].v;
if (ind == pre) continue;
dp(ind, start);
int d = v[start][i].d;
if (d == 0) children.push_back(child(ind, up[ind], down[ind]));
else if (d == 2) up_s = max(up_s, up[ind]+1);
else down_s = max(down_s, down[ind]+1);
}
if (children.empty()){
up[start] = up_s, down[start] = down_s;
if (up_s + down_s > maxlen) up[start] = down[start] = INF;
return up[start] < INF;
}
up[start] = INF, down[start] = INF;
int amount = children.size();
sort(children.begin(),children.end(),compare_upp);
int max_d[210];
max_d[amount - 1] = children[amount - 1].downn;
for (int i = amount - 2; i >= 0; i--){
max_d[i] = max(children[i].downn, max_d[i + 1]);
}
for (int i = 0; i <= amount; i++){
int t1 = up_s, t2 = down_s;
if (i>0) t1 = max(t1, children[i - 1].upp+1);
if (i < amount) t2 = max(t2, max_d[i]+1);
if (t1 + t2 <= maxlen) up[start] = min(up[start], t1);
}
sort(children.begin(), children.end(), compare_down);
int max_u[210];
max_u[amount - 1] = children[amount - 1].upp;
for (int i = amount - 2; i >= 0; i--){
max_u[i] = max(children[i].upp,max_u[i+1]);
}
for (int i = 0; i <= amount; i++){
int t1 = up_s, t2 = down_s;
if (i<amount) t1 = max(t1, max_u[i] + 1);
if (i>0) t2 = max(t2, children[i-1].downn + 1);
if (t1 + t2 <= maxlen) down[start] = min(down[start], t2);
}
return up[start] < INF;
}
int main(){
total = 0;
while (init()){
maxlen = 0;
for (int i = 1; i <= total; i++){
maxlen = max(maxlen, dfs(i));
}
if (dp(root, -1)) cout << maxlen + 1 << endl;
else cout << maxlen + 2 << endl;
total = 0;
}
return 0;
}