LA 3644 X-Plosives && LA 3027 Corporative Network (并查集入门)

题目:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=12648 and http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=33982

题意:两题都是比较裸的并查集。。。LA3644判断环路,LA3027算点到根节点的距离。

分析:用带路径压缩的并查集查找操作即可。

LA3644代码:

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int maxn = 1e5+6;
int f[maxn];
int Find(int x)
{
	if(f[x]==x)
		return x;
	return f[x]=Find(f[x]);
}
int main()
{
	int x,y,i,j,fx,fy,ans;
	while(scanf("%d",&x)==1)
	{
		for(i=0;i<maxn;i++)
			f[i]=i;
		ans=0;
		while(x!=-1)
		{
			scanf("%d",&y);
			fx=Find(x);
			fy=Find(y);
			if(fx==fy)
				ans++;
			else
				f[fx]=fy;
			scanf("%d",&x);
		}
		printf("%d\n",ans);
	}
	return 0;
}

LA3027代码:

#include <iostream>
#include <cstdio>
using namespace std;
const int maxn = 2e5+6;
const int mod =1000;
int dis[maxn],f[maxn];
int ABS(int a)
{
	if(a<0)
		return -a;
	return a;
}
struct node
{
	int d,pa;
	node(int a=0,int b=0)
	:d(a),pa(b){}
};
node Find(int x)
{
	if(f[x]==x)
		return node(0,x);
	node temp=Find(f[x]);
	f[x]=temp.pa;
	dis[x]=dis[x]+temp.d;
	return node(dis[x],f[x]);
}

int main()
{
	int n,u,d,p,ncase;
	char str[10];
	scanf("%d",&ncase);
	while(ncase--)
	{
		scanf("%d",&n);
		for(int i=0;i<maxn;i++)
		{
			dis[i]=0;
			f[i]=i;
		}
		while(scanf("%s",str)&&str[0]!='O')
		{
			if(str[0]=='E')
			{
				scanf("%d",&u);
				Find(u);
				printf("%d\n",dis[u]);
			}
			else
			{
				scanf("%d%d",&u,&p);
				f[u]=p;
				dis[u]=ABS(u-p)%mod;
			}
		}
	}
	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值