#include #include #include #include #include #include using namespace std; const int maxn=200+5; const int oo=1e9; struct Edge { int u, v, d;//d=1 u->v, d=2 v->u, d=0 u-v Edge(int u=0,int v=0,int d=0):u(u),v(v),d(d) { } }; vector edges[maxn]; int n,root,maxlen,f[maxn],g[maxn],have_father[maxn]; int dfs(int u) { int ans=0,sz=edges[u].size(); for(int i=0;i>a&&a) { string str; have_data=true; if(a>n) n=a;//update the maximum node while(cin>>str&&str!="0") { int len=str.length(); char type=str[len-1]; if(type=='d'||type=='u') str=str.substr(0,len-1); stringstream ss(str); ss>>b;// b is a's son if(b>n) n=b; have_father[b]=true; if(type=='d') { edges[a].push_back(Edge(a,b,1));//forward edges[b].push_back(Edge(b,a,2));//backward } else if(type=='u') { edges[a].push_back(Edge(a,b,2)); edges[b].push_back(Edge(b,a,1)); } else edges[a].push_back(Edge(a,b,0)); } } if(have_data)//find the root of the graph for(int i=1;i<=n;i++) if(!have_father[i]&&!edges[i].empty()) { root=i; break; } return have_data; } struct State { int w,f,g; State(int w=0,int f=0,int g=0):w(w),f(f),g(g) { } }; bool cmp_f(const State& w1,const State& w2) { return w1.f sons; int f0=0,g0=0,sz=edges[i].size(); //f'[i]=max{f[w]|w->i}+1,g'[i]=max{g[w]|i->w}+1 for(int k=0;kmaxlen) f[i]=g[i]=oo; return f[i]=0;k--) maxg[k]=max(sons[k].g,maxg[k+1]); for(int p=0;p<=s;p++) { int ff=f0,gg=g0; if(p>0) ff=max(ff,sons[p-1].f+1); if(p=0;k--) maxf[k]=max(sons[k].f,maxf[k+1]); for(int p=0;p<=s;p++) { int ff=f0,gg=g0; if(p>0) gg=max(gg,sons[p-1].g+1); if(p