QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#504572#9101. Zayin and Buslearner__#WA 1ms3656kbC++201.7kb2024-08-04 13:54:562024-08-04 13:54:56

Judging History

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

  • [2024-08-04 13:54:56]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:3656kb
  • [2024-08-04 13:54:56]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;

const int N = 1e5 + 10;
int n;
vector<int> g[N];
int a[N];
multiset<array<int, 2>> st;
int deg[N];
int fa[N];

void dfs(int x, int father, int dep)
{
    fa[x] = father;
    int ok = 1;
    for(auto &to : g[x])
    {
        if(to == father) continue;
        ok = 0;
        deg[x]++;
        dfs(to, x, dep + 1);
    }
    if(ok)
        st.insert({dep, x});
}

int check(int x)
{
    st.clear();
    for(int i = 0; i <= n; i++) deg[i] = 0;

    dfs(1, -1, 0);

    for(int i = 1; i <= n; i++)
    {
        int mx_d = x - i - a[i];
        auto it = st.upper_bound(array<int, 2>{mx_d + 1, -1});

        if(it == st.begin()) return 0;
        it--;
        array<int, 2> tmp = *it;
        st.erase(it);

        deg[fa[tmp[1]]]--;

        if(tmp[0] && deg[fa[tmp[1]]] == 0) st.insert({tmp[0] - 1, fa[tmp[1]]});

        // cerr << i << ' ' << mx_d << ' ' << fa[tmp[1]] << ' '<< deg[fa[tmp[1]]] << ' ' << tmp[0] << ' ' << st.size() << '\n';
    }

    return 1;
}

void solve()
{
    cin >> n;
    for(int i = 1; i <= n; i++) g[i].clear();
    for(int i = 1; i < n; i++)
    {
        int to;
        cin >> to;
        g[to].push_back(i + 1);
        g[i + 1].push_back(to);
    }
    for(int i = 1; i <= n; i++) cin >> a[i];

    int l = 1, r = 2e8, ans;
    while(l <= r)
    {
        int mid = (l + r) / 2;
        if(check(mid)) r = mid - 1, ans = mid;
        else l = mid + 1;
    }

    cout << ans << '\n';
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);

    int t = 1;
    cin >> t;
    while(t--) solve();
    return 0;
}

详细

Test #1:

score: 0
Wrong Answer
time: 1ms
memory: 3656kb

input:

14
1
1
1
2
1
3
2
1
1 1
2
1
1 2
2
1
2 1
2
1
1 3
2
1
3 1
2
1
1 4
2
1
4 1
3
1 1
1 1 1
3
1 2
1 1 1
3
1 1
1 3 2
3
1 2
1 3 2

output:

2
3
4
3
4
4
5
5
6
6
4
4
6
6

result:

wrong answer 8th lines differ - expected: '4', found: '5'