hdu3974-并查集办法
来源:互联网 发布:meego社交软件 编辑:程序博客网 时间:2024/06/10 16:56
#include <iostream>
#include <stdio.h>
using namespace std;
int father[50010];
int relation[50010];
int time[50010];
void init(int n)
{
for(int i=1;i<=n;i++)
{
relation[i]=-1;
father[i]=i;
time[i]=-1;
}
}
int find(int x)
{
if(x!=father[x])
{
int tmp=father[x];
find(father[x]);
if(time[tmp]>time[x])
{
time[x]=time[tmp];
relation[x]=relation[tmp];
}
}
return father[x];
}
void Union(int x,int y)
{
father[x]=y;
}
int main()
{
int T;
cin>>T;
for(int k=1;k<=T;k++)
{ int n,x,y;
cin>>n;
init(n);
for(int j=1;j<n;j++)
{
cin>>x>>y;
Union(x,y);
}
int m;
char ch[5];
cin>>m;
int cot=0;
printf("Case #%d:\n",k);
while(m--)
{ scanf("%s",ch);
if(ch[0]=='T')
{
cin>>x>>y;
time[x]=cot++;
relation[x]=y;
}
else
{
cin>>x;
find(x);
cout<<relation[x]<<endl;
}
}
}
return 0;
}
#include <stdio.h>
using namespace std;
int father[50010];
int relation[50010];
int time[50010];
void init(int n)
{
for(int i=1;i<=n;i++)
{
relation[i]=-1;
father[i]=i;
time[i]=-1;
}
}
int find(int x)
{
if(x!=father[x])
{
int tmp=father[x];
find(father[x]);
if(time[tmp]>time[x])
{
time[x]=time[tmp];
relation[x]=relation[tmp];
}
}
return father[x];
}
void Union(int x,int y)
{
father[x]=y;
}
int main()
{
int T;
cin>>T;
for(int k=1;k<=T;k++)
{ int n,x,y;
cin>>n;
init(n);
for(int j=1;j<n;j++)
{
cin>>x>>y;
Union(x,y);
}
int m;
char ch[5];
cin>>m;
int cot=0;
printf("Case #%d:\n",k);
while(m--)
{ scanf("%s",ch);
if(ch[0]=='T')
{
cin>>x>>y;
time[x]=cot++;
relation[x]=y;
}
else
{
cin>>x;
find(x);
cout<<relation[x]<<endl;
}
}
}
return 0;
}
0 0
- hdu3974-并查集办法
- 【HDU3974】【并查集】【技巧】【代码短时间快】
- HDU3938 并查集 并查集
- 并查集(集并查)
- HDU1232 并查集<并>
- 并查集
- 数据结构-并查集
- 并查集
- 并查集!
- 并查集
- 并查集
- 并查集
- 并查集
- 并查集总结
- 并查集学习
- 并查集
- 并查集
- 并查集
- nncq---见证工程模块
- Elastic-Job - 分布式定时任务框架
- pixhawk _control_mode如何产生的
- 理解IP地址和端口号
- linux下使用C获取mp3 ID3数据时的一些注意事项(v1,v2.3,v2.4)
- hdu3974-并查集办法
- MyEclipse Servers视窗出现“Could not create the view: An unexpected exception was thrown”错误解决办法
- android 内存泄漏篇——OOM问题的解决办法总结
- HTML5学习(三)—1
- @Override问题
- hdoj 1875 畅通工程再续 (prim )
- iOS判断一个日期是否为昨日、今日、今年
- 我常用的Windbg命令
- 动态添加图片并绑定删除事件