QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#328819 | #8212. Football in Osijek | thisisatest | WA | 0ms | 3628kb | C++17 | 3.2kb | 2024-02-16 07:37:42 | 2024-02-16 07:37:43 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
int main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
//ifstream cin("input.txt");
int n;
cin>>n;
vector<int> preference(n+1);
vector<vector<int>> children(n+1);
for (int i=1;i<=n;i++){
int num;
cin>>num;
preference[i] = num;
children[num].push_back(i);
}
vector<int> num_children(n+1);
queue<int> q;
for (int i=1;i<=n;i++){
num_children[i] = children[i].size();
if (num_children[i]==0){
q.push(i);
}
}
vector<bool> part_of_cycle(n+1,true);
while (!q.empty()){
int node = q.front();
q.pop();
part_of_cycle[node] = false;
num_children[preference[node]]--;
if (num_children[preference[node]]==0){
q.push(preference[node]);
}
}
vector<bool> used(n+1);
vector<int> answer(n+1,-1);
vector<int> all_chains;
int max_cycle = 0;
for (int i=1;i<=n;i++){
if (used[i] || !part_of_cycle[i]){
continue;
}
vector<int> chains;
queue<int> bfs;
int cycle = 0;
int curr = i;
while (!used[curr]){
cycle++;
used[curr] = true;
for (int child:children[curr]){
if (!part_of_cycle[child]){
q.push(child);
}
}
curr = preference[curr];
}
max_cycle = max(max_cycle,cycle);
vector<int> leaves;
while (!q.empty()){
for (int x=0;x<q.size();x++){
int num = q.front();
q.pop();
if (children[num].size()==0){
leaves.push_back(num);
}else{
/*for each loop*/
for (int child:children[num]){
q.push(child);
}
}
}
}
for (int leaf:leaves){
int curr = leaf;
int length = 0;
while (!part_of_cycle[curr]){
length++;
used[curr] = true;
part_of_cycle[curr] = true;
curr = preference[curr];
}
chains.push_back(length);
}
answer[cycle] = 0;
if (chains.size()>0){
for (int j=1;j<=chains.back();j++){
answer[cycle+j] = 0;
}
all_chains.push_back(cycle+chains.back());
chains.pop_back();
for (int chain:chains){
all_chains.push_back(chain);
}
}else{
all_chains.push_back(cycle);
}
}
for (int i=1;i<max_cycle;i++){
if (answer[i]!=0){
answer[i] = 1;
}
}
sort(all_chains.rbegin(),all_chains.rend());
int val = all_chains[0];
for (int i=1;i<all_chains.size();i++){
for (int j=val+1;j<=val+all_chains[i];j++){
answer[j] = i;
}
}
for (int i=1;i<=n;i++){
cout<<answer[i]<<" ";
}
return 0;
}
/*
5
2 3 1 3 5
*/
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 3600kb
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: 3560kb
input:
1 1
output:
0
result:
ok 1 number(s): "0"
Test #3:
score: 0
Accepted
time: 0ms
memory: 3628kb
input:
2 2 2
output:
0 0
result:
ok 2 number(s): "0 0"
Test #4:
score: -100
Wrong Answer
time: 0ms
memory: 3628kb
input:
10 4 7 2 8 4 3 4 9 7 3
output:
1 1 1 0 0 3 1 1 -1 -1
result:
wrong answer 6th numbers differ - expected: '0', found: '3'