C - The Suspects POJ - 1611(并查集)】的更多相关文章

The Suspects Time Limit: 1000MS   Memory Limit: 20000K Total Submissions: 30158   Accepted: 14665 Description Severe acute respiratory syndrome (SARS), an atypical pneumonia of unknown aetiology, was recognized as a global threat in mid-March 2003. T…
http://poj.org/problem?id=1611 题意:有个学生感染病毒了,只要是和这个学生接触过的人都会感染,而和这些被感染者接触的人,也会被感染,现在给定你一些协会的人数,以及所在学生的编号,要你求被感染的人数. 思路:首先,把同一个社团的人,合并到一个这个社团的第一个人的那里,并用一个数组记录这个集合有多少人,最后查找0这个元素在哪个集合就可以. #include <stdio.h> #include <string.h> #define l 30005 int…
并查集主要是两个过程,一个是并,一个是查 原理是用一个数组p[i]保存每个i的根节点,如果根节点一样则在同一个集合里,所以只有根节点p[i]=i; 查: int find(int x){return p[x]==x?x:p[x]=find(p[x]);} 并: void Union(int x,int y) { int xx = find(x);int yy=find(y); if(xx!=yy)//不在一个集合里 p[xx]=yy; } #include <cstdio> #include…
我发现以后写题要更细心,专心! #include<iostream>#include<algorithm>#include<stdio.h>#include<string.h>#include<ctype.h>#include<stdlib.h>#include<limits.h>#include<math.h>#include<queue>#include<stack>#include…
题目意思是一个图中,只有上下左右四个方向的边.给出这样的一些边, 求任意指定的2个节点之间的距离. 就是看不懂,怎么破 /* POJ 1984 并查集 */ #include <stdio.h> #include <string.h> #include <iostream> #include <algorithm> #include <math.h> using namespace std; ; int F[MAXN]; int dx[MAXN]…
Severe acute respiratory syndrome (SARS), an atypical pneumonia of unknown aetiology, was recognized as a global threat in mid-March 2003. To minimize transmission to others, the best strategy is to separate the suspects from others.   In the Not-Spr…
Severe acute respiratory syndrome (SARS), an atypical pneumonia of unknown aetiology, was recognized as a global threat in mid-March 2003. To minimize transmission to others, the best strategy is to separate the suspects from others.In the Not-Spread…
并查集的模板题,为了避免麻烦,合并的时候根节点大的合并到小的结点. #include<cstdio> #include<algorithm> using namespace std; const int maxn = 33333; int fa[maxn]; int num[maxn]; int n,m,t; void init(){ for(int i = 0; i < n; i++) {fa[i] = i; num[i] = 1;} } int find_father(i…
http://poj.org/problem?id=1797 题意:就是从第一个城市运货到第n个城市,最多可以一次运多少货. 输入的意思分别为从哪个城市到哪个城市,以及这条路最多可以运多少货物. 思路:我觉得可以用floyd来做这道题,结果交上去就TLE了,不过时间复杂度为n3TLE看起来也是比较正常,毕竟数字大. 然后我就看到网上有人用并查集来做,不然以前我都没往这方面想过,然后就用并查集来做 用并查集的思路就是,首先,对每组数据按照重量由大到小进行排序.然后查找合并.当Find(n) ==…
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…