Poj1291 this sentence is false (query set | hash)

Source: Internet
Author: User

From: http://blog.csdn.net/svitter


Before writing:

Recently, I feel that I have done a lot of questions and checked them. It is found that A is right to B, and that games like who is telling the truth, who is telling the truth, are basically a collection query. Make a record to prevent future forgetting.


Question:

N sentences are given in the question. The number starts from 1. Every sentence is in the form of sentence $ num is true/false.

The number of vertices that output the most vertices.

Input and Output Analysis:

Note that the format of scanf cannot be "sentence % d is % s. The specific cause is unknown. You can use % s to constantly read unused variables to eliminate characters in the cache.

Algorithm Data Structure Analysis:

First, I, I + N is used to determine the right and wrong. If there is a paradox, continue to absorb the buffer, but do not perform calculations.

For details, refer to: poj2492 A Bug's Life (and query set)

After finding the corresponding set, count the number of num [getroot (I)] and add them together.

Problem:

At first, I also counted getroot (I + n). Later, I referred to the code of the great god. Considering that I + N is an opposite situation of I, there is no need to calculate it.

The confrontation with I must be included in the case except I, so there is no need to make statistics.

In the process of traversing the num array, take the larger group of num [getroot (I)] and num [getroot (I + n. That is, take the I or ~ I. Because there is no paradox, I and ~ I must have been established.


AC code:
//author: svtter//#include <iostream>#include <stdio.h>#include <string.h>#include <vector>#include <map>#include <algorithm>#include <queue>#include <cmath>#define INF 0xffffffusing namespace std;int root[2010];int n;int num[2010];int vis[2010];void init(){    for(int i = 0; i <= n*2; i++)        root[i] = i;}int getRoot(int i){    if(i == root[i])        return i;    return root[i] = getRoot(root[i]);}int a, b;void Merge(int i, int j){    a = getRoot(i);    b = getRoot(j);    if(a == b)        return;    root[a] = b;}bool jud(int i, int j){    return getRoot(i) == getRoot(j);}void solve(){    int i, ans;     ans = 0;    memset(num, 0, sizeof(num));    memset(vis, 0, sizeof(vis));    for(i = 1; i <= n; i++)    {        num[getRoot(i)] ++;           // num[getRoot(i+n)]++;    }        for(i = 1; i <= n; i++)    {        if(vis[getRoot(i)] || vis[getRoot(i+n)])            continue;        ans += max(num[getRoot(i)], num[getRoot(i+n)]);        vis[getRoot(i)] = vis[getRoot(i+n)] = 1;    }    printf("%d\n", ans);}int main(){    char ju[10];    int i, t;    bool paradox;    while(~scanf("%d", &n))    {        if(n == 0)            break;        paradox = false;        //input        init();        for(i = 1; i <= n; ++i)        {            scanf("%s", ju);            scanf("%d", &t);            scanf("%s", ju);            scanf("%s", ju);            if(paradox)                continue;            if(ju[0] == 'f')            {                if(jud(i, t) || jud(i+n, t+n))                    paradox = 1;                else                {                    Merge(i, t+n);                    Merge(t, i+n);                }            }            else            {                if(jud(i, t+n) || jud(t, i+n))                    paradox = 1;                else                {                    Merge(i, t);                    Merge(i+n, t+n);                }            }        }        if(paradox)            printf("Inconsistent\n");        else            solve();    }    return 0;}


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.