QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#329100#8212. Football in OsijekthisisatestRE 0ms0kbC++173.3kb2024-02-16 13:24:352024-02-16 13:24:35

Judging History

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

  • [2024-02-16 13:24:35]
  • 评测
  • 测评结果:RE
  • 用时:0ms
  • 内存:0kb
  • [2024-02-16 13:24:35]
  • 提交

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]){
                    bfs.push(child);
                }
            }
            curr = preference[curr];
        }

        max_cycle = max(max_cycle,cycle);
        vector<int> leaves;
        
        while (!bfs.empty()){

            int num = bfs.front();
            bfs.pop();
            if (children[num].size()==0){
                leaves.push_back(num);
            }else{
                /*for each loop*/
                for (int child:children[num]){
                    bfs.push(child);
                }
            }

        }
        reverse(leaves.begin(),leaves.end());
        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);
        }
        sort(chains.rbegin(),chains.rend());

        answer[cycle] = 0;

        if (chains.size()>0){
            for (int j=1;j<=chains[0];j++){
                answer[cycle+j] = 0;
            }
            all_chains.push_back(cycle+chains[0]);
            for (int j=1;j<chains.size();j++){
                all_chains.push_back(chains[j]);
            }
            
        }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;          
        }
        val += all_chains[i];
    }
    for (int i=1;i<=n;i++){
        cout<<answer[i]<<" ";
    }
    return 0;
}
/*
5
2 3 1 3 5
*/

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Runtime Error

input:

5
2 3 1 3 5

output:


result: