菜鸟第一次做这种。想了好一会儿.==

首先还是初始化记忆数组,使得每一个元素的初始根节点是自己。

然后是对输入的数据进行并集。我们拿出每组元素的第一个作为根节点。

每次检测是否已经存在根节点。如果存在根节点压缩该元素通往新的根节点的路径,同时将其原来的根节点接在新的根节点上。

第一次WA是因为忽略了剩余元素的根节点没有更新。倒数第二个循环用于把所有点都连接到根节点上。

#include<stdio.h>
#include<iostream>
using namespace std;
int stu[];
int main()
{
int n,m,k,tmp1,tmp2,tmp3,er;
scanf("%d%d",&n,&m);
while(n)
{
for(int i=;i<n;i++)
{
stu[i]=i;
}
for(int i=;i<m;i++)
{
scanf("%d",&k);
scanf("%d",&tmp1);
for(int j=;j<k-;j++)
{
scanf("%d",&tmp2);
while(stu[tmp2]!=tmp2)
{
tmp3=tmp2;
tmp2=stu[tmp2];
stu[tmp3]=stu[tmp1];
}
stu[tmp2]=stu[tmp1];
}
}
er=;
for(int i=;i<n;i++)
{
tmp2=i;
while(stu[tmp2]!=tmp2)
{
tmp2=stu[tmp2];
}
stu[i]=tmp2;
}
for(int i=;i<n;i++)
{
if(stu[i]==stu[])
{
er++;
}
}
printf("%d\n",er);
scanf("%d%d",&n,&m);
}
return ;
}

POJ 1611的更多相关文章

  1. poj 1611(并查集)

    http://poj.org/problem?id=1611 题意:有个学生感染病毒了,只要是和这个学生接触过的人都会感染,而和这些被感染者接触的人,也会被感染,现在给定你一些协会的人数,以及所在学生 ...

  2. poj 1611 The Suspects 解题报告

    题目链接:http://poj.org/problem?id=1611 题意:给定n个人和m个群,接下来是m行,每行给出该群内的人数以及这些人所对应的编号.需要统计出跟编号0的人有直接或间接关系的人数 ...

  3. poj 1611 The Suspects(简单并查集)

    题目:http://poj.org/problem?id=1611 0号是病原,求多少人有可能感染 #include<stdio.h> #include<string.h> # ...

  4. 【原创】poj ----- 1611 The Suspects 解题报告

    题目地址: http://poj.org/problem?id=1611 题目内容: The Suspects Time Limit: 1000MS   Memory Limit: 20000K To ...

  5. (并查集)The Suspects --POJ --1611

    链接: http://poj.org/problem?id=1611 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=82830#probl ...

  6. POJ - 1611 The Suspects 【并查集】

    题目链接 http://poj.org/problem?id=1611 题意 给出 n, m 有n个人 编号为 0 - n - 1 有m组人 他们之间是有关系的 编号为 0 的人是 有嫌疑的 然后和 ...

  7. 【裸的并查集】POJ 1611 The Suspects

    http://poj.org/problem?id=1611 [Accepted] #include<iostream> #include<cstdio> #include&l ...

  8. 并查集 (poj 1611 The Suspects)

    原题链接:http://poj.org/problem?id=1611 简单记录下并查集的模板 #include <cstdio> #include <iostream> #i ...

  9. [并查集] POJ 1611 The Suspects

    The Suspects Time Limit: 1000MS   Memory Limit: 20000K Total Submissions: 35206   Accepted: 17097 De ...

  10. poj 1611:The Suspects(并查集,经典题)

    The Suspects Time Limit: 1000MS   Memory Limit: 20000K Total Submissions: 21472   Accepted: 10393 De ...

随机推荐

  1. Amazon后台模拟登陆

    本文基于python3.4的selenium库打开浏览器,并将浏览器中的登陆cookie保存到本地,那么下次登陆就可以直接利用cookie了: # !/usr/bin/python3.4 # -*- ...

  2. eclipse 打开其他项目的jar源码 乱码解决

    步骤1.在eclipse菜单栏中,Window–>Preferences–>General–>Content types 将JAR Content , Java Class File ...

  3. tree 查询出数据遍历tree

    $('#tree1').tree({ url:'${contextPath}/pedition/treelistJc.html?editionUid=${ formatEdition.ppmId}', ...

  4. C#文件系统管理

    目录 前言 Directory类和DirectoryInfo类 File类和FileInfo类 Path类 前言 管理文件系统主要是对计算机中文件和目录的管理,例如,读取文件信息.删除文件和读取目录信 ...

  5. smarty函数

    内置函数(重要的几个): <{html_checkboxes name='nation' values=$code output=$name selected=$selid separator= ...

  6. 多线程编程之Windows环境下创建新线程

    转自: http://www.cnblogs.com/lgxqf/archive/2009/02/10/1387480.html 在 Win32 API 中,创建线程的基本函数是 CreateThre ...

  7. WPF 数据绑定

    1.1绑定到对象 1.1.1.前台绑定 前台代码 5: </Grid> 1: <Grid x:Name=”GridProductDetails”> 2:   3: <Te ...

  8. [转] matlab figure最大化

    http://blog.163.com/yinhexiwen@126/blog/static/6404826620122942057214/ % figure 窗口最大化,坐标轴也随着窗口变大而相应变 ...

  9. HDU3415

    题目大意: 给出一个有N个数字(-1000..1000,N<=10^5)的环状序列,找出一个长度不大于k的连续子序列,使其和最大. 分析: 我们可以将环状序列从某处切开,变成一行,然后复制前n- ...

  10. 【java】之转码

    GBK->UTF-8 String str = "任意字符串"; str = new String(str.getBytes("gbk"),"u ...