QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#504675#9110. Zayin and TreeXunwuqishi#WA 193ms27356kbC++231.5kb2024-08-04 14:42:382024-08-04 14:42:39

Judging History

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

  • [2024-08-04 14:42:39]
  • 评测
  • 测评结果:WA
  • 用时:193ms
  • 内存:27356kb
  • [2024-08-04 14:42:38]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
#define lowbit(x) x&(-x)
#define int long long

const int N = 1e6+10;
int INF  = 1e9;
//int mod = 1e9+7;
//int mod =  1e9+7;
//int ways[4][2] = {{1,0},{-1,0},{0,1},{0,-1}};

int n,q,ma[N],mi[N],deep[N],lma[N],lmi[N],v[N];
vector<int> to[N];

int ans = INF;

void update(int x,int y){
    if(-ma[x] + lma[x] > -ma[y] + lma[y]){
        ma[x] = ma[y];
        lma[x] = lma[y];
    }

    if(mi[x]+lmi[x] > mi[y] + lmi[y]){
        mi[x] = mi[y];
        lmi[x] = lmi[y];
    }
}

void init(int x,int pre){
    ma[x] = v[x];
    mi[x] = v[x];
    lma[x] = deep[x];
    lmi[x] = deep[x];

    for(auto i:to[x]){
        if(i == pre) continue;
        deep[i] = deep[x]+1;
        init(i,x);
    }

}

void dfs(int x,int pre){
    for(auto i:to[x]){
        if(i == pre) continue;
        dfs(i,x);
        update(x,i);
    }

    ans = min(ans,-ma[x]+mi[x]+lma[x]-deep[x]+lmi[x]-deep[x]+1);
}

void solve() {
    cin>>n;

    for(int i = 1;i<=n;i++) cin>>v[i];

    for(int i = 1;i<=n;i++) to[i].clear();

    for(int i = 1;i<n;i++){
        int u,v;
        cin>>u>>v;
        to[u].push_back(v);
        to[v].push_back(u);
    }

    init(1,0);
    dfs(1,0);

    cout<<ans<<endl;

}
signed main() {
    std::ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    //cout<<fixed<<setprecision(8);
    int caset = 1;
    cin>>caset;

    for(int i = 1;i<=caset;i++){
        //cout<<"Case "<<i<<": "<<endl;
		solve();
	}

    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Wrong Answer
time: 193ms
memory: 27356kb

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
-7
-7
-7
-7
-7
-7
-7
-7
-7
-7
-7
-7
-7
-7
-7
-7
-7
-7
-7
-7
-7
-7
-7
-7
-7
-7
-7
-7
-7
-7
-7
-7
-7
-7
-7
-7
-7
-7
-7
-7
-7
-7
-7
-7
-7
-7
-7
-7
-7
-7
-7
-7
-7
-7
-7
-7
-7
-7
-7
-7
-7
-7
-7
-7
-7
-7
-7
-7
-7
-7
-7
-7
-7
-7
-7
-7
-7
-7
-7
-7
-7
-7
-7
-7
-7
-7
-7
-7
-7
-7
-7
-7
-7
-7
-7
-...

result:

wrong answer 6th lines differ - expected: '-6', found: '-7'