http://www.lydsy.com/JudgeOnline/problem.php?id=4160
Gives a non-directed graph, seeking the minimum value of the longest road of a given DAG.
Because the points are less, consider the state compression DP.
According to the Dilworth theorem, the longest chain length of a direction-free graph is equal to the minimum number of anti-chain divisions. So the problem is equivalent to dividing the point set into several sets, so that there is no edge inside each subset. After the transformation of the problem can be solved with state compression dynamic planning, preprocessing out Ok[code] Indicates whether there is an edge in the collection code, and then calculate F[code] represents the number of minimum subsets of the Set Code division, when the transfer enumeration subcode meet Ok[subcode] is true, with f[ Code-subcode]+1 Update the answer. The total time complexity is O (2nm+3n).
#include <cstdio>#include<cstring>#include<iostream>#include<algorithm>using namespacestd;Const intmaxn= the, maxc= -;BOOLok[1<<MAXN];intn,m,idx[maxc],g[maxn][maxn],f[1<<MAXN];voidinit () {scanf ("%d",&m); memset (IDX,-1,sizeof(IDX)); for(intI=1; i<=m;++i) { Chars1[2],s2[2]; scanf ("%s%s", S1,S2); if(idx[s1[0]-'A']==-1) idx[s1[0]-'A']=n++; if(idx[s2[0]-'A']==-1) idx[s2[0]-'A']=n++; g[idx[s1[0]-'A']][idx[s2[0]-'A']]=g[idx[s2[0]-'A']][idx[s1[0]-'A']]=1; }}voidWork () { for(intI=0;i<1<<n;++i) {Ok[i]=1; for(intj=0; j<n;++j) for(intk=0; k<n;++k)if(j!=k&& (I>>J) &1) && ((i>>k) &1) &&g[j][k]) {ok[i]=0; Break;} } memset (F, the,sizeof(f)); f[0]=0; for(intI=0;i<1<<n;++i) for(intt=i;t;t= (t1) &i)if(Ok[t]) f[i]=min (f[i],f[i^t]+1); printf ("%d\n", f[(1<<n)-1]-2);}intMain () {init (); Work (); return 0;}
My Code
4160: [Neerc2009]exclusive Access 2