poj-1703-Find them, Catch them
2018-06-17 23:44:55来源:未知 阅读 ()
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 41928 | Accepted: 12886 |
Description
Assume N (N <= 10^5) criminals are currently in Tadu City, numbered from 1 to N. And of course, at least one of them belongs to Gang Dragon, and the same for Gang Snake. You will be given M (M <= 10^5) messages in sequence, which are in the following two kinds:
1. D [a] [b]
where [a] and [b] are the numbers of two criminals, and they belong to different gangs.
2. A [a] [b]
where [a] and [b] are the numbers of two criminals. This requires you to decide whether a and b belong to a same gang.
Input
Output
Sample Input
1
5 5
A 1 2
D 1 2
A 1 2
D 2 4
A 1 4
Sample Output
Not sure yet.
In different gangs.
In the same gang.
题目大意:判断俩人是否一个集团,但给的信息是谁和谁不是一个集团的,考的是祖宗节点的距离
题目链接:http://poj.org/problem?id=1703
代码:
#include <iostream>
#include <cstring>
#include <cstdio>
const int MAX=1e5+5;
using namespace std;
int rea[MAX],f[MAX];
int find(int n)
{
if(n==f[n])
return n;
int tmp=f[n];
f[n]=find(f[n]);
rea[n]=(rea[tmp]+rea[n])%2;
return f[n];
}
int main()
{ int t,n,m;
cin>>t;
while(t--)
{
cin>>n>>m;
for(int i=1;i<=n;i++)
{
f[i]=i;
rea[i]=0;
}
char a[10];
int b,c;
for(int i=0;i<m;i++)
{
scanf("%s",a);
if(a[0]=='D')
{
scanf("%d%d",&b,&c);
int b1=find(b),c1=find(c);
if(b1!=c1)
{
f[b1]=c1;
rea[b1]=(rea[b]+rea[c]+1)%2;
}
}
else
{
scanf("%d%d",&b,&c);
int b1=find(b),c1=find(c);
if(b1==c1)
{
if(rea[b]==rea[c])
printf("In the same gang.\n");
else
printf("In different gangs.\n");
}
else
printf("Not sure yet.\n");
}
}
}
return 0;
}
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
- Catch That Cow 2020-02-02
- CodeForces 427D Match & Catch 2019-08-16
- POJ 3278 Catch That Cow 2019-08-16
- POJ1743 Musical Theme(后缀数组 二分) 2018-07-06
- Java异常之try,catch,finally,throw,throws 2018-06-18
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash