其核心是追溯其根数和链接两个数,而HDU 1856要多一步,每一个根数要标记自己和自己子数的个数,因此用结构体。注意:1856 用C写没超时,用C++写超时了╮(╯﹏╰)╭

接下来是题目和代码:

畅通工程

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 22006    Accepted Submission(s): 11457

Problem Description 某省调查城镇交通状况,得到现有城镇道路统计表,表中列出了每条道路直接连通的城镇。省政府“畅通工程”的目标是使全省任何两个城镇间都可以实现交通(但不一定有直接的道路相连,只要互相间接通过道路可达即可)。问最少还需要建设多少条道路?

Input 测试输入包含若干测试用例。每个测试用例的第1行给出两个正整数,分别是城镇数目N ( < 1000 )和道路数目M;随后的M行对应M条道路,每行给出一对正整数,分别是该条道路直接连通的两个城镇的编号。为简单起见,城镇从1到N编号。 注意:两个城市之间可以有多条道路相通,也就是说 3 3 1 2 1 2 2 1 这种输入也是合法的 当N为0时,输入结束,该用例不被处理。

Output 对每个测试用例,在1行里输出最少还需要建设的道路数目。

Sample Input 4 2 1 3 4 3 3 3 1 2 1 3 2 3 5 2 1 2 3 5 999 0 0

Sample Output 1 0 2 998

#include<iostream>
using namespace std;
int father[1100],sum;
int find(int a)
{
 while(a!=father[a])//这个地方只能用while ,不能用if,while循环,找到最初的father.
  a=father[a];
 return a;
}
void join(int a,int b)
{
 int x=find(a),y=find(b);  //追溯其根数
 if(x!=y)
 {
  father[x]=y; //链接两个不共根数的数
     sum--;
 }
}
int main()
{
 int m,n;
 while(cin>>m,m)
 {
  int i,a,b;
  cin>>n;
  sum=m-1;
  for(i=1;i<=m;i++)
   father[i]=i;   //初始化
  for(i=1;i<=n;i++)
  {
   cin>>a>>b;
   join(a,b);
  }
  cout<<sum<<endl;
 }
 return 0;
}

More is better Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 327680/102400 K (Java/Others) Total Submission(s): 9653    Accepted Submission(s): 3547

Problem Description Mr Wang wants some boys to help him with a project. Because the project is rather complex, the more boys come, the better it will be. Of course there are certain requirements.

Mr Wang selected a room big enough to hold the boys. The boy who are not been chosen has to leave the room immediately. There are 10000000 boys in the room numbered from 1 to 10000000 at the very beginning. After Mr Wang's selection any two of them who are still in this room should be friends (direct or indirect), or there is only one boy left. Given all the direct friend-pairs, you should decide the best way.

Input The first line of the input contains an integer n (0 ≤ n ≤ 100 000) - the number of direct friend-pairs. The following n lines each contains a pair of numbers A and B separated by a single space that suggests A and B are direct friends. (A ≠ B, 1 ≤ A, B ≤ 10000000)

Output The output in one line contains exactly one integer equals to the maximum number of boys Mr Wang may keep.

Sample Input 4 1 2 3 4 5 6 1 6 4 1 2 3 4 5 6 7 8

Sample Output 4 2

Hint A and B are friends(direct or indirect), B and C are friends(direct or indirect), then A and C are also friends(indirect).

In the first sample {1,2,5,6} is the result. In the second sample {1,2},{3,4},{5,6},{7,8} are four kinds of answers.

#include<stdio.h>
struct A
{
 int a;  //记录根数
 int b;  //自己和自己的子数
}rid[10000005];
int maxn;
int find(int x)
{
 int t=x;
 while(t!=rid[t].a)
  t=rid[t].a;  //追溯其源数
 int i=x;
 while(i!=t)
 {
  int j=rid[i].a;
  rid[i].a=t;  //把所有衍生的数放在源数下
  i=j;
 }
 return t;
}
void join(int x,int y)
{
 int i=find(x),j=find(y);
 if(i!=j)
 {
  rid[j].b=rid[j].b+rid[i].b;  //追寻其源数的子数及其本身的个数
  if(maxn<rid[j].b)
   maxn=rid[j].b; //找最大值
  rid[i].a=j;  //链接两个数~
 }
}
int main()
{
 int n,i,x,y;
 while(~scanf("%d",&n))
 {
  maxn=1;
  for(i=0;i<10000005;i++)
  {
   rid[i].a=i;  //初始化
   rid[i].b=1;
  }
  for(i=0;i<n;i++)
  {
   scanf("%d%d",&x,&y);
   join(x,y);
  }
  printf("%d\n",maxn);
 }
 return 0;
}

1232卡在find 函数的while 那好久,刚开始用的if,又因为测试数据太简单,所以检查了好久╭(╯^╰)╮

1856 和1232很像,只要加一个结构体,以及追溯根数的子数和相加~\(≧▽≦)/~,但是用C++写居然超!时!了!换C写就没超╮(╯▽╰)╭

关于 图论·并查集·HDU1232&1856的更多相关文章

  1. ACM: meixiuxiu学图论-并查集-最小生成树-解题报告

    /* 最小生成树,最小环的最大权值按照排序后去构建最小生成树就可以了,注意遇到的第一个根相同的点就记录权值,跳出,生成的环就是最小权值环. */ //AC代码: #include"iostr ...

  2. 并查集hdu1232

    Problem Description 某省调查城镇交通状况,得到现有城镇道路统计表,表中列出了每条道路直接连通的城镇.省政府“畅通工程”的目标是使全省任何两个城镇间都可以实现交通(但不一定有直接的道 ...

  3. 并查集——hdu1232(入门)

    传送门:畅通工程 实质是求连通分支的数量 #include <iostream> #include <cstdio> #include <algorithm> us ...

  4. hdu1232 并查集

    1. hdu1232 2.链接:http://acm.hdu.edu.cn/showproblem.php?pid=1232 3.总结:简单并查集 #include<iostream> # ...

  5. 简单并查集 -- HDU 1232 UVALA 3644 HDU 1856

    并查集模板: #include<iostream> using namespace std; ],x,y; ]; //初始化 x 集合 void init(int n) { ; i< ...

  6. 并查集(HDOJ 1856)

    并查集   英文:Disjoint Set,即“不相交集合” 将编号分别为1…N的N个对象划分为不相交集合, 在每个集合中,选择其中某个元素代表所在集合. 常见两种操作: n       合并两个集合 ...

  7. HDU(1856),裸的带权并查集

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1856 题意:朋友圈问题,A和B是朋友,B和C是朋友则A和C也是朋友,依次类推,题目的意思就是求最大的朋 ...

  8. HDU 1856 More is better(并查集+离散化)

    题目地址:HDU 1856 水题.因为标号范围太大,而数据数仅仅有10w,所以要先进行离散化.然后就是裸的并查集了. 代码例如以下: #include <iostream> #includ ...

  9. HDU 1856 并查集

    http://acm.hdu.edu.cn/showproblem.php?pid=1856 More is better Time Limit: 5000/1000 MS (Java/Others) ...

随机推荐

  1. bootstrap简单form表单样式-form-horizontal

    jsp代码: <div id="content" style="background-color: white;"> <form class= ...

  2. 浅谈自学Python之路(day1)

    2018-02-19  17:15:14 Python语言相对于其他语言较为简洁,也相对好入门比如后面不加分号,基本见不着大括号等优点 第一个程序,也是学每门语言都需要掌握的第一个代码 print(& ...

  3. ThinkPHP3.2.3扩展之自动分词获取关键字

    ThinkPHP自动获取关键词调用在线discuz词库 先按照下图路径放好插件 /** * 自动获取关键词(调用第三方插件) * @return [type] [description] * www. ...

  4. 【寒假集训系列DAY3】

    DAY2的坑之后会补上 DAY3 今天暴力分拿的还是不错的...60+30+25=115,但还是太弱了呀,每题都只会暴力(话说第3题直接输-1给了15分,本以为只会给5分,然后打了半个小时的爆搜... ...

  5. jmeter 参数化学习笔记

    上次写了在接口的交互过程中,系统返回的内容,需要在接下来的交互中用到,从而把参数进行参数化的关联,这次写一下在压测过程中,如果每次发起请求参数名相同,单参数值需要替换的,我们需要进行的参数化. 在使用 ...

  6. js数据管理的思考

    最近要做一个农场项目,涉及到很多js数据管理的需求,这里也做下总结,不断的总结,再修正内容,也是快速进步的方法. 数据管理几个方面考虑: * 设置(更新)字段值 * 检索,根据id, index, 属 ...

  7. NPOI导出Excel自动计算公式问题

    以前用过sheet.ForceFormulaRecalculation = true;当时能够自动计算出来. 今天把模板改了一下(没动公式,但是模板是老板改的,我也不知道他操作了什么),结果就不能自动 ...

  8. Windows phone开发 网络编程之HttpWebRequest

    HttpWebRequest和WebClient的区别1,HttpWebRequest是个抽象类,所以无法new的,需要调用HttpWebRequest.Create();2,其Method指定了请求 ...

  9. 多种效果进度指示层效果iOS源码项目

    该源码是一个多种效果进度指示层效果源码案例,源码KVNProgress,KVNProgress提供多种效果的进度指示层,有半透明效果,也支持全屏显示.指示层还高度支持自定义,可以按自己的需求定制.效果 ...

  10. 《计算机图形学基础(OpenGL版)》勘误表

    第1版第1次印刷: 所在页码 所在行 原内容 更正为 41 16 k=Δx/Δy k=Δy/Δx 46 6 s-t=2Δy/Δx(xi+1)+2b+2yi-1 s-t=2Δy/Δx(xi+1)+2b- ...