poj1417 with right and check collection +0/1 backpack

Source: Internet
Author: User

Test instructions: There is a few gods and demons living on an island, and the number of gods and demons known, and now known that God is always telling the truth, demons are always telling lies, there are N questioning, asking a certain God or demon (identity unknown), the question is whether a god or a demon, according to their answer, ask if you can determine which is God and

For these problems, we only need to find that if the answer is a demon, then you can tell that the two are not the same race, and if the answer is God, then the two are the same race, then you can use the right to combine these gods and demons, and then record the two different how many, So when all the queries are processed, we can get a set of collections, each with its two races, but for each set, we don't know which one is God and which is the demon, and that's when we need to use the 0/1-pack method, Dp[i][j] represents the process to the first A set of total J numbers (gods or demons) of the number of cases, it from dp[i-1][j-num1[i]] and Dp[i-1][j-num2[i]] ", here Num1, Num2 is a and a set of two people. Transfer it by the way to record where it is transferred from the value, to facilitate the final output. In this way, it is possible to do so as long as the number of gods or the number of demons in the final processing of all collections is exactly the case. Follow the recorded transfer traverse back to output all the solutions on the line. Adding a small optimization is that when the two quantities of a set are equal, they can be judged directly, because the two numbers are equivalent.

1#include <stdio.h>2#include <string.h>3 4 intfa[605],num[605],n,num1[605],num2[605],p[605],dp[605][605],fat[605][605],num3[605];5 intp1,p2;6 Chars[5];7 8 intMmax (intAintb) {9     returnA>b?a:b;Ten } One  A voidinit () { -      for(intI=1; i<=p1+p2;i++){ -fa[i]=i; thenum1[i]=1; -     } -memset (NUM,0,sizeof(num)); -memset (num2,0,sizeof(num2)); + } -  + intFindintx) { A     intR=x,t1,t2,c=0; at      while(r!=Fa[r]) { -c+=Num[r]; -R=Fa[r]; -     } -      while(r!=x) { -t1=Fa[x]; int2=c-Num[x]; -num[x]=c%2; tofa[x]=R; +x=T1; -C=T2; the     } *     returnR; $ }Panax Notoginseng  - intMain () { the      while(SCANF ("%d%d%d", &AMP;N,&AMP;P1,&AMP;P2)!=eof&&n!=0|| p1!=0|| p2!=0){ +         inti; A init (); the          for(i=1; i<=n;i++){ +             inta,b,v; -scanf"%d%d%s",&a,&b,s); $             if(s[0]=='y') v=0; $             Elsev=1; -             intX=find (a), y=find (b); -             if(x!=y) { theNum[x]= ((Num[b]+v-num[a])%2+2)%2; -                 if(num[x]==0){Wuyinum1[y]+=Num1[x]; thenum2[y]+=Num2[x]; -                 } Wu                 Else{ -num1[y]+=Num2[x]; Aboutnum2[y]+=Num1[x]; $                 } -fa[x]=y; -             } -         } A         BOOLf=1; +         if(P1==P2) printf ("no\n"); the         Else{ -             intCnt=0, J; $              for(i=1; i<=p1+p2;i++){ the                 if(fa[i]==i) { thep[++cnt]=i; the                     if(Num1[i]==num2[i]) f=0; the                 } -             } in             if(f) { theMemset (DP,0,sizeof(DP)); thedp[0][0]=1; About                  for(i=1; i<=cnt;i++){ the                      for(j=0; j<=p1;j++){ the                         if(dp[i-1][j]) { thedp[i][j+num1[p[i]]]+=dp[i-1][j]; +fat[i][j+num1[p[i]]]=J; -dp[i][j+num2[p[i]]]+=dp[i-1][j]; thefat[i][j+num2[p[i]]]=J;Bayi                         } the                     } the                 } -                 if(dp[cnt][p1]!=1) f=0; -             } the             if(!f) printf ("no\n"); the             Else{ the                 intFather=P1; the                  for(i=cnt;i>=1; i--){ -                     if(father-fat[i][father]==Num1[p[i]]) { thenum3[p[i]]=0; the                     } the                     Elsenum3[p[i]]=1;94Father=Fat[i][father]; the                 } the                  for(i=1; i<=p1+p2;i++){ the                     intx=find (i);98                     if(Num[i]==num3[x]) printf ("%d\n", i); About                 } -printf"end\n");101             }102         }103     }104     return 0; the}
View Code

poj1417 with right and check collection +0/1 backpack

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.