QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#353740 | #8235. Top Cluster | zsq147258369 | WA | 3ms | 15924kb | C++14 | 1.8kb | 2024-03-14 15:46:48 | 2024-03-14 15:46:49 |
Judging History
answer
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=2e6+5000;
int n,q,w[N],d[N],mp[N],siz[N],a[N],b[N],dep[N],cnt,head[N],fa[N],top[N],son[N],m;
struct node
{
int u,v,w,nxt;
}e[N];
void dfs(int x)
{
siz[x]=1;d[x]=d[fa[x]]+1;
for(int i=head[x];i;i=e[i].nxt)
{
int v=e[i].v;
if(v==fa[x])continue;
dep[v]=dep[x]+e[i].w;fa[v]=x;
dfs(v);siz[x]+=siz[v];
if(siz[v]>siz[son[x]])son[x]=v;
}
}
void dfs2(int x,int topp)
{
// top[x]=topp;cout<<x<<' '<<topp<<'\n';
top[x]=topp;
for(int i=head[x];i;i=e[i].nxt)
{
int v=e[i].v;
if(v==fa[x])continue;
dfs2(v,v==son[x]?topp:v);
}
}
int LCA(int u,int v)
{
while(top[u]!=top[v])
{
if(d[top[u]]<d[top[v]])swap(u,v);
u=fa[top[u]];
}
return d[u]<d[v]?u:v;
}
int len(int u,int v)
{
if(!u||!v)return 0;
return dep[u]+dep[v]-2*dep[LCA(u,v)];
}
main()
{
// freopen("in.txt","r",stdin);
cin>>n>>q;
for(int i=1;i<=n;i++)
{
cin>>w[i];
if(w[i]<=n)mp[w[i]]=i;
}
for(int i=1;i<n;i++)
{
int u,v,w;
cin>>u>>v>>w;
e[++cnt]=(node){u,v,w,head[u]};head[u]=cnt;
e[++cnt]=(node){v,u,w,head[v]};head[v]=cnt;
}
dfs(1);dfs2(1,1);while(mp[m++]);
a[0]=b[0]=mp[0];
for(int i=1;i<m;i++)
{
int x=a[i-1],y=b[i-1],w=len(x,y),t=mp[i],r=len(x,t)>len(y,t)?x:y;
if(len(r,t)>w)a[i]=r,b[i]=t;else a[i]=x,b[i]=y;
}
while(q--)
{
int x,d;cin>>x>>d;
int l=0,r=m-1,ans=m;
while(l<=r)
{
int mid=(l+r)>>1;
if(len(a[mid],x)>d||len(b[mid],x)>d)ans=mid,r=mid-1;
else l=mid+1;
}
cout<<ans<<'\n';
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 3ms
memory: 15924kb
input:
5 4 3 9 0 1 2 1 2 10 3 1 4 3 4 3 3 5 2 3 0 1 0 4 6 4 7
output:
1 0 3 5
result:
wrong answer 4th numbers differ - expected: '4', found: '5'