QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#504479 | #9110. Zayin and Tree | AnotherDayofSun# | AC ✓ | 281ms | 16264kb | C++20 | 1.9kb | 2024-08-04 13:24:05 | 2024-08-04 13:24:06 |
Judging History
answer
#include<bits/stdc++.h>
#define For(i,j,k) for(int i=(j);i<=(k);i++)
#define foR(i,j,k) for(int i=(j);i>=(k);i--)
#define vi vector<int>
#define pb push_back
#define pii pair<int,int>
#define mkp make_pair
#define SZ(x) ((int)x.size())
using namespace std;
inline int read() {
char c;int res=0;bool flag=0;
while(c=getchar(),c<48)(c=='-')&&(flag=1);
do res=(res<<3)+(res<<1)+(c^48);
while(c=getchar(),c>47);
flag&&(res=-res); return res;
}
const int MN=1e6+5;
vi e[MN]; int n,a[MN],siz[MN],vis[MN],vrt,rt,ans=1;
void getroot(int u,int fa,int allsiz) {
siz[u]=1;
int mx=0;
for(auto v:e[u]) {
if(vis[v]||v==fa) continue;
getroot(v,u,allsiz);
siz[u]+=siz[v]; mx=max(mx,siz[v]);
}
mx=max(mx,allsiz-siz[u]);
if(rt==-1||vrt>mx) vrt=mx,rt=u;
}
int mn1,mn2,mn3,mn4;
void get(int u,int fa,int mn,int mx,int dep) {
mn=min(mn,a[u]); mx=max(mx,a[u]);
mn1=min(dep-mx,mn1),mn2=min(dep+mn,mn2);
ans=min(ans,dep+1-mx+mn);
for(auto v:e[u]) {
if(v==fa||vis[v]) continue;
get(v,u,mn,mx,dep+1);
}
}
void solve(int u) {
mn3=(1<<30),mn4=(1<<30);
for(auto v:e[u]) {
if(vis[v]) continue;
mn1=(1<<30),mn2=(1<<30);
get(v,u,a[u],a[u],1);
ans=min(ans,min(mn1+mn4+1,mn2+mn3+1));
mn3=min(mn3,mn1),mn4=min(mn4,mn2);
}
}
void dfs(int u,int allsiz) {
vis[u]=1;
solve(u);
for(auto v:e[u]) {
if(vis[v]) continue;
int t=(siz[v]<siz[u])?siz[v]:allsiz-siz[u];
vrt=(1<<30); getroot(v,u,t);
dfs(rt,t);
}
}
void works() {
ans=1;
n=read();
For(i,1,n) e[i].clear(),vis[i]=0;
For(i,1,n) a[i]=read();
For(i,1,n-1) {
int u=read(),v=read();
e[u].pb(v),e[v].pb(u);
}
dfs(1,n);
// cerr<<"?";
printf("%d\n",ans);
}
signed main() {
#ifdef wasa855
freopen("pro.in","r",stdin);
freopen("pro.out","w",stdout);
#endif
int T=read();
while(T--) {
works();
}
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 281ms
memory: 16264kb
input:
3009 5 4 5 3 4 2 1 2 2 3 3 4 3 5 5 4 4 1 1 2 1 2 2 3 3 4 3 5 10 5 8 1 0 8 7 5 2 0 4 2 4 3 8 3 9 1 2 1 3 3 6 4 5 5 7 6 10 10 6 8 8 4 8 0 6 6 0 2 7 10 1 7 2 9 2 3 3 4 1 5 1 6 6 8 1 2 10 9 0 4 0 4 6 0 2 0 0 1 5 1 3 1 7 2 6 1 2 1 9 1 4 5 8 7 10 10 8 8 1 2 7 4 8 6 0 8 1 6 1 7 1 5 7 9 1 3 1 2 2 10 3 4 1 8...
output:
0 -1 -6 -6 -7 -6 -7 -4 -3 -7 -5 -6 -5 -4 -6 -3 -4 -7 -4 -4 -6 -6 -6 -5 -4 -5 -6 -6 -7 -7 -5 -7 -6 -6 -7 -6 -5 -5 -4 -6 -6 -5 -6 -6 -6 -6 -3 -6 -3 -6 -4 -6 -7 -6 -7 -6 -6 -5 -7 -6 -4 -7 -3 -5 -5 -6 -4 -5 -7 -6 -5 -5 -4 -3 -5 -3 -4 -2 -6 -5 -7 -4 -5 -5 -7 -7 -4 -6 -5 -4 -6 -5 -5 -6 -3 -6 -7 -7 -7 -6 -...
result:
ok 3009 lines