P1892 [BOI2003]团伙-(扩展域并查集-我很烦)

总结

这个题,把我弄得要哭了。如果有10分,我觉得9分这个题意有问题,要么就是数据有问题。
数据
4
3
E 1 2
E 3 4
F 1 3
输出是2还是3
AC代码输出3,为啥不是2
我自己的常识认为2的敌人的敌人应该是朋友。

跪求大佬指正

未AC代码-40

/*
                ____________    ______________       __
               / _________  /\ /_____   _____/\     / /\
              / /\       / /  \\    /  /\    \ \   / /  \
             / /  \_____/ /   / \__/  /  \____\/  / /   /
            / /   /    / /   /    /  /   /       / /   /
           / /   /    / /   /    /  /   /       / /   /
          / /   /    / /   /    /  /   /       / /   /
         / /___/____/ /   /    /  /   /       / /___/________
        /____________/   /    /__/   /       /______________/\
        \            \  /     \  \  /        \              \ \
         \____________\/       \__\/          \______________\/
           ___       ___               ___    __________
          /  /\     /  /\             /  /\  /_______  /\
         /  /__\___/  /  \           /  /  \ \      /  /  \
        /____    ____/   /          /  /   /  \____/  /   /
        \   /   /\   \  /          /  /   /       /  /   /
         \_/   /  \___\/ ___      /  /   /       /  /   /
          /   /   /     /  /\    /  /   /       /  /   /
         /   /   /     /  /__\__/  /   /       /  /___/____
        /___/   /     /___________/   /       /___________/\
        \   \  /      \           \  /        \           \ \
         \___\/        \___________\/          \___________\/

          A CODE OF CBOY

*/
#include<bits/stdc++.h>
//typedef long long ll;
//#define ull       unsigned long long
//#define int       long long
#define F           first
#define S           second
#define endl        "\n"//<<flush
#define eps         1e-6
#define lowbit(x)   (x&(-x))
#define PI          acos(-1.0)
#define inf         0x3f3f3f3f
#define MAXN        0x7fffffff
#define INF         0x3f3f3f3f3f3f3f3f
#define pa          pair<int,int>
#define ferma(a,b)  pow(a,b-2)
#define pb          push_back
#define all(x)      x.begin(),x.end()
#define memset(a,b) memset(a,b,sizeof(a));
#define IOS         ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
using namespace std;
void file()
{
#ifdef ONLINE_JUDGE
#else
    freopen("cin.txt","r",stdin);
    //  freopen("cout.txt","w",stdout);
#endif
}
const int N=1e3+5;
int fat[N*2];
int find(int x)
{
    return fat[x]==x?x:fat[x]=find(fat[x]);
}
signed main()
{
    IOS;
    //file();
    int n,m;
    cin>>n;
    for(int i=1;i<=n*2;i++)
        fat[i]=i;
    cin>>m;
    while(m--)
    {
        char a;
        int b,c;
        cin>>a>>b>>c;
        int f_b=find(b),e_b=find(b+n);
        int f_c=find(c),e_c=find(c+n);
        if(a=='F')
        {
            if(f_b!=f_c)
                fat[f_b]=f_c,fat[e_b]=fat[e_c];
        }
        else
        {
            if(f_b!=e_c)
                fat[f_b]=e_c,fat[f_c]=e_b;
        }
    }
    set<int>se;
    for(int i=1;i<=n*2;i++)
    {
        int root=find(i);
        if(min(root,i)<=n)
        se.insert(root);
    }
    cout<<se.size()<<endl;
    return 0;
}

AC代码

/*
                ____________    ______________       __
               / _________  /\ /_____   _____/\     / /\
              / /\       / /  \\    /  /\    \ \   / /  \
             / /  \_____/ /   / \__/  /  \____\/  / /   /
            / /   /    / /   /    /  /   /       / /   /
           / /   /    / /   /    /  /   /       / /   /
          / /   /    / /   /    /  /   /       / /   /
         / /___/____/ /   /    /  /   /       / /___/________
        /____________/   /    /__/   /       /______________/\
        \            \  /     \  \  /        \              \ \
         \____________\/       \__\/          \______________\/
           ___       ___               ___    __________
          /  /\     /  /\             /  /\  /_______  /\
         /  /__\___/  /  \           /  /  \ \      /  /  \
        /____    ____/   /          /  /   /  \____/  /   /
        \   /   /\   \  /          /  /   /       /  /   /
         \_/   /  \___\/ ___      /  /   /       /  /   /
          /   /   /     /  /\    /  /   /       /  /   /
         /   /   /     /  /__\__/  /   /       /  /___/____
        /___/   /     /___________/   /       /___________/\
        \   \  /      \           \  /        \           \ \
         \___\/        \___________\/          \___________\/

          A CODE OF CBOY

*/
#include<bits/stdc++.h>
//typedef long long ll;
//#define ull       unsigned long long
//#define int       long long
#define F           first
#define S           second
#define endl        "\n"//<<flush
#define eps         1e-6
#define lowbit(x)   (x&(-x))
#define PI          acos(-1.0)
#define inf         0x3f3f3f3f
#define MAXN        0x7fffffff
#define INF         0x3f3f3f3f3f3f3f3f
#define pa          pair<int,int>
#define ferma(a,b)  pow(a,b-2)
#define pb          push_back
#define all(x)      x.begin(),x.end()
#define memset(a,b) memset(a,b,sizeof(a));
#define IOS         ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
using namespace std;
void file()
{
#ifdef ONLINE_JUDGE
#else
    freopen("cin.txt","r",stdin);
    //  freopen("cout.txt","w",stdout);
#endif
}
const int N=1e3+5;
int fat[N*2];
int find(int x)
{
    return fat[x]==x?x:fat[x]=find(fat[x]);
}
signed main()
{
    IOS;
    //file();
    int n,m;
    cin>>n;
    for(int i=1;i<=n*2;i++)
        fat[i]=i;
    cin>>m;
    while(m--)
    {
        char a;
        int b,c;
        cin>>a>>b>>c;
        int f_b=find(b),e_b=find(b+n);
        int f_c=find(c),e_c=find(c+n);
        if(a=='F')
        {
            if(f_b!=f_c)
                fat[f_b]=f_c;
        }
        else
        {
            if(f_b!=e_c)
                fat[f_b]=e_c,fat[f_c]=e_b;
        }
    }
    set<int>se;
    for(int i=1;i<=n*2;i++)
    {
        int root=find(i);
        if(min(root,i)<=n)
        se.insert(root);
    }
    cout<<se.size()<<endl;
    return 0;
}

发布了149 篇原创文章 · 获赞 5 · 访问量 6894

猜你喜欢

转载自blog.csdn.net/weixin_44224825/article/details/104635804