4160: [Neerc2009]exclusive Access 2

Source: Internet
Author: User

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&GT;&GT;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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.