QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#320164#8212. Football in Osijekucup-team2112#WA 0ms3684kbC++202.8kb2024-02-03 14:16:342024-02-03 14:16:34

Judging History

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

  • [2024-02-03 14:16:34]
  • 评测
  • 测评结果:WA
  • 用时:0ms
  • 内存:3684kb
  • [2024-02-03 14:16:34]
  • 提交

answer

#include <bits/stdc++.h>

struct DSU {
    std::vector<int> f, siz, free;
    
    DSU() {}
    DSU(int n) {
        init(n);
    }
    
    void init(int n) {
        f.resize(n);
        free.resize(n);
        std::iota(f.begin(), f.end(), 0);
        siz.assign(n, 1);
    }
    
    int find(int x) {
        while (x != f[x]) {
            x = f[x] = f[f[x]];
        }
        return x;
    }
    
    bool same(int x, int y) {
        return find(x) == find(y);
    }
    
    bool merge(int x, int y) {
        x = find(x);
        y = find(y);
        if (x == y) {
            return false;
        }
        siz[x] += siz[y];
        f[y] = x;
        return true;
    }
    
    int size(int x) {
        return siz[find(x)];
    }
};

int main(){
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    std::cout.tie(nullptr);
    int n;
    std::cin >> n;
    DSU dsu(n + 1);
    std::vector adj(n + 1, std::vector<int>());
    std::vector<int> in(n + 1);
    std::vector<int> in_cycle(n + 1, 1);
    for (int i = 1; i <= n; i += 1) {
        int x;
        std::cin >> x;
        adj[x].push_back(i);
        dsu.merge(i, x);
        in[x] += 1;
    }
    std::queue<int> q;
    for (int i = 1; i <= n; i += 1) {
        if (in[i] == 0) {
            q.push(i);
        }
    }
    while(not q.empty()) {
        int u = q.front();
        q.pop();
        in_cycle[u] = 0;
        for (int v : adj[u]) {
            in[v] -= 1;
            if (in[v] == 0) {
                q.push(v);
            }
        }
    }
    for (int i = 1; i <= n; i += 1) {
        dsu.free[dsu.find(i)] += in_cycle[i] == 0;
    }
    std::vector<std::pair<int, int> > cycle;
    for (int i = 1; i <= n; i += 1) {
        if (dsu.find(i) == i) {
            cycle.push_back({dsu.size(i) - dsu.free[i], dsu.size(i)});
        }
    }
    std::sort(cycle.begin(), cycle.end(), [](auto a, auto b) {
        return a.second > b.second;
    });
    std::vector<int> cnt(n + 5);
    int mx = 0;
    for (auto [l, r] : cycle) {
        cnt[l] += 1;
        cnt[r + 1] -= 1;
        mx = std::max(mx, r);
    }
    std::vector<int> res(n + 1, -1);
    for (int i = 1; i <= n; i += 1) {
        cnt[i] += cnt[i - 1];
        if(cnt[i]) {
            res[i] = 0;
        }
    }
    for (int i = 1; i <= mx; i += 1) {
        if(res[i] == -1) {
            res[i] = 1;
        }
    }
    if (cycle.size() > 1) {
        int cur = cycle[0].second;
        for (int ans = 1; ans < cycle.size(); ans += 1) {
            cur += cycle[ans].second;
            while(mx <= cur) {
                if (res[mx] == -1) res[mx] = ans;
                mx += 1;
            }
        }
    }
    for (int i = 1; i <= n; i += 1) {
        std::cout << res[i] << " \n"[i == n];
    }
    return 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 0ms
memory: 3684kb

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: 3588kb

input:

1
1

output:

0

result:

ok 1 number(s): "0"

Test #3:

score: 0
Accepted
time: 0ms
memory: 3596kb

input:

2
2 2

output:

0 0

result:

ok 2 number(s): "0 0"

Test #4:

score: -100
Wrong Answer
time: 0ms
memory: 3572kb

input:

10
4 7 2 8 4 3 4 9 7 3

output:

1 1 1 1 1 0 0 0 0 0

result:

wrong answer 4th numbers differ - expected: '0', found: '1'