种类并查集 发现它 抓住它

tag[i] 表示 i 结点 与其父节点是否在同一个集合中(表示的是一种相对关系)

GetFather时:压缩路径,同时更新tag[i]

合并时:更新tag[father[i]]

#include <stdio.h>
#include <iostream>
#include <stack>
#include <string>
#include <set>
#include <memory.h>
#include <math.h>
#include <algorithm>
#include <queue>
using namespace std;
int father[100005], N, M, T, tag[100005];
void Init()
{
    for( int i = 1; i <= N; i++ )
        father[i] = i;
    for( int i = 1; i <= N; i++ )
        tag[i] = 0;
}
int GetFather( int i )
{
    if( i == father[i] )
        return i;
    int tmp = GetFather( father[i] );
    tag[i] = (tag[i] + tag[father[i]]) % 2;
    father[i] = tmp;
    return tmp;
}
int main()
{
    scanf("%d", &T);
    while( T-- )
    {
        scanf("%d %d", &N, &M);
        Init();
        char c;
        int tmpa, tmpb;
        while( M-- )
        {
            cin >> c >> tmpa >> tmpb;
            int fa = GetFather(tmpa);
            int fb = GetFather(tmpb);
            if( c == 'A' )
            {
                if( fa != fb )
                {
                    cout << "Not sure yet." << endl;
                }
                else
                {
                    if( tag[tmpa] == tag[tmpb] )
                    {
                        cout << "In the same gang." << endl;
                    }
                    else
                        cout << "In different gangs." << endl;
                }
            }
            else
            {
                father[fa] = fb;
                tag[fa] = (tag[tmpa] - tag[tmpb] + 1)%2;
            }
        }
    }
    return 0;
}


阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页