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 ...
随机推荐
- poj 2251
http://poj.org/problem?id=2251 一道简单的BFS,只不过是二维数组,变三维数组,也就在原来基础上加了两个方向. 题意就是从S走到E,#不能走. #include < ...
- CentOS更新yum源
CentOS更新yum源 1.备份 mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup 2.下载 ...
- ios 在ios9中 NSNotificationCenter addObserver 不会影响对象释放
如题,ios9上, [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(test) name:@&qu ...
- NoSQL之【MongoDB】学习(三):配置文件说明
摘要: 继上一篇NoSQL之[MongoDB]学习(一):安装说明 之后,知道了如何安装和启动MongoDB,现在对启动时指定的配置文件(mongodb.conf)进行说明,详情请见官方. 启动Mon ...
- Effective C++ -----条款51:编写new 和delete 时需固守常规
operator new 应该内含一个无穷循环,并在其中尝试分配内存,如果它无法满足内存需求,就该调用new-handler.它也应该有能力处理0 bytes 申请.Class专属版本则还应该处理“比 ...
- ocx文件转换成C#程序引用的DLL
将ocx文件转换成C#程序引用的DLL文件的办法 将ocx文件转换成C#程序引用的DLL文件的办法,需要的朋友可以参考一下 1.打开VS2008或VS2010命令提示符(此例用VS2008) 将o ...
- ACdream 1224 Robbers (贪心)
一道贪心题,很久前做的,代码是我以前写的. 题意:有n个抢劫者抢劫了m块金子,然后第i个人平分xi/y块金子,但是会有除不尽的情况而金子不可再分,那么每个人都有一个不满意度fabs(xi / y - ...
- Hibernate双向一对一对象关系模型映射
一个员工一辆车:one-to-one 实现一:让汽车表中的外键唯一 create table emp ( eid int primary key auto_increment, ename varch ...
- [Android Pro] AIDL进程间传递自定义类型参数
1.创建.aidl 文件 AIDL 语法简单,用来声明接口,其中的方法接收参数和返回值,但是参数和返回值的类型是有约束的,且有些类型是需要 import,另外一些则无需这样做. AIDL 支持的数据类 ...
- August 7th 2016, Week 33rd Sunday
Knowing yourself is the height of wisdom. 了解自己就是大智慧. Two-day holiday, even I didn't have enought tim ...