部分题集代码

B - This Sentence is False (6.3.2)(6.3相关题库)

#include <cstdio>  
#include <iostream>
#include <cstdlib>  
#include <cstring>  
#include <algorithm>  
using namespace std; 
#define maxn 2010    
void makeset(int f[], int a) 

    f[a]=a; 

int find(int f[], int a) 

    if (f[a]==a) return a; 
    return f[a]=find(f, f[a]); 

bool same(int f[], int a, int b) 

    return find(f,a)==find(f,b); 

void setunion(int f[], int a, int b) 

    f[find(f,a)]=find(f,b); 


bool sentence(int n, int f[], int i, int t, char tf) 

    if (tf=='f')  //i: Sentence t is false.  
    { 
  if (same(f, i, t) || same(f, i+n, t+n)) return false; 
  setunion(f, i, n+t); 
  setunion(f, t, n+i); 
 } 
 else            //i: Sentence t is true.  
 { 
  if (same(f, i, n+t) || same(f, t, n+i)) return false; 
  setunion(f, i, t); 
  setunion(f, i+n, t+n); 
 } 
 return true; 

void printans(int n, int f[]) 

 //循环每一个集合,取其 和 其对立集合对的元素多的集合  
    int root1, root2, cal1, cal2, ans=0; 
    bool v[maxn]={0}; 
    for (int i=0; i<n; i++) 
    { 
        if (same(f, i, n+i)) 
  { 
            printf("Inconsistent "); 
   return; 
  } 
        if (v[i]!=v[i+n]){ 
  {
    printf("BBBBBBBBBBBBBBBBBUUUUUUUUUUUUUUUGGGGGGGG ");  
            return; 
        } 
  if (!v[i] && !v[i+n]) 
        { 
            root1=find(f, i); 
            root2=find(f, i+n); 
            cal1=1; 
            cal2=0; 
            v[i]=v[i+n]=1; 
            for (int j=0; j<2*n; j++) 
                if (!v[j]) 
                { 
                    if (same(f, root1, j)){v[j]=1;if(j<n)cal1++;} 
                    else if (same(f, root2, j)){v[j]=1;if(j<n)cal2++;} 
     
                } 
    ans+=(cal1>cal2)?cal1:cal2; 
        } 
    } 
    printf("%d ", ans); 

int main() 

   
 int n, t; 
   
 int f[maxn]; 
 
 
 char str[100]; 
 
 char s1[20], s2[20], s3[20]; 
   
 bool inconsistent; 
   
 while (scanf("%d", &n)==1 && n) 
  
 { 
  
  gets(str); 
  
  
  for (int i=0; i<2*n; i++)       //n+i stands for !i  
   
   makeset(f, i); 
  
  inconsistent=false; 
       
  for (int i=0; i<n; i++) 
   
  { 
   scanf("%s %d %s %s", s1, &t, s2, s3); 
   if (!inconsistent && !sentence(n, f, i, t-1, s3[0])) 
    inconsistent=true; 
  } 
  if (inconsistent) 
   printf("Inconsistent "); 
  else 
   printans(n, f); 
 } 
 return 0; 

原文地址:https://www.cnblogs.com/zswbky/p/5432119.html