QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#770940#8212. Football in Osijektassei903TL 1ms3584kbC++231.8kb2024-11-22 03:49:232024-11-22 03:49:24

Judging History

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

  • [2024-11-22 03:49:24]
  • 评测
  • 测评结果:TL
  • 用时:1ms
  • 内存:3584kb
  • [2024-11-22 03:49:23]
  • 提交

answer

#include <bits/stdc++.h>

#define rep1(n) for(int i = 0; i < (int)n; i++)
#define rep2(i, n) for(int i = 0; i < (int)n; i++)
#define rep3(i, l, r) for(int i = (int)l; i < (int)r; i++)
#define overloadrep(a, b, c, x, ...) x
#define rep(...) overloadrep(__VA_ARGS__, rep3, rep2, rep1)(__VA_ARGS__)
#define all(v) v.begin(), v.end()
#define sz(v) (int)v.size()
using namespace std;

using ll = long long;
using vi = vector<int>;


void solve() {
    int n;cin >> n;
    vector<int> p(n);for (auto &x: p) cin >> x, x--;
    vector<int> deg(n, 0);
    rep(i, n)deg[p[i]]++;
    queue<int> q;
    rep(i, n)if (deg[i] == 0) q.push(i);
    vector<int> dp(n, 0);
    vector<int> order;
    while (!q.empty()) {
        int x = q.front();q.pop();
        order.emplace_back(x);
        dp[x] = -1;
        deg[p[x]]++;
        if (deg[p[x]] == 0)q.push(p[x]);
    }

    rep(i, n) {
        if (dp[i] != 0) continue;
        int cnt = 0, x = i;
        do {
            cnt++;
            x = p[x];
            
        } while (x != i);
        
        do {
            dp[x] = cnt;
        }while (x != i);
    }
    // rep(i, sz(order)) cout << order[i] << " ";
    // cout << endl;
    for (int i : order | views :: reverse) {
        dp[i] = dp[p[i]] + 1;
    }
    // rep(i, n) cout << dp[i] << " " ;
    // cout << endl;
    vector<bool> flag(n+1, 0);
    int m = 0;
    rep(i, n)flag[dp[i]] = true, m = max(m, dp[i]);
    
    rep(i, 1, n+1) {
        if (i > 1) cout << " ";
        if (flag[i]) cout << 0;
        else if (i <= m) cout << 1;
        else cout << i - m; 
    }
    cout << endl;
}

int main() {

    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int T = 1;
    // int T;cin >> T;
    while(T--) {
        solve();
    }
    
} 

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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: 1ms
memory: 3528kb

input:

2
2 2

output:

0 0

result:

ok 2 number(s): "0 0"

Test #4:

score: -100
Time Limit Exceeded

input:

10
4 7 2 8 4 3 4 9 7 3

output:


result: