Submission #1470279


Source Code Expand

Copy
#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
#include <vector>
#include <list>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <deque>
#include <complex>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <cstring>

#define REP(i,x) for(int i=0 ; i<(int)(x) ; i++)
#define ALL(x) (x).begin(),(x).end()
#define LL long long

using namespace std;

int N;
vector<int> from;
vector<vector<int> > to;

int get_not_exist_min(const vector<int> &vec, const vector<int> &A, bool second=false){
    int n = vec.size();
    vector<int> children(n);
    REP(i, vec.size()){
        int v = vec[i];
        if(A[v]<0 || A[v]>=n)continue;
        children[A[v]] = 1;
    }
    int res = 0;
    while(res<n && children[res])res++;
    if(second){
        res++;
        while(res<n && children[res])res++;
    }
    return res;
}

void bfs(queue<int> que, vector<int> &visit, vector<int> &res){
    while(!que.empty()){
        int u = que.front();que.pop();
        res[u] = get_not_exist_min(to[u], res);
        int v = from[u];
        visit[v]++;
        if(visit[v]!=(int)to[v].size())continue;
        que.push(v);
    }
}

bool check(queue<int> que, vector<int> visit, vector<int> res){
    bfs(que, visit, res);
    bool ok = true;
    REP(u, N){
        vector<int> cnt(res[u]);
        REP(i, to[u].size()){
            int v = to[u][i];
            if(res[v]==res[u])ok = false;
            if(res[v]<res[u])cnt[res[v]]++;
        }
        REP(i, cnt.size()){
            if(cnt[i]==0)ok = false;
        }
    }
    return ok;
}

int main(){
    cin >> N;
    from.resize(N);
    to.assign(N, vector<int>());

    REP(i, N){
        int p;
        cin >> p;
        p--;
        from[i] = p;
        to[p].push_back(i);
    }
    vector<int> res(N, -1);
    queue<int> que;
    REP(i, N)if((int)to[i].size()==0)que.push(i);
    vector<int> visit(N);
    bfs(que, visit, res);

    bool ok = false;
    REP(i, N){
        if(res[i]==-1){
            queue<int> que;
            que.push(from[i]);
            int first = get_not_exist_min(to[i], res, false);
            int second = get_not_exist_min(to[i], res, true);
            res[i] = first;
            visit[i]++;
            if(check(que, visit, res))ok = true;
            res[i] = second;
            if(check(que, visit, res))ok = true;
            break;
        }
    }

    cout << (ok ? "POSSIBLE" : "IMPOSSIBLE") << endl;
    return 0;
}

Submission Info

Submission Time
Task F - Namori Grundy
User nel215
Language C++14 (GCC 5.4.1)
Score 800
Code Size 2583 Byte
Status AC
Exec Time 127 ms
Memory 14208 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 800 / 800
Status
AC × 4
AC × 68
Set Name Test Cases
Sample example0, example1, example2, example3
All example0, example1, example2, example3, loop0, loop1, loop10, loop11, loop12, loop13, loop14, loop15, loop16, loop17, loop18, loop19, loop2, loop3, loop4, loop5, loop6, loop7, loop8, loop9, loopex0, loopex1, loopex10, loopex11, loopex12, loopex13, loopex14, loopex15, loopex16, loopex17, loopex18, loopex19, loopex2, loopex20, loopex21, loopex22, loopex23, loopex3, loopex4, loopex5, loopex6, loopex7, loopex8, loopex9, rand0, rand1, rand2, rand3, rand4, rand5, rand6, rand7, rand8, rand9, small0, small1, small2, small3, small4, small5, small6, small7, small8, small9
Case Name Status Exec Time Memory
example0 AC 1 ms 256 KB
example1 AC 1 ms 256 KB
example2 AC 1 ms 256 KB
example3 AC 1 ms 256 KB
loop0 AC 99 ms 11392 KB
loop1 AC 100 ms 11396 KB
loop10 AC 100 ms 11400 KB
loop11 AC 100 ms 11396 KB
loop12 AC 100 ms 11396 KB
loop13 AC 100 ms 11400 KB
loop14 AC 100 ms 11396 KB
loop15 AC 100 ms 11396 KB
loop16 AC 100 ms 11400 KB
loop17 AC 99 ms 11396 KB
loop18 AC 100 ms 11396 KB
loop19 AC 99 ms 11396 KB
loop2 AC 100 ms 11400 KB
loop3 AC 100 ms 11396 KB
loop4 AC 99 ms 11396 KB
loop5 AC 100 ms 11396 KB
loop6 AC 100 ms 11400 KB
loop7 AC 99 ms 11400 KB
loop8 AC 97 ms 11400 KB
loop9 AC 100 ms 11396 KB
loopex0 AC 102 ms 11644 KB
loopex1 AC 101 ms 11524 KB
loopex10 AC 99 ms 11400 KB
loopex11 AC 101 ms 11392 KB
loopex12 AC 100 ms 11392 KB
loopex13 AC 100 ms 11392 KB
loopex14 AC 100 ms 11396 KB
loopex15 AC 100 ms 11524 KB
loopex16 AC 102 ms 11644 KB
loopex17 AC 100 ms 11524 KB
loopex18 AC 103 ms 11768 KB
loopex19 AC 106 ms 11396 KB
loopex2 AC 99 ms 11396 KB
loopex20 AC 102 ms 11396 KB
loopex21 AC 98 ms 11644 KB
loopex22 AC 101 ms 11648 KB
loopex23 AC 100 ms 11396 KB
loopex3 AC 102 ms 11400 KB
loopex4 AC 100 ms 11396 KB
loopex5 AC 102 ms 11640 KB
loopex6 AC 99 ms 11400 KB
loopex7 AC 101 ms 11520 KB
loopex8 AC 101 ms 11516 KB
loopex9 AC 102 ms 11644 KB
rand0 AC 29 ms 3584 KB
rand1 AC 44 ms 5352 KB
rand2 AC 12 ms 1664 KB
rand3 AC 100 ms 10092 KB
rand4 AC 127 ms 14208 KB
rand5 AC 51 ms 6088 KB
rand6 AC 21 ms 2816 KB
rand7 AC 6 ms 896 KB
rand8 AC 76 ms 8500 KB
rand9 AC 5 ms 896 KB
small0 AC 1 ms 256 KB
small1 AC 1 ms 256 KB
small2 AC 1 ms 256 KB
small3 AC 1 ms 256 KB
small4 AC 1 ms 256 KB
small5 AC 1 ms 256 KB
small6 AC 1 ms 256 KB
small7 AC 1 ms 256 KB
small8 AC 1 ms 256 KB
small9 AC 1 ms 256 KB