QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#414768 | #8235. Top Cluster | ushg8877 | TL | 0ms | 46708kb | C++14 | 2.5kb | 2024-05-19 17:08:34 | 2024-05-19 17:08:34 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define MP make_pair
mt19937 rnd(time(0));
const int MAXN=5e5+5;
int n,m,q,a[MAXN],tsiz,mson;
int eu[MAXN],ev[MAXN],ew[MAXN],siz[MAXN],maxx[MAXN];
int col[MAXN],ans[MAXN];
ll dis[MAXN];bool vis[MAXN],isw[MAXN];
struct query{ll id,d;};
struct info{int op,fr;ll d;};
vector<query> vec[MAXN];
vector<int> edg[MAXN];
info f[MAXN<<1];
void get_size(int u,int fa){
siz[u]=maxx[u]=1;
for(int id:edg[u]){
int v=eu[id]^ev[id]^u;
if(v==fa||vis[v]) continue;
get_size(v,u);
maxx[u]=max(maxx[u],siz[v]);
siz[u]+=siz[v];
}
maxx[u]=max(maxx[u],tsiz-siz[u]);
if(maxx[u]<maxx[mson]) mson=u;
}
void get_col(int u,int fa){
for(int id:edg[u]){
int v=eu[id]^ev[id]^u;
if(v==fa||vis[v]) continue;
col[v]=col[u];dis[v]=dis[u]+ew[id];
get_col(v,u);
}
return;
}
void get_info(int u,int fa){
f[++m]=(info){0,u,dis[u]};
for(auto q:vec[u]) f[++m]=(info){col[u],q.id,q.d-dis[u]};
for(int id:edg[u]){
int v=eu[id]^ev[id]^u;
if(v==fa||vis[v]) continue;
get_info(v,u);
}
}
void dfz(int u){
// cout<<"Dfz at "<<u<<'\n';
vis[u]=true;col[u]=u;dis[u]=0;
for(int id:edg[u]){
int v=eu[id]^ev[id]^u;
if(vis[v]) continue;
col[v]=v;dis[v]=ew[id];
get_col(v,u);
}
m=0;
get_info(u,0);
sort(f+1,f+m+1,[&](info x,info y){
if(x.d!=y.d) return x.d>y.d;
return x.op>y.op;
});
int mn1=1e9,fr1=0,mn2=1e9,fr2=0;
for(int i=1;i<=m;i++){
if(f[i].op==0){
if(mn1>a[f[i].fr]){
if(col[f[i].fr]!=col[fr1]) mn2=mn1,fr2=fr1;
mn1=a[f[i].fr];fr1=col[f[i].fr];
}else if(col[f[i].fr]!=col[fr1]&&mn2>a[f[i].fr]){
mn2=a[f[i].fr];fr2=col[f[i].fr];
}
}else{
if(fr1!=f[i].op) ans[f[i].fr]=min(ans[f[i].fr],mn1);
else ans[f[i].fr]=min(ans[f[i].fr],mn2);
}
}
for(int id:edg[u]){
int v=eu[id]^ev[id]^u;
if(vis[v]) continue;
mson=0;
get_size(v,0);get_size(mson,0);dfz(mson);
}
return;
}
int main(){
ios::sync_with_stdio(false);
// freopen("Otomachi_Una.in","r",stdin);
// freopen("Otomachi_Una.out","w",stdout);
cin>>n>>q;
for(int i=1;i<=n;i++){
cin>>a[i];
if(a[i]<=n) isw[a[i]]=true;
}
int mn=0;
while(isw[mn]) mn++;
for(int i=1;i<n;i++){
cin>>eu[i]>>ev[i]>>ew[i];
edg[eu[i]].push_back(i);
edg[ev[i]].push_back(i);
}
for(int i=1;i<=q;i++){
ll x,k;cin>>x>>k;
vec[x].push_back((query){i,k});
ans[i]=mn;
}
maxx[0]=1e9;
tsiz=n;
get_size(1,0);get_size(mson,0);
dfz(mson);
for(int i=1;i<=q;i++) cout<<ans[i]<<'\n';
cerr<<clock();
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 46708kb
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
Time Limit Exceeded
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...