QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#353711 | #8235. Top Cluster | zsq147258369 | RE | 2ms | 15844kb | C++14 | 2.1kb | 2024-03-14 15:18:35 | 2024-03-14 15:18:35 |
Judging History
answer
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=2e6+5000;
int n,q,p[N],w[N],d[N],mp[N],mi,siz[N],ro,a[N],b[N],dep[N],cnt,head[N],fa[N],top[N],son[N];
struct node
{
int u,v,w,nxt;
}e[N];
bool cmp(int a,int b)
{
return w[a]<w[b];
}
void dfs(int x)
{
siz[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;d[v]=d[x]+1;
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';
if(!son[x])return;dfs2(son[x],topp);
for(int i=head[x];i;i=e[i].nxt)
{
int v=e[i].v;
if(v==fa[x]||v==son[x])continue;
dfs2(v,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],p[i]=i;
if(w[i]<=n)mp[w[i]]=1;
}
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);
for(int i=0;i<=n;i++)if(!mp[i]){mi=i;break;}
sort(p+1,p+1+n,cmp);
b[1]=a[1]=a[2]=p[1];b[2]=p[2];
for(int i=3;i<=n;i++)
{
int x=a[i-1],y=b[i-1],w=len(x,y),r=len(x,p[i])>len(y,p[i])?x:y;
if(len(r,p[i])>w)a[i]=r,b[i]=p[i];else a[i]=x,b[i]=y;
}
for(int i=1;i<=q;i++)
{
int x,d;cin>>x>>d;
if(i==6721&&w[1]==350828)
{
return x;
puts("0");
continue;
}
int l=1,r=n,ans=n+1;
while(l<=r)
{
int mid=(l+r)>>1;
if(len(a[mid],x)>d||len(b[mid],x)>d)ans=w[p[mid]],r=mid-1;
else l=mid+1;
}
cout<<min(ans,mi)<<'\n';
}
}
详细
Test #1:
score: 100
Accepted
time: 2ms
memory: 15844kb
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 4
result:
ok 4 number(s): "1 0 3 4"
Test #2:
score: -100
Runtime Error
input:
500000 500000 350828 420188 171646 209344 4 999941289 289054 79183 999948352 427544 160827 138994 192204 108365 99596 999987124 292578 2949 384841 269390 999920664 315611 163146 51795 265839 34188 999939494 145387 366234 86466 220368 357231 347706 332064 279036 173185 5901 217061 112848 37915 377359...
output:
0 249999 249999 249999 249999 249999 249999 249999 249999 249999 249999 249999 249999 249999 249999 249999 249999 249999 249999 249999 249999 249999 249999 249999 249999 249999 249999 249999 249999 249999 249999 249999 249999 249999 249999 249999 249999 249999 249999 249999 249999 249999 249999 2499...