POJ - 1733 Parity game

题目链接:http://poj.org/problem?id=1733
题意:给定n长的字符串,m个假设,输出第几个假设是错误的,若都是正确的,输出m。
带全并查集裸模板,然后因为这题只有奇数还是偶数这两个情况,所以直接取两个数值就好了。

下图是带全并查集的理解,向量做法。

图片.png

其实只要固定一个方向就可以了,不需要分情况。

#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstring>
using namespace std;

const int maxn = 1e6+5;

int father[maxn];
int sum[maxn];
int dis[maxn];

int find(int x)
{
    if(x != father[x])
    {
        int f = father[x];
        father[x] = find(father[x]);
        sum[x] += sum[f];
    }
    return father[x];
}

int main()
{
    int m;
    cin >> m;
    for(int i = 1; i <= m; i++)
    {
        father[i] = i;
        sum[i] = 0;
        dis[i] = 1;
    }
    while(m --)
    {
        char op;
        int x, y;
        cin >> op;
        if(op == 'M')
        {
            cin >> x >> y;
            int fx = find(x);
            int fy = find(y);
            if(fx != fy)
            {
                father[fx] = fy;
                sum[fx] += dis[fy];
                dis[fy] += dis[fx];
            }    
        }
        else
        {
            cin >> x;
            int fx = find(x);
            cout << sum[x] << endl;    
        } 
    }
    return 0;    
}

猜你喜欢

转载自blog.csdn.net/qq_38295645/article/details/89916997