http://acm.timus.ru/problem.aspx?space=1&num=1362

水题,树形DP

代码:

#include<iostream>
#include<stack>
#include<cstdio>
#include<queue>
#include<cstring>
#include<algorithm>
#include<vector>
#include<set>
#include<map>
#include<string>
#include<cmath> using namespace std; typedef long long ll;
typedef pair<int,int> pp;
const int INF=0x3f3f3f3f;
const int N=100002;
int head[N],I;
int d[N];
priority_queue<int>qt[N];
struct node
{
int j,next;
}edge[N*2];
void add(int i,int j)
{
edge[I].j=j;
edge[I].next=head[i];
head[i]=I++;
}
int dp(int pre,int x)
{
if(d[x]!=-1)
return d[x]; for(int t=head[x];t!=-1;t=edge[t].next)
{
int j=edge[t].j;
if(j==pre)continue;
qt[x].push(dp(x,j));
}
d[x]=0;
int t=0;
while(!qt[x].empty())
{
++t;
d[x]=max(d[x],t+qt[x].top());
qt[x].pop();
}
return d[x];
}
int main()
{
//freopen("data.in","r",stdin);
int n;
cin>>n;
memset(head,-1,sizeof(head));I=0;
memset(d,-1,sizeof(d));
for(int i=1;i<=n;++i)
{
int k;
while(scanf("%d",&k))
{
if(k==0) break;
add(i,k);add(k,i);
}
}
int root;
cin>>root;
cout<<dp(-1,root)<<endl;
return 0;
}

1362. Classmates 2的更多相关文章

  1. 【HDOJ】1362 The Bermuda Triangle

    1. 题目描述给定几个三角形拼成一个百慕大三角形. 2. 基本思路基本思路肯定是搜索,关键点是剪枝.(1) 若存在长度为$l$的边,则一定可以拼成长度为$k \cdot l$的三角形,则可拼成长度为$ ...

  2. 九度OJ 1362 左旋转字符串(Move!Move!!Move!!!)【算法】

    题目地址:http://ac.jobdu.com/problem.php?pid=1362 题目描述: 汇编语言中有一种移位指令叫做循环左移(ROL),现在有个简单的任务,就是用字符串模拟这个指令的运 ...

  3. 51nod 1362 搬箱子——[ 推式子+组合数计算方法 ] [ 拉格朗日插值 ]

    题目:http://www.51nod.com/Challenge/Problem.html#!#problemId=1362 方法一: 设 a 是向下走的步数. b 是向右下走的步数. c 是向下走 ...

  4. 剑指Offer - 九度1362 - 左旋转字符串(Move!Move!!Move!!!)

    剑指Offer - 九度1362 - 左旋转字符串(Move!Move!!Move!!!)2013-11-23 03:05 题目描述: 汇编语言中有一种移位指令叫做循环左移(ROL),现在有个简单的任 ...

  5. 51Nod 1362 搬箱子 —— 组合数(非质数取模) (差分TLE)

    题目:http://www.51nod.com/Challenge/Problem.html#!#problemId=1362 首先,\( f[i][j] \) 是一个 \( i \) 次多项式: 如 ...

  6. 题目1362:左旋转字符串(Move!Move!!Move!!!)

    题目1362:左旋转字符串(Move!Move!!Move!!!) 时间限制:2 秒 内存限制:32 兆 特殊判题:否 提交:2306 解决:961 题目描述: 汇编语言中有一种移位指令叫做循环左移( ...

  7. 【剑指Offer面试编程题】题目1362:左旋转字符串--九度OJ

    题目描述: 汇编语言中有一种移位指令叫做循环左移(ROL),现在有个简单的任务,就是用字符串模拟这个指令的运算结果.对于一个给定的字符序列S,请你把其循环左移K位后的序列输出.例如,字符序列S=&qu ...

  8. UVA 1362 Exploring Pyramids 区间DP

    Archaeologists have discovered a new set of hidden caves in one of the Egyptian pyramids. The decryp ...

  9. 九度oj 题目1362:左旋转字符串(Move!Move!!Move!!!)

    题目描述: 汇编语言中有一种移位指令叫做循环左移(ROL),现在有个简单的任务,就是用字符串模拟这个指令的运算结果.对于一个给定的字符序列S,请你把其循环左移K位后的序列输出.例如,字符序列S=”ab ...

随机推荐

  1. jQuery对select操作小结

    //遍历option和添加.移除optionfunction changeShipMethod(shipping){ var len = $("select[@name=ISHIPTYPE] ...

  2. oracle 面试题

    1.对数据库SQL2005.ORACLE熟悉吗?   SQL2005是微软公司的数据库产品.是一个RDBMS数据库,一般应用在一些中型数据库的应用,不能跨平台.   ORACLE是ORACLE公司的数 ...

  3. Android DrawerLayout Plus 增强版抽屉菜单

    DrawerLayout是官方提供的侧滑菜单,相比SliddingMenu,它更加轻量级.默认情况下,DrawerLayout可以设置左侧或者右侧滑出菜单.如下,   xml布局: <!-- & ...

  4. 马哥教育视频笔记:01(Linux常用命令)

    1.查看缓存中使用的命令和命令路径 [wskwskwsk@localhost /]$ hash 命中 命令 /usr/bin/printenv /usr/bin/ls /usr/bin/clear 2 ...

  5. HTML5——语音输入

    一.使用方式: <input type="text" x-webkit-speech /> 二.属性 1.lang属性:语言种类 <input type=&quo ...

  6. spring IOC 容器中 Bean 的生命周期

    IOC 容器中 Bean 的生命周期: 1.通过构造器或工厂方法创建 Bean 实例 2.为 Bean 的属性设置值和对其他 Bean 的引用 3.调用 Bean 后置处理器接口(BeanPostPr ...

  7. linux下rpm包安装、配置和卸载mysql

    WIN10下虚拟机:VMware workstation 12 PRO 安装 # 1.查看系统版本 [root@vm-xiluhua][/home/xiluhua]$ cat /etc/redhat- ...

  8. 【转】Server Tomcat v7.0 Server at localhost was unable to start within 45 seconds. If

    转载地址:http://fanshuyao.iteye.com/blog/1695482 在eclipse启动tomcat时遇到超时45秒的问题: Server Tomcat v7.0 Server ...

  9. bat 自动编译运行

    @echo off g++ %1 -Wall -Wextra --std=c++98 -O3 -DNDEBUG -D_NDEBUG -D_GLIBCXX_NO_ASSERT ^ -U_STLP_DEB ...

  10. C++string中有关字符串内容修改和替换的函数浅析

    1.assign() 原型: //string (1) basic_string& assign (const basic_string& str); //substring (2) ...