hrbust 哈理工oj 2016 势力较量 (并查集)
来源:互联网 发布:淘宝买的邮票是真的吗 编辑:程序博客网 时间:2024/05/05 13:20
势力较量Time Limit: 1000 MSMemory Limit: 32768 KTotal Submit: 123(48 users)Total Accepted: 70(46 users)Rating: Special Judge: NoDescription
为了简化问题,给每个国家编号,像“国家1”, “国家2”......“国家N”
第一行两个整数N,M。N为国家个数,M为关系数目。
接下来M行,每行两个整数u, v。表示国家u,国家v相遇,有一场打斗。
然后试一个整数Q,表示Q个询问。
接下来Q行,每行一个整数S。
(N<100, M < 100, Q < 100)OutputCase k:
对于每个询问,输出两个整数,
第一个整数表示S所在国家的头目,第二个整数表示S所在国家里边一共有多少个小国家。Sample Input5 3
1 2
1 3
4 5
2
1
5
5 4
1 2
1 3
1 4
1 5
4
2
3
4
5Sample OutputCase 1:
2 3
5 2
Case 2:
2 5
2 5
2 5
2 5
Source2014 Winter Holiday Contest 4#include<stdio.h>
#include<string.h>
using namespace std;
struct data
{
int num,pos;
} pre[103];
int find(int x)
{
if(pre[x].pos!=x)
{
return find(pre[x].pos);
}
return x;
}
int main()
{
int n,m;
int t=0;
while(~scanf("%d%d",&n,&m))
{
for(int i=1; i<=n; i++)
{
pre[i].pos=i;
pre[i].num=1;
}
for(int i=1; i<=m; i++)
{
int a,b;
scanf("%d%d",&a,&b);
int x=find(a);
int y=find(b);
if(x==y)
continue;
if(pre[x].num>pre[y].num)
{
pre[y].pos=x;
pre[x].num+=pre[y].num;
}
if(pre[x].num<pre[y].num)
{
pre[x].pos=y;
pre[y].num+=pre[x].num;
}
if(pre[x].num==pre[y].num)
{
if(pre[x].pos<pre[x].pos)
{
pre[y].pos=x;
pre[x].num+=pre[y].num;
}
else
{
pre[x].pos=y;
pre[y].num+=pre[x].num;
}
}
}
int q;
scanf("%d",&q);
printf("Case %d:\n",++t);
while(q--)
{
int z;
scanf("%d",&z);
int asd=find(z);
printf("%d %d\n",asd,pre[asd].num);
}
}
}
在战火纷乱的古代有许多小国家。在这些小国家中,就会有一些强大的,一些弱小的。势力大的就会吞并势力小的,
形成更大的势力国家。现在给出你一些目前的国家势力关系,你能预算出以后的局势吗?
为了简化问题,给每个国家编号,像“国家1”, “国家2”......“国家N”
国家的较量首先从人数上进行比较,人数多的能打赢人数少的。如果两个国家的人数相同,就根据国家头目的编号来判断,
我们假设编号大的国家能打得过编号小的国家。
当两个国家相遇的时候,就会有一场打斗,且一定要分出胜负,输的就会归顺于赢得, 形成一个新的国家。Input多组测试数据,处理到文件结束。对于每组数据:第一行两个整数N,M。N为国家个数,M为关系数目。
接下来M行,每行两个整数u, v。表示国家u,国家v相遇,有一场打斗。
然后试一个整数Q,表示Q个询问。
接下来Q行,每行一个整数S。
(N<100, M < 100, Q < 100)OutputCase k:
对于每个询问,输出两个整数,
第一个整数表示S所在国家的头目,第二个整数表示S所在国家里边一共有多少个小国家。Sample Input5 3
1 2
1 3
4 5
2
1
5
5 4
1 2
1 3
1 4
1 5
4
2
3
4
5Sample OutputCase 1:
2 3
5 2
Case 2:
2 5
2 5
2 5
2 5
Source2014 Winter Holiday Contest 4#include<stdio.h>
#include<string.h>
using namespace std;
struct data
{
int num,pos;
} pre[103];
int find(int x)
{
if(pre[x].pos!=x)
{
return find(pre[x].pos);
}
return x;
}
int main()
{
int n,m;
int t=0;
while(~scanf("%d%d",&n,&m))
{
for(int i=1; i<=n; i++)
{
pre[i].pos=i;
pre[i].num=1;
}
for(int i=1; i<=m; i++)
{
int a,b;
scanf("%d%d",&a,&b);
int x=find(a);
int y=find(b);
if(x==y)
continue;
if(pre[x].num>pre[y].num)
{
pre[y].pos=x;
pre[x].num+=pre[y].num;
}
if(pre[x].num<pre[y].num)
{
pre[x].pos=y;
pre[y].num+=pre[x].num;
}
if(pre[x].num==pre[y].num)
{
if(pre[x].pos<pre[x].pos)
{
pre[y].pos=x;
pre[x].num+=pre[y].num;
}
else
{
pre[x].pos=y;
pre[y].num+=pre[x].num;
}
}
}
int q;
scanf("%d",&q);
printf("Case %d:\n",++t);
while(q--)
{
int z;
scanf("%d",&z);
int asd=find(z);
printf("%d %d\n",asd,pre[asd].num);
}
}
}
0 0
- hrbust 哈理工oj 2026 势力较量【并查集】
- hrbust 哈理工oj 2016 势力较量 (并查集)
- 哈理工oj hrbust 1725 Virus Outbreaking【并查集】
- [hrbust 2026] 势力较量
- hrbust 1658,哈理工oj 1658 一笔画【并查集过】
- hrbust 哈理工oj 1418 夏夜星空【带权并查集】
- hrbust 哈理工oj 1360 Leyni的国家III【多路径并查集】
- hrbust/哈理工oj 1507 水神的栅栏【计算几何+并查集】
- hrbust 哈理工oj1490 咒语【基础并查集】
- 哈理工OJ 1959 森林木(并查集)
- 哈理工oj 1725 Virus Outbreaking 【并查集】
- 哈理工oj 1490咒语【基础并查集】
- Hrbust 哈理工 oj 2317 Game (完全背包)
- hrbust 哈理工oj 1588 神医【贪心】
- hrbust 哈理工oj 网线【MST+Prim】
- 哈理工oj/hrbust 1790 武林【DP】
- hrbust哈理工oj 1674 充电【贪心】
- hrbust 哈理工oj 1330 邂逅【模拟】
- 请教java中这个正则表达式该咋写
- js闭包解析
- 随机密码约瑟夫环
- Java文件拷贝的几种实现方案
- Spring面试问答Top 25
- hrbust 哈理工oj 2016 势力较量 (并查集)
- switch...case...和if...else...区别
- 递归递推之马拦过河卒
- LeetCode:153. Find Minimum in Rotated Sorted Array
- JavaSE_48th_缓冲流
- Activity如何后台运行?不是缓存,是运行。
- thinkphp标签实现bootsrtap轮播carousel实例
- 删除链表中倒数第N个节点
- Hibernate注解方法使用总结