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…
例题: poj 1308 题目大意比较简单,对任意两个点,有且仅有一条道路,也就是一棵树. 题解:一棵树中,肯定是不能有环的,而且只能由一个根节点.(没认真读题,只知道在那里判环....),所以这个题先判环然后就是判根节点的唯一性. //#include<bits/stdc++.h> #include<cstdio> #include<iostream> #include<algorithm> using namespace std; ; int fa[N]…
题目链接 /* 判断一棵树: * 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. 并查集应用 */…
题目链接:http://poj.org/problem?id=3310 思路:首先是判断图的连通性,以及是否有环存在,这里我们可以用并查集判断,然后就是找2次dfs找树上最长直径了,并且对树上最长直径上的点进行标记,于是根据题意我们可以发现,如果这个图是“caterpillar”的话,那么他所有的边要么两端都在树上最长直径上,要么就是其中一端在,于是我们可以再次dfs进行判断就可以了. #include<iostream> #include<cstdio> #include<…
题目如下: A graph which is connected and acyclic can be considered a tree. The height of the tree depends on the selected root. Now you are supposed to find the root that results in a highest tree. Such a root is called the deepest root. Input Specificat…
题目意思是一个图中,只有上下左右四个方向的边.给出这样的一些边, 求任意指定的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]…
UVA1455 - Kingdom(并查集 + 线段树) 题目链接 题目大意:一个平面内,给你n个整数点,两种类型的操作:road x y 把city x 和city y连接起来,line fnum (浮点数小数点一定是0.5) 查询y = fnum这条直线穿过了多少个州和city.州指的是连通的城市. 解题思路:用并查集记录城市之间是否连通,还有每一个州的y的上下界.建立坐标y的线段树,然后每次运行road操作的时候,对范围内的y坐标进行更新:更新须要分三种情况:两个州是相离,还是相交,还是包…
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4514 Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others) Problem Description 随着杭州西湖的知名度的进一步提升,园林规划专家湫湫希望设计出一条新的经典观光线路,根据老板马小腾的指示,新的风景线最好能建成环形,如果没有条件建成环形,那就建的越长越好. 现在已经勘探确定了n个…
题目描述 给定一个 $n\times m$ 的方格图,每个格子有 ↑.↓.←.→,表示从该格子能够走到相邻的哪个格子.有一些格子是空着的,需要填上四者之一,需要满足:最终的方格图中,从任意一个位置出发都能够走出方格图.求方案数 mod 10^9+7. $数据组数\le 10$ ,$n,m\le 300$ ,$空格子数k\le 200$ 题解 并查集+矩阵树定理 由于k很小,又是计数问题,考虑矩阵树定理. 先使用并查集处理出从每个位置开始,最终会走到哪个位置.显然如果有环则答案为0,否则一定走到的…
超级无敌巨牛逼并查集(带权并查集)https://vjudge.net/problem/UVALive-4487 带删点的加权并查集 https://vjudge.net/problem/UVA-11987 并查集+线段树进行修改与统计 https://vjudge.net/problem/UVALive-4730 线段树 https://vjudge.net/problem/UVALive-4108 暴力 线段树 https://vjudge.net/problem/UVA-12299 树状数…