BZOJ2599: [IOI2011]Race
2016-03-13 07:19
357 查看
点分
我是用map存的可能多了个log
但是跑得过去233333
我是用map存的可能多了个log
但是跑得过去233333
#include<cstdio> #include<iostream> #include<cstring> #include<cstdlib> #include<set> #include<map> using namespace std; char c; inline void read(int &a) {a=0;do c=getchar();while(c<'0'||c>'9');while(c<='9'&&c>='0')a=(a<<3)+(a<<1)+c-'0',c=getchar();} map<int,int>Cache,Cach; struct Chain { int u,len; Chain*next; Chain(){next=NULL;} }*Head[200001]; inline void Add(int u,int v,int len){Chain *tp=new Chain;tp->u=v;tp->len=len;tp->next=Head[u];Head[u]=tp;} int n,k; int Heavy[200001]; int size[200001]; bool chn[200001]; int Root; int Max; void FindRoot(int u,int f) { Heavy[u]=0; size[u]=1; for(Chain *tp=Head[u];tp;tp=tp->next) if(tp->u!=f&&!chn[tp->u]) { FindRoot(tp->u,u); size[u]+=size[tp->u]; if(size[tp->u]>Heavy[u])Heavy[u]=size[tp->u]; } Heavy[u]=max(Heavy[u],Max-size[u]); if(Heavy[u]<Heavy[Root])Root=u; } int MaxLen; int Ans=1<<29; void DFS(int u,int f,int Cur,int Has) { if(Cur==k) Ans=min(Has,Ans); MaxLen=max(MaxLen,Cur); if(Cach[Cur]>Has||!Cach[Cur])Cach[Cur]=Has; for(Chain *tp=Head[u];tp;tp=tp->next) if(tp->u!=f&&!chn[tp->u]) DFS(tp->u,u,Cur+tp->len,Has+1); } void Div(int u) { MaxLen=0; chn[u]=true; Cache.clear(); map<int,int>::iterator B,E; for(Chain *tp=Head[u];tp;tp=tp->next) if(!chn[tp->u]) { Cach.clear(); DFS(tp->u,u,tp->len,1); B=Cach.begin();E=Cach.end(); for(;B!=E;B++) { if(Cache[k-B->first]) Ans=min(Ans,B->second+Cache[k-B->first]); } B=Cach.begin(); for(;B!=E;B++) { if(Cache[B->first]>B->second||!Cache[B->first]) Cache[B->first]=B->second; } } int Old=Max; for(Chain *tp=Head[u];tp;tp=tp->next) if(!chn[tp->u]) { Max=size[tp->u]>size[u]?Old-size[u]:size[tp->u]; Root=0; FindRoot(tp->u,u); Div(Root); } } int main() { int i,j,t,l; read(n),read(k); for(i=2;i<=n;i++) read(j),read(t),read(l),Add(++j,++t,l),Add(t,j,l); Heavy[0]=Ans; Max=n; FindRoot(1,1); Div(Root); printf("%d\n",Ans==1<<29?-1:Ans); return 0; }
相关文章推荐
- python selenium2 环境搭建
- Oracle DB管理ASM实例
- 请问 char *型指针 和 int *型指针 的区别是什么?
- MySQL的配置
- 数组和指针背后——内存角度
- linux安装mysql出现Could NOT find Curses (missing CURSES_LIBRARY CURSES_INCLUDE_PATH)解决方法
- 【JAVA集合】集合迭代器快速失败行为及CopyOnWriteArrayList
- 惊了!modbus的RTU 3.5字符
- Android通用布局UniversalLayout
- [leetcode] 323. Number of Connected Components in an Undirected Graph 解题报告
- bzoj 1507: [NOI2003]Editor
- bzoj 1503: [NOI2004]郁闷的出纳员
- bzoj 1497: [NOI2006]最大获利
- 第一次
- Eclipse快捷键
- bzoj 1492: [NOI2007]货币兑换Cash
- bzoj 1491: [NOI2007]社交网络
- 71.双向最短路径:聚会
- bzoj 1483: [HNOI2009]梦幻布丁
- 71.双向最短路径:聚会