QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#592048#6638. TreelectionKandarp08WA 1391ms82340kbC++203.3kb2024-09-26 20:17:182024-09-26 20:17:19

Judging History

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

  • [2024-09-26 20:17:19]
  • 评测
  • 测评结果:WA
  • 用时:1391ms
  • 内存:82340kb
  • [2024-09-26 20:17:18]
  • 提交

answer

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

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

#define MOD 1000000007
#define HASH_MOD1 998244353
#define HASH_MOD2 1000000009
#define HASH 31
#define ll long long
#define int long long

typedef __gnu_pbds::tree<int, __gnu_pbds::null_type, less<int>, __gnu_pbds::rb_tree_tag, __gnu_pbds::tree_order_statistics_node_update> ordered_set;

template <typename T>
istream& operator>> (istream& in, vector<T>& a)
{
    for (int i = 0; i < a.size(); ++i)
        in >> a[i];

    return in;
}

template <typename T>
ostream& operator<< (ostream& out, vector<T>& a)
{
    for (int i = 0; i < a.size(); ++i)
        out << a[i] << " ";

    out << "\n";

    return out;
}

template <typename T>
ostream& operator<< (ostream& out, set<T>& s)
{
    for (int x : s)
        out << x << " ";

    out << "\n";

    return out;
}

template <typename T>
ostream& operator<< (ostream& out, vector<pair<T, T>>& v)
{
    for (int i = 0; i < v.size(); ++i)
        out << v[i].first << " " << v[i].second << "\n";

    return out;
}

ll modinv(ll a)
{
    return a <= 1 ? a : MOD - (MOD / a) * modinv(MOD % a) % MOD; 
}

ll binpow(ll x, ll y)
{
    ll ans = 1;

    while (y > 0)
    {
        if (y % 2 == 1)
            ans *= x;

        x = x * x;
        y = y / 2;

        x %= MOD;
        ans %= MOD;
    }

    return ans;
}

void init(vector<vector<int>>& children, vector<int>& size, int u)
{
    size[u] = 1;

    for (int v : children[u])
    {
        init(children, size, v);
        size[u] += size[v];
    }
}

int dfs(vector<vector<int>>& children, vector<int>& got, vector<bool>& passed, int u, int z)
{
    int res = 0;
    got[u] = 0;
    passed[u] = false;

    for (int v : children[u])
        res += dfs(children, got, passed, v, z);

    got[u] = res;

    if (got[u] > z)
    {
        res = got[u] - z;
        passed[u] = true;
    }

    else
    {
        res = 0;
        passed[u] = false;
    }

    return res + (u != 0);
}

void solve()
{
    int n, i;
    cin >> n;

    vector<vector<int>> children(n);
    vector<int> p(n), size(n), got(n);
    vector<bool> passed(n);

    for (i = 1; i < n; ++i)
    {
        cin >> p[i];
        children[p[i] - 1].push_back(i);
    }

    init(children, size, 0);

    int low = 1;
    int high = n;
    int ans = n;

    while (low <= high)
    {
        int mid = (low + high) / 2;

        dfs(children, got, passed, 0, mid);

        if (got[0] <= mid)
        {
            ans = mid;
            high = mid - 1;
        }

        else
            low = mid + 1;
    }    

    string s(n, '0');
    s[0] = '1';

    for (i = 0; i < n; ++i)
    {
        if (size[i] - 1 > ans)
            s[i] = '1';
    }

    dfs(children, got, passed, 0, ans - 1);

    if (got[0] == ans)
    {
        passed[0] = true;

        for (i = 1; i < n; ++i)
        {
            passed[i] = (passed[i] && passed[p[i]]);

            if ((size[i] == ans + 1) && passed[i])
                s[i] = '1';
        }
    }

    cout << s << "\n";
}

int32_t main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    int t = 1;
    cin >> t;

    while (t--)
    {
        solve();
    }
}

详细

Test #1:

score: 100
Accepted
time: 0ms
memory: 3772kb

input:

2
4
1 2 3
5
1 1 2 2

output:

1100
10000

result:

ok 2 lines

Test #2:

score: 0
Accepted
time: 195ms
memory: 11460kb

input:

10
100000
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 10...

output:

111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111...

result:

ok 10 lines

Test #3:

score: 0
Accepted
time: 256ms
memory: 82340kb

input:

1
1000000
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 10...

output:

111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111...

result:

ok single line: '111111111111111111111111111111...0000000000000000000000000000000'

Test #4:

score: 0
Accepted
time: 1391ms
memory: 64032kb

input:

1
1000000
1 2 2 3 5 1 7 3 9 8 4 11 9 6 10 12 10 13 16 18 14 16 7 24 23 21 14 8 26 27 17 21 33 24 20 17 5 34 29 34 37 20 12 22 39 44 33 37 32 27 31 25 30 46 39 26 53 4 11 55 41 29 6 22 32 36 52 57 65 38 59 67 51 56 30 61 36 64 62 19 51 63 53 83 38 75 31 41 49 43 60 18 62 67 91 81 44 25 55 98 86 64 46...

output:

111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111...

result:

ok single line: '111111111111111111111111111111...0000000000000000000000000000000'

Test #5:

score: -100
Wrong Answer
time: 167ms
memory: 9996kb

input:

65764
13
1 1 1 1 1 1 1 1 2 10 10 11
11
1 1 2 2 2 4 4 4 5 10
14
1 2 2 3 3 3 4 4 5 6 10 12 13
14
1 2 2 3 3 3 4 5 5 6 6 7 8
13
1 2 2 3 3 4 5 5 5 6 8 12
13
1 1 1 2 2 3 5 7 7 7 9 9
12
1 1 1 2 2 3 5 7 7 9 10
14
1 1 2 2 3 4 5 6 7 7 8 12 12
14
1 2 3 4 4 5 5 5 7 7 7 10 13
14
1 1 1 1 1 2 2 2 3 10 10 10 11
13
...

output:

1000000000000
11000000000
11101000010000
11100000000000
1110100000000
1010001000000
101000100000
11011001000000
11111010000000
10000000000000
1111010000000
111000100010000
111010001000
11000000000000
11100000000000
11001001000000
11000000000000
11111000000000
110011000000
1010000100000
1100111010000...

result:

wrong answer 120th lines differ - expected: '1100100000000', found: '1100000000000'