[ZOJ1518][POJ1291] This Sentence is False

ZOJ: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1518

POJ: http://poj.org/problem?id=1291

题目大意:

有n个句子,编号为1..n,为

Sentence XXX is true/false.

问是否自相矛盾,如果是输出Inconsistent。否则输出最多多少句可以是对的?



解题思路:

使用并查集的方法。

对每个句子i,设置一个对立量!i (用n+i表示)。

(1)第i个句子: Sentence t is false.

    if (same(i, t) || same(!i, !t)) inconsistant

    setunion(i, !t)

    setunion(t, !i)


(2)第i个句子: Sentence t is true.

    if (same(i,!t) || same(!i, t)) inconsistant

    setunion(i, t)

    setunion(!i, !t)

并查集建立之后遍历每一个集合和其对立的集合,取元素较多的集合赋真值(注意这里统计元素的时候不考虑之前设置的对立量),真语句数量对应增加。


源代码:

/*
(1)i: Sentence t is false.
    if (same(i, t) || same(!i, !t)) inconsistant
    setunion(i, !t)
    setunion(t, !i)

(2)i: Sentence t is true.
    if (same(i,t)) continue
    if (same(i,!t) || same(!i, t)) inconsistant
    setunion(i, t) setunion(!i, !t)

*/

#include <cstdio>
#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\n");
            return;
        }
        if (v[i]!=v[i+n]){
            printf("BBBBBBBBBBBBBBBBBUUUUUUUUUUUUUUUGGGGGGGG\n"); 
            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\n", 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\n");
        else
            printans(n, f);
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值