QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#549850#6638. TreelectionpigstdTL 1925ms16032kbC++201.6kb2024-09-06 22:09:082024-09-06 22:09:08

Judging History

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

  • [2024-09-06 22:09:08]
  • 评测
  • 测评结果:TL
  • 用时:1925ms
  • 内存:16032kb
  • [2024-09-06 22:09:08]
  • 提交

answer

#include<bits/stdc++.h>
#define int long long
#define ull unsigned long long
#define pb push_back
#define mp make_pair
#define pii pair<int,int>
#define x first
#define y second
#define vi vector<int>
#define vpi vector<pii>
#define all(x) (x).begin(),(x).end()
using namespace std;

inline int read()
{
    char c=getchar();int x=0;bool f=0;
    for(;!isdigit(c);c=getchar())f^=!(c^45);
    for(;isdigit(c);c=getchar())x=(x<<1)+(x<<3)+(c^48);
    if(f)x=-x;return x;
}

const int M=1e6+10;
int n,sz[M],fa[M],deg[M],f[M],tmp[M];

bool check(int u,int ans)
{
    while(u!=1)
        if (tmp[u]==0)return 0;
        else u=fa[u];
    return 1;
}

void solve()
{
    n=read();
    for (int i=1;i<=n;i++)deg[i]=0,sz[i]=1,tmp[i]=0;
    for (int i=2;i<=n;i++)fa[i]=read(),deg[fa[i]]++;
    for (int i=n;i>=2;i--)sz[fa[i]]+=sz[i];
    int tl=1,tr=n,ans=-1;
    while(tl<=tr)
    {
        int Mid=(tl+tr)>>1;
        for (int i=1;i<=n;i++)f[i]=deg[i];
        for (int i=n;i>=2;i--)
            if (f[i]>Mid)f[fa[i]]+=f[i]-Mid,f[i]=Mid;
        if (f[1]<=Mid)ans=Mid,tr=Mid-1;
        else tl=Mid+1;
    }
    for (int i=1;i<=n;i++)f[i]=deg[i];
    ans--;
    int cnt=0;
    for (int i=n;i>=2;i--)
        if (f[i]>ans)f[fa[i]]+=f[i]-ans,f[i]=ans,tmp[i]=1;
    for (int i=1;i<=n;i++)
    {
        if (sz[i]-1>ans+1)cout<<"1";
        else if (sz[i]-1<ans+1)cout<<"0";
        else if (f[1]==ans+1&&deg[i]==ans+1&&check(i,ans))cout<<"1";
        else cout<<"0";
    }
    cout<<"\n";
}

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

详细

Test #1:

score: 100
Accepted
time: 2ms
memory: 11832kb

input:

2
4
1 2 3
5
1 1 2 2

output:

1100
10000

result:

ok 2 lines

Test #2:

score: 0
Accepted
time: 1925ms
memory: 16032kb

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: -100
Time Limit Exceeded

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: