hdu-1829 & poj-2492 并查集

http://acm.hdu.edu.cn/showproblem.php?pid=1829

#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <iomanip>

using namespace std;

int t, n, m;
int fa[2200];
int sex[2200];//sex[i]=j 表示i与j性别相反
int a, b;
int ok;

int findd(int x)
{
	if (x == fa[x])
		return x;
	else
		return findd(fa[x]);
}

void un(int x, int y)
{
	int fx = findd(x);
	int fy = findd(y);

	if (fx == fy)//同性的
	{
		return;
	}
	else
	{
		if (fy > fx)
			fa[fy] = fx;
		else
			fa[fx] = fy;
	}
}

int cases = 1;

int main()
{
	scanf("%d",&t);
	while (t--)
	{
		ok = 1;
		scanf("%d %d", &n, &m);
		{
			for (int i = 1; i <= n; i++)
			{
				fa[i] = i;
			}
			memset(sex,0,sizeof(sex));

			for (int i = 1; i <= m;i++)
			{
				scanf("%d%d",&a,&b);
				if (!ok)
					continue;
				if (findd(a) == findd(b))//同性的
				{
					ok = 0;
					continue;
				}

				if (sex[a] == 0)
				{
					sex[a] = b;
				}
				else
				{
					un(sex[a], b);
				}

				if (sex[b] == 0)
				{
					sex[b] = a;
				}
				else
				{
					un(sex[b], a);
				}
			}
		}

		if (!ok)
		{
			printf("Scenario #%d:\n",cases++);
			puts("Suspicious bugs found!");
			printf("\n");
		}
		else
		{
			printf("Scenario #%d:\n",cases++);
			puts("No suspicious bugs found!");
			printf("\n");
		}
	}
	return 0;
}
时间: 2024-08-03 11:14:28

hdu-1829 & poj-2492 并查集的相关文章

HDU 1829 &amp;&amp; POJ 2492 A Bug&#39;s Life(种类并查集)

题目地址:HDU 1829     POJ 2492 这个题可以用两种方法做,第一眼看完题是觉得用dfs染色判断二分图.然后又写的刚学的种类并查集.原来并查集可以这样用,真是神奇.. dfs染色代码: #include <iostream> #include <cstdio> #include <string> #include <cstring> #include <stdlib.h> #include <math.h> #incl

HDU 1829/POJ 2492 A Bug&#39;s Life

A Bug's Life Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 11981    Accepted Submission(s): 3901 Problem Description Background Professor Hopper is researching the sexual behavior of a rare s

POJ 2492 并查集扩展(判断同性恋问题)

G - A Bug's Life Time Limit:10000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 2492 Appoint description: Description BackgroundProfessor Hopper is researching the sexual behavior of a rare species of bugs. H

POJ 2492 并查集应用的扩展

A Bug's Life Time Limit: 10000MS Memory Limit: 65536K Total Submissions: 28651 Accepted: 9331 Description Background Professor Hopper is researching the sexual behavior of a rare species of bugs. He assumes that they feature two different genders and

poj 2492(并查集)

题意:有一个科学家提出了一个假设,一种虫子只有异性恋,而不是同性恋,然后开始实验来验证猜想,给出了n个虫子,编号从1到n,给出了q组恋爱虫子的编号,问是否验证猜想. 题解:数组倍增,给每个虫子一个异性恋的对象集合,然后每次输入的两个虫子如果不在同一个集合内,就放到对方异性恋集合内,否则无法验证猜想. #include <stdio.h> const int N = 4005; int n, q, pa[N]; int get_parent(int x) { return x == pa[x]

hdu 3234 Exclusive-OR (并查集+异或性质)

Exclusive-OR Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2177    Accepted Submission(s): 603 Problem Description You are not given n non-negative integers X0, X1, ..., Xn-1 less than 220 ,

HDU 4496 D-City (并查集)

题意:给你n个点m条边,问删除前i条边后有多少个连通分块. 思路:从后往前操作,从后往前添加i条边等于添加完m条边后删掉前m-i条边,可知刚开始没有边,所以sum[m]=n; #include <stdio.h> #include <iostream> #include <algorithm> #include <string.h> #include <queue> #include <math.h> #define M 100010

hdu 1811Rank of Tetris (并查集 + 拓扑排序)

1 /* 2 题意:这些信息可能有三种情况,分别是"A > B","A = B","A < B",分别表示A的Rating高于B,等于B,小于B. 3 4 现在Lele并不是让你来帮他制作这个高手榜,他只是想知道,根据这些信息是否能够确定出这个高手榜,是的话就输出"OK". 5 否则就请你判断出错的原因,到底是因为信息不完全(输出"UNCERTAIN"),还是因为这些信息中包含冲突(输出&quo

HDU 1232 畅通工程(并查集)

畅通工程 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 30485    Accepted Submission(s): 16013 Problem Description 某省调查城镇交通状况,得到现有城镇道路统计表,表中列出了每条道路直接连通的城镇.省政府"畅通工程"的目标是使全省任何两个城镇间都可以实现交通(但不一定有

HDU 2017 Code Lock (并查集的应用+快速幂)

链接:HDU 3461 题目大意: 题目的大意是一个密码锁上有编号为1到N的N个字母,每个字母可以取26个小写英文字母中的一个.再给你M个区间[L,M],表示该区间的字母可以一起同步"增加"(从'a'变为'b'为增1,'z'增1为'a').假如一组密码按照给定的区间进行有限次的"增加"操作后可以变成另一组密码,那么我们认为这两组密码是相同的.该题的目标就是在给定N.M和M个区间的前提下计算有多少种不同的密码. 根据题意,如果一个可调整的区间都没有的话,答案应该是26