Submission #505087

#TimeUsernameProblemLanguageResultExecution timeMemory
505087tgbegimxUntitled (POI11_dyn)C++17
100 / 100
900 ms20612 KiB
#include <iostream> #include <cstdio> #include <cstring> #define INF 1000000000 #define maxn 300005 using namespace std; int n,m; int a[maxn]; int ans; struct edge { int to,ne; }b[maxn*2]; int k=0,head[maxn]; int f[maxn],g[maxn]; int limit; int num=0,op1=0; inline int read() { int x=0,f=1; char ch=getchar(); while(ch<'0'||ch>'9'){ if(ch=='-') f=-1; ch=getchar(); } while(ch>='0'&&ch<='9'){ x=x*10+ch-'0'; ch=getchar(); } return x*f; } void add(int u,int v) { k++; b[k].to=v; b[k].ne=head[u]; head[u]=k; } void dfs(int x,int pre) { f[x]=INF; //The closest distance from the detonated point in the subtree rooted at i to i if(a[x]) g[x]=0; //The farthest distance between the unexploded point in the subtree rooted at i and i else g[x]=-INF; for(int i=head[x];i!=-1;i=b[i].ne) if(b[i].to!=pre) { dfs(b[i].to,x); f[x]=min(f[x],f[b[i].to]+1);//Find a tipping point closest to x g[x]=max(g[x],g[b[i].to]+1);//An unexploded point farthest from x } if(g[x]+f[x]<=limit) //For the subtree rooted at x, there is a tipping point that can detonate those unintended points within a specified time g[x]=-INF; if(g[x]==limit) //The distance from the point farthest from x that failed to be detonated from x = limit, indicating that point x must be ignited { f[x]=0,g[x]=-INF,num++; } } bool check(int x) { if(!x) return op1<=m; limit=x; num=0; dfs(1,0); if(g[1]+f[1]>limit) num++; return num<=m; } void getans() { int l=0,r=n,mid; ans=n; while(l<=r) { mid=(l+r)>>1; if(check(mid)) ans=mid,r=mid-1; else l=mid+1; } } int main() { memset(head,-1,sizeof(head)); n=read(); m=read(); for(int i=1;i<=n;i++) { a[i]=read(); if(a[i]) op1++; } int x,y; for(int i=1;i<n;i++) { x=read(); y=read(); add(x,y); add(y,x); } getans(); printf("%d\n",ans); return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...