QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#320824 | #8212. Football in Osijek | ucup-team1134# | WA | 0ms | 3828kb | C++23 | 2.5kb | 2024-02-03 22:03:31 | 2024-02-03 22:03:31 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return true; } return false; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return true; } return false; }
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define mp make_pair
#define si(x) int(x.size())
const int mod=998244353,MAX=500005,INF=1<<30;
struct UF{
int n;
vector<int> par,size,edge;
void init(int n_){
n=n_;
par.assign(n,-1);
size.assign(n,1);
edge.assign(n,0);
for(int i=0;i<n;i++){
par[i]=i;
}
}
int root(int a){
if(par[a]==a) return a;
else return par[a]=root(par[a]);
}
void unite(int a,int b){
edge[root(a)]++;
if(root(a)!=root(b)){
size[root(a)]+=size[root(b)];
edge[root(a)]+=edge[root(b)];
par[root(b)]=root(a);
}
}
bool check(int a,int b){
return root(a)==root(b);
}
};
int main(){
std::ifstream in("text.txt");
std::cin.rdbuf(in.rdbuf());
cin.tie(0);
ios::sync_with_stdio(false);
int N;cin>>N;
vector<int> A(N),deg(N);
UF uf;uf.init(N);
for(int i=0;i<N;i++){
int x;cin>>x;x--;
A[i]=x;
deg[x]++;
uf.unite(i,x);
}
queue<int> Q;
for(int i=0;i<N;i++){
if(deg[i]==0) Q.push(i);
}
while(!Q.empty()){
int u=Q.front();Q.pop();
deg[A[u]]--;
if(deg[A[u]]==0) Q.push(A[u]);
}
vector<int> seen(N);
vector<pair<int,int>> S;
for(int i=0;i<N;i++){
if(deg[i]&&!seen[i]){
int now=i,cn=0;
while(1){
seen[now]=true;
now=A[now];
cn++;
if(seen[now]) break;
}
S.push_back(mp(cn,uf.size[uf.root(i)]));
}
}
sort(all(S),[](auto a,auto b){
return a.se>b.se;
});
vector<int> ans(N+1,INF);
int sum=0,cn=0;
for(auto [a,b]:S){
for(int x=a;x<=b;x++) chmin(ans[x],0);
for(int x=1;x<a;x++) chmin(ans[x],1);
if(cn){
for(int x=sum+1;x<=sum+b;x++) chmin(ans[x],cn);
}
sum+=b;
cn++;
}
for(int i=1;i<=N;i++){
if(i>1) cout<<" ";
cout<<ans[i];
}
cout<<endl;
}
詳細信息
Test #1:
score: 100
Accepted
time: 0ms
memory: 3828kb
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: 0ms
memory: 3628kb
input:
1 1
output:
0
result:
ok 1 number(s): "0"
Test #3:
score: 0
Accepted
time: 0ms
memory: 3700kb
input:
2 2 2
output:
0 0
result:
ok 2 number(s): "0 0"
Test #4:
score: -100
Wrong Answer
time: 0ms
memory: 3524kb
input:
10 4 7 2 8 4 3 4 9 7 3
output:
1 1 1 0 0 0 0 0 0 0
result:
wrong answer 8th numbers differ - expected: '1', found: '0'