QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#504707#9110. Zayin and Treeurayaha_yahaura#AC ✓76ms42472kbC++141.1kb2024-08-04 15:07:542024-08-04 15:07:55

Judging History

你现在查看的是最新测评结果

  • [2024-08-04 15:07:55]
  • 评测
  • 测评结果:AC
  • 用时:76ms
  • 内存:42472kb
  • [2024-08-04 15:07:54]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
int read(){
    char c;int res=0;
    while(!isdigit(c=getchar()));
    res=c-'0';
    while(isdigit(c=getchar()))
        res=res*10+c-'0';
    return res;
}
const int N=1000005;
int n,a[N]; vector<int> G[N];

#define out(x) cerr<<#x<<'='<<x<<' '
#define outln(x) cerr<<#x<<"="<<x<<"\n"
#define sz(x) (int)(x).size()
typedef long long ll;
const int inf=(int)1e9;

int ans=0,dep[N],mi1[N],mi2[N];
void dfs(int u,int f){
    mi1[u]=a[u]+dep[u],mi2[u]=-a[u]+dep[u];
    for(auto &v : G[u]) if(v!=f){
        dep[v]=dep[u]+1;
        dfs(v,u);
        mi1[u]=min(mi1[u],mi1[v]);
        mi2[u]=min(mi2[u],mi2[v]);
    }
    ans=min(ans,mi1[u]+mi2[u]-2*dep[u]+1);
}

void init(){
    n=read(); ans=inf;
    for(int i=1;i<=n;i++) a[i]=read(),G[i].clear();
    for(int i=1;i<n;i++){
        int x=read(),y=read();
        G[x].push_back(y); G[y].push_back(x);
    }
    dfs(1,0);
    printf("%d\n",ans);
}
 
void solve(){
    // outln(s[1]);
}

int main(){
    int T=read();
    while(T--){init(); solve();}
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 76ms
memory: 42472kb

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