hdu 4607 Park Visit dfs

   这题题意很好理解,就是要找到个最大长度,比赛的时候就在纠结怎么找,一开始想dfs怕超时,后来用并查集乱搞过了,看了标程果然是dfs,但只要2次就可以了,显然。

   于是自己又敲了一遍

/*
author:jxy
lang:C/C++
university:China,Xidian University
**If you need to reprint,please indicate the source**
*/
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <queue>
#define INF 1E9
using namespace std;
vector<int>node[100005];
int d[100005];
void dfs(int u,int f,int dp)
{
    d[u]=dp;
    for(int i=0;i<node[u].size();i++)
        if(node[u][i]!=f)dfs(node[u][i],u,dp+1);
}
int main()
{
    int T,n,m,k,K,i,v,u;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d",&n,&m);
        for(i=1;i<=n;i++)node[i].clear();
        for(i=1;i<n;i++)
        {
            scanf("%d%d",&v,&u);
            node[u].push_back(v);
            node[v].push_back(u);
        }
        K=1;
        dfs(K,0,1);
        for(i=1;i<=n;i++) if(d[i]>d[K])K=i;//直径的一个端点
        dfs(K,0,1);
        for(i=1;i<=n;i++) if(d[i]>d[K])K=i;//另一个端点
        K=d[K];
        while(m--)
        {
            scanf("%d",&k);
            if(k<=K)printf("%d\n",k-1);
            else printf("%d\n",K-1+2*(k-K));
        }
    }
}
时间: 2024-10-28 22:56:45

hdu 4607 Park Visit dfs的相关文章

hdu1175连连看广搜WA,各大神走过路过看一看啊~

问题描述 hdu1175连连看广搜WA,各大神走过路过看一看啊~ package cn.hncu.search; import java.util.Scanner; public class P1175Bfs { static int[][] map; static int[][] visited; static int eh,el;//终点行列 static int h,l;//输入数据时限制的行列 private static void bfs(int ch, int cl) {//传入当前

第三届蓝桥杯复试

第四题:奇怪的比赛 某电视台举办了低碳生活大奖赛.题目的计分规则相当奇怪: 每位选手需要回答10个问题(其编号为1到10),越后面越有难度.答对的,当前分数翻倍:答错了则扣掉与题号相同的分数(选手必须回答问题,不回答按错误处理). 每位选手都有一个起步的分数为10分. 某获胜选手最终得分刚好是100分,如果不让你看比赛过程,你能推断出他(她)哪个题目答对了,哪个题目答错了吗? 如果把答对的记为1,答错的记为0,则10个题目的回答情况可以用仅含有1和0的串来表示.例如:0010110011 就是可

HDU 2489 Minimal Ratio Tree (DFS枚举+最小生成树Prim)

链接: HDU : http://acm.hdu.edu.cn/showproblem.php?pid=2489 POJ  : http://poj.org/problem?id=3925 题目: Problem Description For a tree, which nodes and edges are all weighted, the ratio of it is calculated according to the following equation. Given a comp

算法题:HDU 1298 T9(手机输入法相关,字典树+dfs)

链接: http://acm.hdu.edu.cn/showproblem.php?pid=1298 题目: Problem Description A while ago it was quite cumbersome to create a message for the Short Message Service (SMS) on a mobile phone. This was because you only have nine keys and the alphabet has mo

hdu 1241 Oil Deposits (一次dfs搞定有某有)

#include<iostream> #include<cstring> #include<cstdio> #include<algorithm> using namespace std; char map[105][105]; int dir[8][2]={0, 1, 1, 0, -1, 0, 0, -1, 1, 1, 1, -1, -1, 1, -1, -1}; int n, m; int ans; bool judge(int x, int y){ i

HDOJ(HDU) 2212 DFS(阶乘相关、)

Problem Description A DFS(digital factorial sum) number is found by summing the factorial of every digit of a positive integer. For example ,consider the positive integer 145 = 1!+4!+5!, so it's a DFS number. Now you should find out all the DFS numbe

HDU 2121 Ice_cream’s world II:不定根最小树形图

链接: http://acm.hdu.edu.cn/showproblem.php?pid=2121 题目: Ice_cream's world II Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1610    Accepted Submission(s): 354 Problem Description After awarded

HDU 1385 Minimum Transport Cost:最短路,打印字典序路径

链接: http://acm.hdu.edu.cn/showproblem.php?pid=1385 题目大意: 有N个城市,然后直接给出这些城市之间的邻接矩阵,矩阵中-1代表那两个城市无道路相连,其他值代表路径长度. 如果一辆汽车经过某个城市,必须要交一定的钱(可能是过路费). 现在要从a城到b城,花费为路径长度之和,再加上除起点与终点外所有城市的过路费之和. 求最小花费,如果有多条路经符合,则输出字典序最小的路径. 分析与总结: 1.   这题的关键在于按照字典序输出路径. 假设有 1---

HDU 1142 A Walk Through the Forest

链接: http://acm.hdu.edu.cn/showproblem.php?pid=1142 题目: A Walk Through the Forest Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 3689    Accepted Submission(s): 1340 Problem Description Jimmy e