Main topic:
A couple married and invited N couples to attend the wedding. There is a very long table, people can only sit on both sides of the table, but also to meet the following requirements: 1. Each couple can not sit on the same side of the 2.N couples may have adultery relationship (including male, female, female), adultery can not sit at the same time opposite the bride, may sit separately, you can sit on the bride side. If there is a workable scheme, output the same side of the bride as the person.
Here because the input is a string, note that the number may not be just a single digit, to
while (IsDigit (S1[index1])) i = i*10+ (s1[index1++]-' 0 ');
while (IsDigit (S2[index2])) j = j*10+ (s2[index2++]-' 0 ');
There's no reason for me to find it, it's been wrong many times
The others, according to the 2i, the husband sits opposite the bride, 2i+1 said I was sitting opposite the bride
The first mark of the groom's corresponding number is set to true, because he is bound to sit opposite the bride
The last sign of false is to sit on the bride's side.
1#include <cstdio>2#include <cstring>3#include <vector>4#include <ctype.h>5#include <algorithm>6 using namespacestd;7 #defineN 3008 intN, M, S[n], C;9 BOOLMark[n];Ten Chars1[Ten], s2[Ten]; Onevector<int>G[n]; A - voidInit () - { thememset (Mark,0,sizeof(Mark)); - for(intI=0; i<n*2; i++) g[i].clear (); - } - + BOOLDfsintu) - { + if(Mark[u])return true; A if(mark[u^1])return false; atMark[u] =true; -S[c++] =u; - for(intI=0; I<g[u].size (); i++) - if(!dfs (G[u][i]))return false; - return true; - } in - BOOLSolve () to { +mark[0] =true;//must be sitting opposite the bride, so the groom is true - for(intI=0; i<2*n; i+=2){ the if(!mark[i] &&!mark[i^1]){ *c =0; $ if(!DFS (i)) {Panax Notoginseng while(c) mark[s[--c]] =false; - if(!dfs (i^1))return false; the } + } A } the return true; + } - $ voidAdd_clause (intIintPintJintq) $ { - intm =2*i+p, n =2*j+Q; -G[m].push_back (n^1); theG[n].push_back (m^1); - }Wuyi the intMain () - { Wu //freopen ("In.txt", "R", stdin); - while(SCANF ("%d%d", &n, &m), n+m) About { $ init (); - while(m--){ -scanf"%s%s", S1, S2); - intI, J, p, Q; Ai =0, j =0; + intIndex1 =0, Index2 =0; the while(IsDigit (s1[index1])) i = i*Ten+ (s1[index1++]-'0'); - while(IsDigit (S2[index2])) j = j*Ten+ (s2[index2++]-'0'); $p = s1[index1]=='h'?0:1, q = s2[index2]=='h'?0:1; the add_clause (I, P, J, Q); the } the if(!solve ()) puts ("Bad luck"); the Else{ - intFlag =0; in for(intI=2; i<n*2; i++){ the if(!Mark[i]) { the if(flag) printf ("%d%c", i/2, i&1?'W':'h'); About Elseprintf"%d%c", i/2, i&1?'W':'h'); theflag=1; the } the } +Puts""); - } the }Bayi return 0; the}
POJ 3648 2-sat