loj 1412(树上最长直径的应用)
题目链接:http://lightoj.com/volume_showproblem.php?problem=1412
思路:好久没写题解了,有点手生,这题从昨天晚上wa到现在终于是过了。。。思想其实很简单,就是预处理出每一块的最长直径,然后每次询问的时候直接查询就可以了。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std; const int MAXN = ( + );
typedef pair<int,int>Pair; vector<int >g[MAXN];
vector<Pair >blocks;
int n,m,q,len,_count,max_len;
bool mark[MAXN]; int dfs(int u,int father)
{
if(!mark[u])mark[u] = true, _count ++;
int first = , second = ;
for(int i = ; i < (int)g[u].size(); i ++) {
int v = g[u][i];
if(v == father)continue;
int tmp = dfs(g[u][i],u) + ;
if(tmp > first)second = first, first = tmp;
else if(tmp > second)second = tmp;
}
if(first + second > len)len = first + second;
return first;
} int cmp(Pair p, Pair q)
{
if(p.second != q.second){
return p.second > q.second;
}else
return p.first > q.first;
} int main()
{
int u,v,_case,t=;
scanf("%d",&_case);
while(_case--){
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++)g[i].clear();
while(m--){
scanf("%d%d",&u,&v);
g[u].push_back(v);
g[v].push_back(u);
}
len=;
max_len = ;
memset(mark,false,sizeof(mark));
blocks.clear();
for(int i=;i<=n;i++){
if(!mark[i]){
len=;
_count = ;
dfs(i,-);
blocks.push_back(make_pair(len,_count));
max_len = max(max_len,len);
}
}
sort(blocks.begin(),blocks.end(),cmp);
scanf("%d",&q);
printf("Case %d:\n",t++);
while(q--){
int k;
scanf("%d",&k);
if(k > blocks[].second){
puts("impossible");
}else if(k <= max_len + ){
printf("%d\n",k - );
}else {
int ans = ( << );
for(int i=; i<(int)blocks.size(); i++){
if(k > blocks[i].second)break;
ans = min (ans, (blocks[i].first)+*(k-blocks[i].first-));
}
printf("%d\n",ans);
}
} }
return ;
}
loj 1412(树上最长直径的应用)的更多相关文章
- poj 3310(并查集判环,图的连通性,树上最长直径路径标记)
题目链接:http://poj.org/problem?id=3310 思路:首先是判断图的连通性,以及是否有环存在,这里我们可以用并查集判断,然后就是找2次dfs找树上最长直径了,并且对树上最长直径 ...
- 牛客小白月赛6 C 桃花 dfs 求树上最长直径
链接:https://www.nowcoder.com/acm/contest/136/C来源:牛客网 题目描述 桃花一簇开无主,可爱深红映浅红. ...
- Codefroces Gym 100781A(树上最长路径)
http://codeforces.com/gym/100781/attachments 题意:有N个点,M条边,问对两两之间的树添加一条边之后,让整棵大树最远的点对之间的距离最近,问这个最近距离是多 ...
- hdu 4607 Park Visit(树上最长链)
求树上最长链:两遍搜索. 第一次从树上任意点开始,最远点必然是某一条最长链上的端点u. 第二次从u开始,最远点即该最长链的另一端点. 先在最长链上走,不足再去走支链. 把询问数m错打成n,狠狠wa了一 ...
- 中南大学oj 1317 Find the max Link 边权可以为负的树上最长路 树形dp 不能两遍dfs
http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1317经典问题:树上最长路,边权可以为负值的,树形dp,不能用两边dfs.反例:5 41 2 22 ...
- [HDU4607]Park Visit(树上最长链)
HDU#4607. Park Visit 题目描述 Claire and her little friend, ykwd, are travelling in Shevchenko's Park! T ...
- HDU4612 Warm up —— 边双联通分量 + 重边 + 缩点 + 树上最长路
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=4612 Warm up Time Limit: 10000/5000 MS (Java/Ot ...
- 【POJ 3162】 Walking Race (树形DP-求树上最长路径问题,+单调队列)
Walking Race Description flymouse's sister wc is very capable at sports and her favorite event is ...
- 树上最长链 Farthest Nodes in a Tree LightOJ - 1094 && [ZJOI2007]捉迷藏 && 最长链
树上最远点对(树的直径) 做法1:树形dp 最长路一定是经过树上的某一个节点的. 因此: an1[i],an2[i]分别表示一个点向下的最长链和次长链,次长链不存在就设为0:这两者很容易求 an3[i ...
随机推荐
- c++数据类型和定义
我们都知道,刚开始学习数学的时候.乘法口诀.99乘法口诀.这个是大家都需要背的.背熟了这个,大家才能知道遇到算术题如何计算.这个99乘法口诀就是一种定义. 同样任何的语言都会有很多的定义.比如语文:各 ...
- pip 安装命令
pip官网文档 https://pip.pypa.io/en/latest/reference/pip.html 若没有将c:\Python27\Scripts加入到path环境变量,可以在c:\Py ...
- spring + myBatis 常见错误:SQL语法错误
在程序运行时,有时候会出现如下错误: 这个错误通常是你的sqlmapper.xml中sql语句语法有错误.所以请仔细查看你sql语句是否正确,比如{#id}这样写就会报上述错误,其实应该#{id}这样 ...
- 使用Java的多线程和IO流写一个文件复制功能类
创建一个复制功能类,继承Thread类,重写run()方法,把FileInputStream和FileOutputStream输入输出流写在run()方法内.示例代码如下: import java.i ...
- zpf框架的business使用方法
2015年3月9日 10:58:42 controller 是接受数据分派任务的地方 model 接收controller来的数据, 获取并处理数据库中的数据, 然后再返回给controller 的业 ...
- C# 读写十六进制bin 文件
读一个十六进制的bin文件,在bin文件添加四行头,生成新的bin文件.bin文件可以用vs打开查看. using System; using System.Collections.Generic; ...
- Windows下安装Maven
上篇文章刚说到Linux下安装maven的过程,有时候为了适合在本地构建项目开发,然后上传到远程服务器执行,需要在本地构建maven项目,那么一般就是在Windows下构建maven项目并导入到我们的 ...
- perl的一些函数(二)
1. tr 转换 转换不是替换(tr///==y///) tr/searchlist/replacementlist/用于查找某个一个字符串,并用replacementlist替换,可以使用正则表达式 ...
- Ubuntu jsp平台使用JDBC来连接MySQL数据库
Ubuntu 7.04 搭建Ubuntu jsp平台开发环境MySQL+tomcat+apache+j2sdk1.6在所有安装开始前先在Terminal中输入 rpm -q -a查看是否安装过rpm ...
- 将file转变成contenthash
一.将MultipartFile转file CommonsMultipartFile cf= (CommonsMultipartFile)file; DiskFileItem fi = (DiskFi ...