HDU 4607 Park Visit HDU暑期多校1】的更多相关文章

10W个点的一棵树,边权为1 求访问K个点要走过的最小路程 BFS求出一条最长路以后,我们可以YY出其他的边都要重复走两次 树上的最长路可以从任意一点开始BFS求出这点的最大距离,再把终点设置为起点再做一次BFS 所以就判断K和最长路间的距离就行了 O(n) 算法 #include <set> #include <map> #include <list> #include <cmath> #include <ctime> #include <…
Park Visit Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 523    Accepted Submission(s): 236 Problem Description Claire and her little friend, ykwd, are travelling in Shevchenko's Park! The par…
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4607 题目大意:给你n个点,n-1条边,将图连成一棵生成树,问你从任意点为起点,走k(k<=n)个点,至少需要走多少距离(每条边的距离是1): 思路:树形dp求树的直径r: a:若k<=r+1 ,ans = k-1: b:若k>=r+1,ans = r+(k-(r+1))*2: 代码: #include "stdio.h" #include "string.h&…
http://acm.hdu.edu.cn/showproblem.php?pid=4607 先求树的直径 方法:两遍bfs ,任选一点 a  求到a点最远的一点b ,然后 求到b点最远点 c 这样 b和c之间的路径为 树的直径 然后讨论 假设直径上的点个数为 k   要访问的点个数为 v 1)如果v<=k 则可以直接在直径上参观  路径为 v-1 2)如果v>k 首先还是要走直径 但是 m=v-k  这m个点要到其他枝叶上访问 一共要花费 2*m 代码: #include<iostre…
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4607 首先如果k小于等于直径长度,那么答案为k−1.如果k大于直径长度,设直径长度为r,那么答案为r−1+(k−r)∗2. #include <cstdio> #include <cmath> #include <algorithm> #include <iostream> #include <cstring> #include <queue&…
Problem Description Claire and her little friend, ykwd, are travelling in Shevchenko's Park! The park is beautiful - but large, indeed. N feature spots in the park are connected by exactly (N-1) undirected paths, and Claire is too tired to visit all…
两次DFS求树直径方法见 这里. 这里的直径是指最长链包含的节点个数,而上一题是指最长链的路径权值之和,注意区分. K <= R: ans = K − 1; K > R:   ans = R − 1 + ( K − R ) ∗ 2; #include <cstdio> #include <cstring> #include <cstdlib> #include <algorithm> using namespace std; ; struct n…
求树上最长链:两遍搜索. 第一次从树上任意点开始,最远点必然是某一条最长链上的端点u. 第二次从u开始,最远点即该最长链的另一端点. 先在最长链上走,不足再去走支链. 把询问数m错打成n,狠狠wa了一次= = #include<stdio.h> #include<string.h> ; struct E{ int v,next; }e[MAXN<<]; struct Q{ int p,c; }q[MAXN]; int tol; int head[MAXN]; int v…
[题目]题意:N个城市形成一棵树,相邻城市之间的距离是1,问访问K个城市的最短路程是多少,共有M次询问(1 <= N, M <= 100000, 1 <= K <= N). [思路] 访问K个城市的路线: 可以发现它由一条主线和若干支线构成,并且主线上的边只用访问一次,而支线上的边必须且只用访问两次.而题目给定的是一棵树,那么访问K的城市就必须且仅需要走K-1条边.总边数是固定的,我们只需要保证主线最长即可,所以就是在树中找最长链. [找最长链]树形dp,dp[i]表示他的子树的最…
题目大意:给定一棵树,让求出依次访问k个点的最小花费,每条边的权值都为1. 思路:如果能一直往下走不回来,那么这个路径肯定是最小的,这就取决于给定的k,但是怎么确定这个能一直走的长度呢,其实这个就是树的直径,也叫作最长简单路径.找出来这个直径之后,只需和k比较一下就能确定走多少步.设直径为maxx, 如果maxx + 1== k的话,说明刚好不用回来走完最长的这个路,所以当k小于等于maxx + 1的时候就是k-1,当k大于maxx + 1的时候,除了要走完不用回来的路,肯定还要走那些用回来的,…
解题思路: 通过两次DFS求树的直径,第一次以随意点作为起点,找到距离该点距离最远的点,则能够证明这个点一定在树的直径上,然后以该点为起点进行DFS得到的最长路就是树的直径. 最后的询问,假设K <= D + 1则能够沿着直径走,距离为K  -  1, 假设K >= D + 1.则须要走直径旁边的分支,每訪问一个点距离为2(从直径到这个点,再返回到直径上). #include <iostream> #include <cstring> #include <cstd…
题意: 莱克尔和她的朋友到公园玩,公园很大也很漂亮.公园包含n个景点通过n-1条边相连.克莱尔太累了,所以不能去参观所有点景点. 经过深思熟虑,她决定只访问其中的k个景点.她拿出地图发现所有景点的入口都很特殊.所以她想选择一个入口,并找到一条最短的 路来参观k个景点.我们假设景点之间的距离为1. 解题思路:因为地图形状为树形,所以求树的最大直径.当k小于等于直径的时候输出k-1,当k大于直径的时候,因为访问了某一个景 需要返回直径所在的路径上,所以当k大于冷的时候结果为len-(k-len-1)…
Park Visit Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 4814    Accepted Submission(s): 2100 Problem Description Claire and her little friend, ykwd, are travelling in Shevchenko's Park! The p…
用树形DP找到这颗树距离最远的两个点....对于一个询问K..若小于最远距离..显然只要走最远的这条边走K-1个边就行了.. 当K大于了最远距离..就要去走其他的点...而去其他点必须再回到这个主线..所以要加上(最远距离上点总数-K)*2... Program: #include<iostream> #include<stack> #include<queue> #include<stdio.h> #include<algorithm> #in…
HDU#4607. Park Visit 题目描述 Claire and her little friend, ykwd, are travelling in Shevchenko's Park! The park is beautiful - but large, indeed. N feature spots in the park are connected by exactly (N-1) undirected paths, and Claire is too tired to visi…
Park Visit Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1826    Accepted Submission(s): 798 Problem Description Claire and her little friend, ykwd, are travelling in Shevchenko's Park! The pa…
传送门 Park Visit Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3721    Accepted Submission(s): 1667 Problem Description Claire and her little friend, ykwd, are travelling in Shevchenko's Park! T…
layout: post title: 2019牛客暑期多校训练营(第五场)G - subsequeue 1 (一题我真的不会的题) author: "luowentaoaa" catalog: true tags: mathjax: true - ACM-ICPC 题意 给你两个由数字组成的字符串\(S\),\(T\) 长度为\(1e3\),问你S中有多少个子序列的值大于字符串T: 思路 首先在比赛的时候一眼确定是\(N^2\) 复杂度的DP,但是想了两三个小时却没想到怎么转移,各种…
传送门 J-Counting Triangles_2021牛客暑期多校训练营3 (nowcoder.com) 题目 Goodeat finds an undirected complete graph with n vertices. Each edge of the graph is painted black or white. He wants you to help him find the number of triangles (a, b, c) (a < b < c), such…
点击打开链接 我就不说官方题解有多坑了 V图那么高端的玩意儿 被精度坑粗翔了 AC前 AC后 简直不敢相信 只能怪自己没注意题目For the distance d1 and d2, if fabs(d1-d2)<1e-7, we think d1 == d2 有空再补充V图的做法吧..本人也是第一次接触V图 //大白p263 #include <cmath> #include <cstdio> #include <cstring> #include <se…
题目链接 Problem Description At Valentine's eve, Shylock and Lucar were enjoying their time as any other couples. Suddenly, LSH, Boss of FFF Group caught both of them, and locked them into two separate cells of the jail randomly. But as the saying goes:…
题目链接 Problem Description Giving two strings and you should judge if they are matched. The first string contains lowercase letters and uppercase letters. The second string contains lowercase letters, uppercase letters, and special symbols: "." an…
题目链接 Problem Description zk has n numbers a1,a2,...,an. For each (i,j) satisfying 1≤i<j≤n, zk generates a new number (ai+aj). These new numbers could make up a new sequence b1,b2,...,bn(n−1)/2. LsF wants to make some trouble. While zk is sleeping, Ls…
题目链接 Problem Description The Eternal Fleet was built many centuries ago before the time of Valkorion by an unknown race on the planet of Iokath. The fate of the Fleet's builders is unknown but their legacy would live on. Its first known action was in…
题目链接 Problem Description The Death Star, known officially as the DS-1 Orbital Battle Station, also known as the Death Star I, the First Death Star, Project Stardust internally, and simply the Ultimate Weapon in early development stages, was a moon-si…
题目链接 Problem Description Galen Marek, codenamed Starkiller, was a male Human apprentice of the Sith Lord Darth Vader. A powerful Force-user who lived during the era of the Galactic Empire, Marek originated from the Wookiee home planet of Kashyyyk as…
题目链接 Problem Description Kyber crystals, also called the living crystal or simply the kyber, and known as kaiburr crystals in ancient times, were rare, Force-attuned crystals that grew in nature and were found on scattered planets across the galaxy.…
题目链接 Problem Description There are n nonnegative integers a1-n which are less than p. HazelFan wants to know how many pairs i,j(1≤i<j≤n) are there, satisfying 1ai+aj≡1ai+1aj when we calculate module p, which means the inverse element of their sum equ…
题目链接 Problem Description HazelFan wants to build a rooted tree. The tree has n nodes labeled 0 to n−1, and the father of the node labeled i is the node labeled ⌊i−1k⌋. HazelFan wonders the size of every subtree, and you just need to tell him the XOR…
题目链接 Problem Description There is a nonnegative integer sequence a1...n of length n. HazelFan wants to do a type of transformation called prefix-XOR, which means a1...n changes into b1...n, where bi equals to the XOR value of a1,...,ai. He will repea…