PAT 1021 Deepest Root[并查集、dfs][难]】的更多相关文章

1021 Deepest Root (25)(25 分) 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…
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 Specification: E…
1021. Deepest Root (25) 时间限制 1500 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue 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 t…
1021 Deepest Root (25)(25 分)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…
#include <cstdio> #include <cstdlib> #include <vector> using namespace std; class Node { public: vector<int> adj; bool visited; Node() : visited(false) {} }; void reset_nodes(vector<Node>& nodes) { int len = nodes.size();…
PAT甲级1021. Deepest Root 题意: 连接和非循环的图可以被认为是一棵树.树的高度取决于所选的根.现在你应该找到导致最高树的根.这样的根称为最深根. 输入规格: 每个输入文件包含一个测试用例.对于每种情况, 第一行包含正整数N(<= 10000),它是节点的数量,因此节点从1到N编号.然后按N-1行,每个都通过给定两个相邻节点的数字来描述一个边. 输出规格: 对于每个测试用例,打印一行中最深的根.如果这样的根不是唯一的, 打印他们的数字增加的顺序.在给定的图形不是树的情况下,打…
1021 Deepest Root (25 分)   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 r…
dfs求最大层数并查集求连通个数 #include <iostream> #include <cstdio> #include <algorithm> #include <string.h> #include <string> #include <vector> using namespace std; /* dfs求最大层数 并查集求连通个数 */ +; int n; ; //最大层数 vector<int>deepro…
http://pat.zju.edu.cn/contests/pat-a-practise/1021 无环连通图也可以视为一棵树,选定图中任意一点作为根,如果这时候整个树的深度最大,则称其为 deepest root. 给定一个图,按升序输出所有 deepest root.如果给定的图有多个连通分量,则输出连通分量的数量. 1.使用并查集判断图是否为连通的. 2.任意选取一点,做 dfs 搜索,选取其中一个最远距离的点 A,再做一次 dfs,找到的所有距离最远的点以及点 A 都是 deepest…
题目如下: 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…
先并查集判断连通性,然后暴力每个点作为根节点判即可. #include<iostream> #include<cstring> #include<cmath> #include<algorithm> #include<cstdio> #include<queue> #include<vector> using namespace std; struct Edge { int a,b; }e[]; ],dep,U; ]; v…
A graph which is connected and acyclic can be considered a tree. The hight 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 Specification: Ea…
https://pintia.cn/problem-sets/994805342720868352/problems/994805482919673856 A graph which is connected and acyclic can be considered a tree. The hight of the tree depends on the selected root. Now you are supposed to find the root that results in a…
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 Specification: E…
Deepest Root A graph which is connected and acyclic can be considered a tree. The hight 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 Spec…
题目链接:Codeforces 455C Civilization 题目大意:给定N.M和Q,N表示有N个城市,M条已经修好的路,修好的路是不能改变的.然后是Q次操作.操作分为两种.一种是查询城市x所在的联通集合中.最长的路为多长. 二是连接两个联通集合,採用联通之后最长路最短的方案. 解题思路:由于一开时的图是不能够改变的,所以一開始用dfs处理出各个联通集合.而且记录住最大值.然后就是Q次操作,用并查集维护,注意由于联通的时候要採用最长路径最短的方案,所以s的转移方程变为s = max(s,…
C. Timofey and a tree time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Each New Year Timofey and his friends cut down a tree of n vertices and bring it home. After that they paint all the n…
[题目大意] 给出n个结点和n-1条边,问它们能否形成一棵n个结点的树,如果能,从中选出结点作为树根,使整棵树的高度最大.输出所有满足要求的可以作为树根的结点. [思路] 方法一:模拟. 1 连通.边数为n-1的图一定是一棵树.因此先判断连通图个数(用DFS遍历图,从而计算连通图个数),等于1则能形成一棵树. 2 遍历每一个节点,假设它为根节点构造一棵树. 方法二: 1 由于连通.边数为n-1的图一定是一棵树,因此需要判断给定数据是否能使图连通.2 确定图连通后,则确定了树,选择合适根结点使树高…
原题: http://acm.hdu.edu.cn/showproblem.php?pid=1232 我的第一道并查集题目,刚刚学会,我是照着<啊哈算法>这本书学会的,感觉非常通俗易懂,另外还有一篇同样非常好的博客:http://blog.csdn.net/niushuai666/article/details/6662911 这两位大神已经把并查集讲解的非常透彻了,所以我就不班门弄斧了... 刚开始看到这道题的时候,我并不知道这里是怎么用到并查集的,可以说我对并查集的理解还不是很到位.看了上…
并查集 题意:找出给定的这些话中是否有冲突.若没有则最多有多少句是对的. /* 思路:如果第x句说y是对的,则x,y必定是一起的,x+n,y+n是一起的:反之x,y+n//y,x+n是一起的. 利用并查集判断 x 和 x+n 是否在同一集合. 至于查找最多正确的话,对这些 “小树” 进行dfs即可. */ #include<stdio.h> #include<string.h> #include<stdlib.h> #include<algorithm> #…
这道题还是非常有意思的,题意很简单,就是给定一个图,和图上的双向边,要求1号节点的度(连接边的条数)等于K,求这棵树的生成树. 我们首先要解决,如何让1号节点的度时为k的呢???而且求的是生成树,意思是不是所有边都会选择.那么我们如何选择才能保证1号节点有K个度呢???这里就要考虑联通分量的问题了,我们刨除1号点,那么联通分量的个数,就是我们让图联通的最小个数,因此我们需要用并查集,把点分在不同的联通块内部. 再考虑我们每个联通块,至少需要1条连接1号点的边.不够K再添加连接1号点的边. 然后考…
Problem UVA208-Firetruck Accept:1733  Submit:14538 Time Limit: 3000 mSec  Problem Description The Center City fire department collaborates with the transportation department to maintain maps of the city which reflects the current status of the city str…
problem 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 Specific…
贝壳找房在遥远的传奇境外,找到了一个强大的魔法师顾问.他有 22 串数量相同的法力水晶,每个法力水晶可能有不同的颜色.为了方便起见,可以将每串法力水晶视为一个长度不大于 10^5105,字符集不大于 10^5105 的字符串.现在魔法师想要通过一系列魔法使得这两个字符串相同.每种魔法形如 (u,\ v),\ u,\ v \le 10^5(u, v), u, v≤105,可以将一个字符 uu改成一个字符 vv,并且可以使用无限次.出于种种原因,魔法师会强行指定这两个串能否进行修改. 若失败输出 -…
Werewolf Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 2363    Accepted Submission(s): 713 Problem Description "The Werewolves" is a popular card game among young people.In the basic ga…
[题目链接] http://poj.org/problem?id=3728 [题目大意] 给出一棵树,每个点上都可以交易货物,现在给出某货物在不同点的价格, 问从u到v的路程中,只允许做一次买入和一次卖出,最多能得到多少钱. [题解] 我们维护一个up表示,x与父节点的连线中, 最大值在靠近父节点的位置时最小值与最大值的最大差值 dw表示,x与父节点的连线中,最小值在靠近父节点的位置时最小值与最大值的最大差值 Min和Max分别表示x到父节点中的最大值和最小值 对于询问x到y的答案,我们发现以L…
链接:https://www.nowcoder.com/acm/contest/84/C 来源:牛客网 题目描述 平面上有若干个点,从每个点出发,你可以往东南西北任意方向走,直到碰到另一个点,然后才可以改变方向. 请问至少需要加多少个点,使得点对之间互相可以到达. 输入描述: 第一行一个整数n表示点数( 1 <= n <= 100). 第二行n行,每行两个整数xi, yi表示坐标( 1 <= xi, yi <= 1000). y轴正方向为北,x轴正方形为东. 输出描述: 输出一个整…
问题描述 为了增加公司收入,F公司新开设了物流业务.由于F公司在业界的良好口碑,物流业务一开通即受到了消费者的欢迎,物流业务马上遍及了城市的每条街道.然而,F公司现在只安排了小明一个人负责所有街道的服务. 任务虽然繁重,但是小明有足够的信心,他拿到了城市的地图,准备研究最好的方案.城市中有n个交叉路口,m条街道连接在这些交叉路口之间,每条街道的 首尾都正好连接着一个交叉路口.除开街道的首尾端点,街道不会在其他位置与其他街道相交.每个交叉路口都至少连接着一条街道,有的交叉路口可能只连接着一 条或两…
题意 给定一个n个点m条边无向图(n,m<=1e5) 支持两个操作 1.添加一条边 2.询问点u到点v的所有路径中必经边的条数 操作数<=1e5 分析 第一眼看起来像是要动态维护无向图的边双连通分量,看似无法做 其实可以这样想,假设我们有一个树,那么这个树上的每一条边都是必经边 现在假设多加一条非树边(u,v),那么相当于原树上u->v这条路径上的所有边都变成了非必经边 也就是如果我们认为必经是1,非必经是0,那么所要做的两个操作就是1.将树上路径全部赋为0    2.询问树上路径的和…
题目: 给出一个结点d和一个无向图中所有的边,按字典序输出这个无向图中所有从1到d的路径. 思路: 1.看到紫书上的提示,如果不预先判断结点1是否能直接到达结点d,上来就直接dfs搜索的话会超时,于是就想到了用并查集来预先判断是否属于同一个连通分量. 2.可以将与d属于同一个连通分量的点用一个数组保存起来,然后dfs搜索这个数组就可以了,这也就是只搜索了与d在一个连通分量里的点. 3.当搜索到d的时候就输出路径. 代码: #include <bits/stdc++.h> #define inf…