QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#380939 | #8212. Football in Osijek | OOBMABTRAMS# | WA | 6ms | 29964kb | C++17 | 2.0kb | 2024-04-07 15:15:00 | 2024-04-07 15:15:00 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
const int mod=998244353;
const int N=500013;
typedef long long ll;
int a[N],in[N],R[N];
vector<int>mp[N];
int top[N],len[N];
void dfs(int x){for(auto i:mp[x])dfs(i),len[x]=max(len[i]+1,len[x]);}
void dfs2(int x,int tp){
top[x]=tp;
int sn=0;
for(auto i:mp[x])if(len[i]>=len[sn])sn=i;
for(auto i:mp[x])if(i==sn)dfs2(i,tp);else dfs2(i,i);
}
int f[N],sz[N],val[N];
int find(int x){return x==f[x]?x:f[x]=find(f[x]);}
void merge(int x,int y){x=find(x),y=find(y);if(x^y)f[x]=y,sz[y]+=sz[x],val[y]=max(val[y],val[x]);}
int zero[N],ans[N];
vector<int>l[N];
void solve(){
int n;
cin>>n;
for(int i=1;i<=n;i++)cin>>a[i],in[a[i]]++;
queue<int>q;
for(int i=1;i<=n;i++)R[i]=1;
for(int i=1;i<=n;i++)if(!in[i])q.push(i);
while(!q.empty()){
int x=q.front();
q.pop();R[x]=0;
if(!--in[a[x]])q.push(a[x]);
}
for(int i=1;i<=n;i++)if(R[i])sz[i]=1;else mp[a[i]].push_back(i);
for(int i=1;i<=n;i++)if(R[i])dfs(i),dfs2(i,i);
for(int i=1;i<=n;i++)f[i]=i,val[i]=len[i];
for(int i=1;i<=n;i++)merge(i,a[i]);
int mx=0;
for(int i=1;i<=n;i++){
mx=max(val[find(i)]+sz[find(i)],mx);
zero[sz[find(i)]]++,zero[sz[find(i)]+val[find(i)]+1]--;
}
for(int i=1;i<=mx;i++)zero[i]+=zero[i-1];
for(int i=1;i<=mx;i++)if(!zero[i])ans[i]=1;
for(int i=1;i<=n;i++)if(top[i]==i)l[find(i)].push_back(len[i]+1);
for(int i=1;i<=n;i++)sort(l[i].begin(),l[i].end(),greater<>());
for(int i=1;i<=n;i++)if(!l[i].empty())l[i][0]+=sz[find(i)]-1;
vector<int>ve;
for(int i=1;i<=n;i++)for(auto x:l[i])ve.push_back(x);
sort(ve.begin(),ve.end(),greater<>());
int nw=ve[0];
for(int i=1;i<ve.size();i++){
for(int j=nw+1;j<=nw+ve[i];j++)ans[j]=i;
nw+=ve[i];
}
for(int i=1;i<=n;i++)cout<<ans[i]<<' ';
}
signed main(){
ios::sync_with_stdio(false);
int T=1;
// cin>>T;
while(T--)solve();
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 4ms
memory: 29964kb
input:
5 2 3 1 3 5
output:
0 1 0 0 1
result:
ok 5 number(s): "0 1 0 0 1"
Test #2:
score: 0
Accepted
time: 6ms
memory: 29944kb
input:
1 1
output:
0
result:
ok 1 number(s): "0"
Test #3:
score: 0
Accepted
time: 0ms
memory: 29948kb
input:
2 2 2
output:
0 0
result:
ok 2 number(s): "0 0"
Test #4:
score: -100
Wrong Answer
time: 2ms
memory: 29716kb
input:
10 4 7 2 8 4 3 4 9 7 3
output:
1 1 1 0 0 0 0 1 1 2
result:
wrong answer 9th numbers differ - expected: '2', found: '1'