Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 43132 | Accepted: 13257 |
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.
Source
#include<stdio.h>
int father[150500],relation[150050];
void init(int n)
{
int i;
for(i=1;i<=n;++i) father[i]=i;
for(i=1;i<=n;++i) relation[i]=0;
}
int find(int x)
{
int t;
if(x==father[x]) return (x);
t=father[x];
father[x]=find(t);
relation[x]=(relation[x]+relation[t])%2;
return (father[x]);
}
void merge(int x,int y)
{
int find_x=find(x);
int find_y=find(y);
if(find_x!=find_y)
{
relation[find_x]=(relation[y]+1-relation[x]+2)%2;
father[find_x]=find_y;
}
}
int main()
{
int T,n,m,num=0;char c;int x,y;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
init(n);
while(m--)
{
getchar();
scanf("%c%d%d",&c,&x,&y);
if(c=='D')
{
merge(x,y);
}else
if(c=='A')
{
if(find(x)!=find(y))
{
printf("Not sure yet.\n");
continue;
}else
if(relation[x]==relation[y])
{
printf("In the same gang.\n");
continue;
}else
if(relation[x]!=relation[y])
printf("In different gangs.\n");
}
}
}
return 0;
}
[并查集] POJ 1703 Find them, Catch them的更多相关文章
-
POJ 2236 Wireless Network ||POJ 1703 Find them, Catch them 并查集
POJ 2236 Wireless Network http://poj.org/problem?id=2236 题目大意: 给你N台损坏的电脑坐标,这些电脑只能与不超过距离d的电脑通信,但如果x和y ...
-
hdu - 1829 A Bug&#39;s Life (并查集)&;&;poj - 2492 A Bug&#39;s Life &;&; poj 1703 Find them, Catch them
http://acm.hdu.edu.cn/showproblem.php?pid=1829 http://poj.org/problem?id=2492 臭虫有两种性别,并且只有异性相吸,给定n条臭 ...
-
POJ 1703 Find them, Catch them (数据结构-并查集)
Find them, Catch them Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 31102 Accepted: ...
-
poj 1703 Find them, Catch them 【并查集 新写法的思路】
题目地址:http://poj.org/problem?id=1703 Sample Input 1 5 5 A 1 2 D 1 2 A 1 2 D 2 4 A 1 4 Sample Output N ...
-
POJ 1703 Find them, Catch them(带权并查集)
传送门 Find them, Catch them Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 42463 Accep ...
-
poj.1703.Find them, Catch them(并查集)
Find them, Catch them Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I6 ...
-
POJ 1703 Find them, Catch them(种类并查集)
Find them, Catch them Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 41463 Accepted: ...
-
POJ 1703 Find them, catch them (并查集)
题目:Find them,Catch them 刚开始以为是最基本的并查集,无限超时. 这个特殊之处,就是可能有多个集合. 比如输入D 1 2 D 3 4 D 5 6...这就至少有3个集合了.并且 ...
-
(中等) POJ 1703 Find them, Catch them,带权并查集。
Description The police office in Tadu City decides to say ends to the chaos, as launch actions to ro ...
随机推荐
-
windows下nginx+php简单配置
Nginx+php运行环境搭建 虽然目前nginx使用很广泛,在大陆主流的互联网站点或多或少会用到这个俄国人开发的小应用(占用资源小).但是我这个土鳖还是第一次自己独立配置,网上资料不少,但是还是遇到 ...
-
JSON 基础知识总结
JSON:JavaScript 对象表示法(JavaScript Object Notation)JSON 语法规则 数据在名称/值对中 数据由逗号分隔 花括号保存对象 方括号保存数组 JSON有6种 ...
-
【第二篇】.NET用NPOI读取Excel表格并在页面预览
博主用的是npoi2.0.1,支持.xls也支持.xlsx 直接上代码吧. <table class="table table-bordered table-striped" ...
-
live555源码研究(四)------UserAuthenticationDatabase类
一.UserAuthenticationDatabase类作用 1,用户/密码管理 2,鉴权管理 二.类UserAuthenticationDatabase继承关系图 ...
-
利用PS脚本自动删除7天之前建立的目录-方法1!
目前有一个备份目录,目录名称为d:\temp\bak目录,在这目录下,根据备份要求,自动生成了如下目录的列表: 20131012 20131011 20131010 20131009 20131008 ...
-
Java SE (1)之 JFrame 组件 FlowLayout 布局
package com.sunzhiyan; import java.awt.*; import java.awt.event.*; import javax.swing.*; public clas ...
-
贪心+容器 hdu4268
Problem Description Alice and Bob's game never ends. Today, they introduce a new game. In this game, ...
-
RPC REST 比较
REST 和 RPC是两种架构设计风格. 一般情况下REST多用于与外部接口访问时的设计,RPC多用于系统内部的. 为什么这样呢? 1.RPC必然有依赖,REST必然没有,不要抬杠,SDK暂时不算. ...
-
admin-6
Admin06 基本权限访问方式(权限) 读取:允许查看内容-read r 写入:允许修改内容-write w 可执行:允许运行和切换-execute x 对与文本文件: r:cat head tai ...
-
CentOS 7下systemd是如何stop mysql服务的
[背景] 有同事在研究mongo的服务启动方式,讨论到mysql5.7的服务管理时一起做了下面测试. MySQL5.7是用systemd来管理service的,它的配置文件/usr/lib/sys ...