并查集,是否成树,Poj(1308)】的更多相关文章

例题: poj 1308 题目大意比较简单,对任意两个点,有且仅有一条道路,也就是一棵树. 题解:一棵树中,肯定是不能有环的,而且只能由一个根节点.(没认真读题,只知道在那里判环....),所以这个题先判环然后就是判根节点的唯一性. //#include<bits/stdc++.h> #include<cstdio> #include<iostream> #include<algorithm> using namespace std; ; int fa[N]…
思路: 对于每一条新的边的两个端点,是否是属于一颗树,要是的话,就不是一颗树.否则,就合并. 这里要注意的是,不能是森林,我这里WA了两次了.只不过在最后,查看每个节点的祖先是否是同一个就可以了. #include <stdio.h> #include <string.h> #include <algorithm> using namespace std; ; int father[maxn]; bool vis[maxn]; int Find_set(int x) {…
  HDU 1272 I - 小希的迷宫 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 1272 Appoint description: Description 上次Gardon的迷宫城堡小希玩了很久(见Problem B),现在她也想设计一个迷宫让Gardon来走.但是她设计迷宫的思路不一样,首先她认为所有的通道都应该是双向连通的,…
一开始以为两道题是一样的,POJ的过了直接用相同代码把HDU的交了,结果就悲剧了.最后发现HDU的没有考虑入度不能大于一. 题意:用树的定义来 判断吧,无环,n个结点最多有n-1条边,不然就会有环.只有一个入度为0的结点,不存在入度大于1的结点. 思路:并查集. AC代码: #include<stdio.h> #include<string.h> #define N 100005 int in[N],pre[N],a,b,c[N]; void init()//初始化 { for(i…
题目链接 /* 判断一棵树: * 1.There is exactly one node, called the root, to which no directed edges point. * 2.Every node except the root has exactly one edge pointing to it. * 3.There is a unique sequence of directed edges from the root to each node. 并查集应用 */…
Problem Description A tree is a well-known data structure that is either empty (null, void, nothing) or is a set of one or more nodes connected by directed edges between nodes satisfying the following properties. There is exactly one node, called the…
http://poj.org/problem?id=1611 水题 题意:就是找一共有多少个人感染了,0是感染学生的编号. #include <stdio.h> #include <string.h> #define maxn 30005 int m,n; int belg[ maxn ]; int Find(int x) { int _x=x,_b; while( _x != belg[ _x ] ) _x = belg[ _x ]; while( x != belg[ x ]…
Find them, Catch them Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 43132   Accepted: 13257 Description The police office in Tadu City decides to say ends to the chaos, as launch actions to root up the TWO gangs in the city, Gang Drago…
Wireless Network Time Limit: 10000MS   Memory Limit: 65536K Total Submissions: 25022   Accepted: 10399 Description An earthquake takes place in Southeast Asia. The ACM (Asia Cooperated Medical team) have set up a wireless network with the lap compute…
http://poj.org/problem?id=1797 题意:就是从第一个城市运货到第n个城市,最多可以一次运多少货. 输入的意思分别为从哪个城市到哪个城市,以及这条路最多可以运多少货物. 思路:我觉得可以用floyd来做这道题,结果交上去就TLE了,不过时间复杂度为n3TLE看起来也是比较正常,毕竟数字大. 然后我就看到网上有人用并查集来做,不然以前我都没往这方面想过,然后就用并查集来做 用并查集的思路就是,首先,对每组数据按照重量由大到小进行排序.然后查找合并.当Find(n) ==…