Submission #1470230


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){
        int cnt = 0;
        REP(i, to[u].size()){
            int v = to[u][i];
            if(res[v]==res[u])ok = false;
            if(res[v]<res[u])cnt++;
        }
        if(res[u]!=cnt)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 0
Code Size 2521 Byte
Status WA
Exec Time 127 ms
Memory 14208 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 0 / 800
Status
AC × 4
AC × 24
WA × 44
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 WA 97 ms 11392 KB
loop1 AC 95 ms 11396 KB
loop10 AC 95 ms 11400 KB
loop11 WA 93 ms 11396 KB
loop12 AC 91 ms 11396 KB
loop13 AC 94 ms 11400 KB
loop14 AC 94 ms 11396 KB
loop15 AC 94 ms 11396 KB
loop16 AC 92 ms 11400 KB
loop17 AC 94 ms 11396 KB
loop18 AC 93 ms 11396 KB
loop19 AC 96 ms 11396 KB
loop2 AC 95 ms 11400 KB
loop3 WA 93 ms 11396 KB
loop4 WA 95 ms 11396 KB
loop5 AC 96 ms 11396 KB
loop6 AC 105 ms 11400 KB
loop7 AC 96 ms 11400 KB
loop8 AC 90 ms 11400 KB
loop9 AC 95 ms 11396 KB
loopex0 WA 99 ms 11644 KB
loopex1 WA 99 ms 11524 KB
loopex10 WA 91 ms 11400 KB
loopex11 AC 93 ms 11520 KB
loopex12 WA 95 ms 11392 KB
loopex13 WA 99 ms 11392 KB
loopex14 WA 92 ms 11396 KB
loopex15 WA 92 ms 11524 KB
loopex16 WA 93 ms 11644 KB
loopex17 WA 93 ms 11396 KB
loopex18 WA 100 ms 11768 KB
loopex19 WA 94 ms 11396 KB
loopex2 WA 92 ms 11396 KB
loopex20 WA 96 ms 11396 KB
loopex21 WA 109 ms 11644 KB
loopex22 WA 102 ms 11648 KB
loopex23 WA 96 ms 11396 KB
loopex3 WA 101 ms 11400 KB
loopex4 WA 97 ms 11396 KB
loopex5 WA 96 ms 11640 KB
loopex6 AC 98 ms 11400 KB
loopex7 WA 99 ms 11520 KB
loopex8 WA 94 ms 11516 KB
loopex9 WA 98 ms 11644 KB
rand0 WA 27 ms 3584 KB
rand1 WA 41 ms 5352 KB
rand2 WA 11 ms 1664 KB
rand3 WA 84 ms 10092 KB
rand4 WA 127 ms 14208 KB
rand5 WA 48 ms 6088 KB
rand6 WA 20 ms 2816 KB
rand7 WA 5 ms 896 KB
rand8 WA 68 ms 8500 KB
rand9 WA 5 ms 896 KB
small0 WA 1 ms 256 KB
small1 WA 1 ms 256 KB
small2 WA 1 ms 256 KB
small3 WA 1 ms 256 KB
small4 WA 1 ms 256 KB
small5 WA 1 ms 256 KB
small6 AC 1 ms 256 KB
small7 WA 1 ms 256 KB
small8 AC 1 ms 256 KB
small9 WA 1 ms 256 KB