C - Corporative Network UVA - 1329 -加权并查集
2017-08-18 14:48
441 查看
题目大意:有n个节点,初始时每个节点的父节点都不存在,每次执行一个I操作和E操作:
I操作:吧节点u的父节点设为v,距离为|u-v| % 1000,输入保证u没有父节点
E操作:询问u到根节点的距离。
1.因为把题目输入结束的‘O'看成’0‘wa了一中午
2.还有一个地方下面做感叹号标记的地方,一开始写的后面注释的部分不对
AC:
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <iomanip>
#include <algorithm>
#include <cmath>
using namespace std;
int r[20001];//
int d[20001];//存储每个点目前到所求点距离
int unite(int x,int y)//将x向y连边
{
r[x]=y;
}
int findp(int x)//查找该点的根
{
if(r[x]==x)
return x;
else //!!!!!
{
int pp=findp(r[x]);
d[x]+=d[r[x]];
return r[x]=pp;
// d[x]+=d[r[x]];
//return r[x]=findp(r[x]);
}
}
bool same(int aa,int bb)
{
return findp(aa)==findp(bb);
}
int main()
{
int t,n,a,b,w;
char c[101];
scanf("%d",&t);
while(t--)
{
memset(r,0,sizeof(r));
memset(d,0,sizeof(d));
scanf("%d",&n);getchar();
for(int i=1;i<=n;i++)
r[i]=i,d[i]=0;
while(scanf("%s",c)&&c[0]!='O')
{
if(c[0]=='E')
{
cin>>w;
findp(w);
cout<<d[w]<<endl;
}
if(c[0]=='I')
{
cin>>a>>b;
if(!same(a,b))
unite(a,b);
d[a] = abs(a - b) % 1000;
}
}
}
}
题目大意:有n个节点,初始时每个节点的父节点都不存在,每次执行一个I操作和E操作:
I操作:吧节点u的父节点设为v,距离为|u-v| % 1000,输入保证u没有父节点
E操作:询问u到根节点的距离。
I操作:吧节点u的父节点设为v,距离为|u-v| % 1000,输入保证u没有父节点
E操作:询问u到根节点的距离。
1.因为把题目输入结束的‘O'看成’0‘wa了一中午
2.还有一个地方下面做感叹号标记的地方,一开始写的后面注释的部分不对
AC:
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <iomanip>
#include <algorithm>
#include <cmath>
using namespace std;
int r[20001];//
int d[20001];//存储每个点目前到所求点距离
int unite(int x,int y)//将x向y连边
{
r[x]=y;
}
int findp(int x)//查找该点的根
{
if(r[x]==x)
return x;
else //!!!!!
{
int pp=findp(r[x]);
d[x]+=d[r[x]];
return r[x]=pp;
// d[x]+=d[r[x]];
//return r[x]=findp(r[x]);
}
}
bool same(int aa,int bb)
{
return findp(aa)==findp(bb);
}
int main()
{
int t,n,a,b,w;
char c[101];
scanf("%d",&t);
while(t--)
{
memset(r,0,sizeof(r));
memset(d,0,sizeof(d));
scanf("%d",&n);getchar();
for(int i=1;i<=n;i++)
r[i]=i,d[i]=0;
while(scanf("%s",c)&&c[0]!='O')
{
if(c[0]=='E')
{
cin>>w;
findp(w);
cout<<d[w]<<endl;
}
if(c[0]=='I')
{
cin>>a>>b;
if(!same(a,b))
unite(a,b);
d[a] = abs(a - b) % 1000;
}
}
}
}
题目大意:有n个节点,初始时每个节点的父节点都不存在,每次执行一个I操作和E操作:
I操作:吧节点u的父节点设为v,距离为|u-v| % 1000,输入保证u没有父节点
E操作:询问u到根节点的距离。
相关文章推荐
- Corporative Network UVA - 1329 加权并查集
- Corporative Network UVA - 1329 加权并查集(压缩路径优化)
- C - Corporative Network UVA - 1329 (并查集)
- Corporative Network UVA - 1329(并查集的修改与查询)
- Corporative Network UVA - 1329
- UVA 1329 Corporative Network
- UVALive 3027---Corporative Network+并查集的应用
- UVa 1329 Corporative Network (加权并查集)
- Corporative Network UVA - 1329 加权并查集
- UVA 1329 - Corporative Network
- Corporative Network +uvalive+并查集
- uva 1329 - Corporative Network(加权并查集)
- Corporative Network UVALive - 3027 并查集
- LA 3027 Corporative Network / 并查集
- UVA 1329 Corporative Network(并查集:路径压缩)
- uva 1329 - Corporative Network(数据结构:并查集)
- uva1329 Corporative Network(并查集:路径压缩)
- uva12232 - Exclusive-OR 加权并查集
- POJ1962 - Corporative Network - 并查集
- uva 12232 - Exclusive-OR(加权并查集)