QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#485444#9110. Zayin and Treepropane#AC ✓489ms12752kbC++202.3kb2024-07-20 17:59:312024-07-20 17:59:32

Judging History

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

  • [2024-07-20 17:59:32]
  • 评测
  • 测评结果:AC
  • 用时:489ms
  • 内存:12752kb
  • [2024-07-20 17:59:31]
  • 提交

answer

#include<iostream>
#include<cstring>
#include<vector>
#include<array>
using namespace std;
using LL = long long;
const int maxn = 1e6 + 5;
vector<int> g[maxn];
int a[maxn];
int ans;
bool v[maxn];

int get_size(int u, int fa){
    if (v[u]) return 0;
    int ans = 1;
    for(auto j : g[u]){
        if (j == fa) continue;
        ans += get_size(j, u);
    }
    return ans;
}

int get_wc(int u, int fa, int tot, int &wc){
    if (v[u]) return 0;
    int sum = 1, mx = 0;
    for(auto j : g[u]){
        if (j == fa) continue;
        int t = get_wc(j, u, tot, wc);
        mx = max(mx, t);
        sum += t;
    }
    mx = max(mx, tot - sum);
    if (2 * mx <= tot){
        wc = u;
    }
    return sum;
}

void dfs(int u, int fa, int dep, int mn, int mx, vector<array<int, 3> > &p){
    if (v[u]) return;
    dep += 1;
    mn = min(mn, a[u]);
    mx = max(mx, a[u]);
    ans = min(ans, dep - mx + mn);
    p.push_back({dep, mn, mx});
    for(auto j : g[u]){
        if (j == fa or v[j]) continue;
        dfs(j, u, dep, mn, mx, p);
    }
}

void calc(int u){
    if (v[u]) return;
    get_wc(u, -1, get_size(u, -1), u);
    v[u] = true;
    int mn1 = 1e9, mn2 = 1e9;
    for(auto j : g[u]){
        if (v[j]) continue;
        vector<array<int, 3> > p;
        dfs(j, u, 1, a[u], a[u], p);
        for(auto [dep, mn, mx] : p){
            ans = min(ans, mn1 + dep + mn);
            ans = min(ans, mn2 + dep - mx);
        }
        for(auto [dep, mn, mx] : p){
            mn1 = min(mn1, dep - 1 - mx);
            mn2 = min(mn2, dep - 1 + mn);
        }
    }
    for(auto j : g[u]){
        calc(j);
    }
}

int main(){

#ifdef LOCAL
    freopen("data.in", "r", stdin);
    freopen("data.out", "w", stdout);
#endif

    cin.tie(0);
    cout.tie(0);
    ios::sync_with_stdio(0);

    int T;
    cin >> T;
    while(T--){
        int n;
        cin >> n;
        for(int i = 1; i <= n; i++){
            g[i].clear();
            v[i] = false;
        }
        ans = 1;
        for(int i = 1; i <= n; i++) cin >> a[i];
        for(int i = 0; i < n - 1; i++){
            int a, b;
            cin >> a >> b;
            g[a].push_back(b);
            g[b].push_back(a);
        }
        calc(1);
        cout << ans << '\n';
    }

}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 489ms
memory: 12752kb

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