【CodeVS 1540】银河英雄传说 2002年NOI全国竞赛

noi这么水?
很久没有自己不看题解A题了 ~~~~~~
带权并查集!
CodeVS的数据不合规定??有乱七八糟的字符?? 见我注释掉的读入!【刚又测试 洛谷用被注释掉的那一句也可以A 说明CadeVS数据不合规定!

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdlib>
using namespace std;
#define INF 100000000
#define MAXN 1000000 // 永远的10^6
int fa[MAXN],depth[MAXN],m;
int F(int x)
{
    int tmp=fa[x];
    if(fa[x]!=x) 
    {
        fa[x]=F(fa[x]);
        depth[x]+=depth[tmp];
    }
    return fa[x];
}
int wa[MAXN];
int W(int x)
{
    if(wa[x]!=x) wa[x]=W(wa[x]);
    return wa[x];
}
int main()
{
    scanf("%d",&m);char c;
    for(int i=1;i<=30000;i++) fa[i]=i,wa[i]=i;
    for(int x,y,i=1;i<=m;i++)
    {
        //  getchar(),c=getchar();
        cin>>c;
        scanf("%d %d",&x,&y);
        if(c=='M') 
        {
            if(F(x)!=F(y)) 
            {
                int s=F(x),t=W(y);
                fa[s]=t;
                wa[t]=s;
                depth[s]=1;
            }
        }
        if(c=='C')
        {
            if(F(x)!=F(y))
                printf("-1\n");
            else 
                F(x),F(y),printf("%d\n",abs(depth[x]-depth[y])-1);
        }
    }
    return 0;
}

转载于:https://www.cnblogs.com/ofsxb/p/5114451.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值