답안 #261073

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
261073 2020-08-11T11:10:32 Z georgerapeanu 낙하산 고리들 (IOI12_rings) C++11
0 / 100
1075 ms 63364 KB
#pragma once

#include <vector>
#include <algorithm>
#include <cstdio>
#include <queue>

using namespace std;

const int NMAX = 1e6;

int n;

vector<int> graph[NMAX + 5];

int dfs(int nod,int tata,vector<int> &viz){
    viz[nod] = 1;
    
    int found = 0;

    for(auto it:graph[nod]){
        if(viz[it] == 2){
            found = 1;
        }
        if(it == tata){
            continue;
        }
        if(viz[it] == 0){
            if(dfs(it,nod,viz) == 0){
                return 0;
            }
        }
        else if(viz[it] == 1){
            return 0;
        }
    }

    if(graph[nod].size() - found > 2){
        return 0;
    }

    return 1;
}

bool is_chain(int nod,vector<int> &viz){
    queue<int> q;
    vector<int> nodes;
    q.push(nod);
    viz[nod] = 1;

    while(!q.empty()){
        int nod = q.front();
        nodes.push_back(nod);
        q.pop();
        for(auto it:graph[nod]){
            if(viz[it] == 0){
                q.push(it);
                viz[it] = 1;
            }
        }
    }

    bool ok = true;
    bool found = false;

    for(auto it:nodes){
        ok &= (graph[it].size() <= 2);
        found |= (graph[it].size() <= 1);
    }

    for(auto it:nodes){
        viz[it] = 0;
    }
    
    return ok & found;
}

int bfs(int nod,vector<int> &viz){
    queue<int> q;
    vector<int> nodes;
    q.push(nod);
    viz[nod] = 1;

    while(!q.empty()){
        int nod = q.front();
        nodes.push_back(nod);
        q.pop();
        for(auto it:graph[nod]){
            if(viz[it] == 0){
                q.push(it);
                viz[it] = 1;
            }
        }
    }

    vector<int> candidates = nodes;

    bool big2 = false;

    for(auto it:nodes){
        if(graph[it].size() > 3){
            bool found = false;
            for(auto it2:candidates){
                found |= (it == it2);
            }
            candidates = {};
            if(found){
                candidates.push_back(it);
            }
            big2 = true;
        }
        else if(graph[it].size() == 3){
            if(big2 == false){
                candidates = graph[it];
                candidates.push_back(it);
            }
            else{
                vector<int> tmp;
                for(auto it2:candidates){
                    if(it2 == it || find(graph[it].begin(),graph[it].end(),it2) != graph[it].end()){
                        tmp.push_back(it2);
                    }
                }
                candidates.swap(tmp);
            }
            big2 = true;
        }
    }

    if(big2 == false)return nodes.size();

    int ans = 0;


    for(auto it:candidates){
        for(auto it2:nodes){
            viz[it2] = 0;
        }
        viz[it] = 2;
        bool ok = true;
        for(auto it2:graph[it]){
            if(viz[it2] == 0){
                ok &= dfs(it2,it,viz);
            }
        }
        ans += ok;
    }
    for(auto it:nodes){
        viz[it] = 1;
    }
    return ans;
}

void Init(int N_) {
    n = N_;

}

void Link(int A, int B) {
    A++;B++;
    graph[A].push_back(B);
    graph[B].push_back(A);
}

int CountCritical() {

    vector<int> viz(n + 1,0);

    int ans = 0;
    
    vector<int> data;
    vector<int> nonchains;

    for(int i = 1;i <= n;i++){
        if(viz[i] == false){
            if(is_chain(i,viz) == 0){
                nonchains.push_back(i - 1);
            }
            data.push_back(bfs(i,viz));
        }
    }

    if(nonchains.empty()){
        for(auto it:data){
            ans += it;
        }
    }
    else if(nonchains.size() == 1){
        ans = data[nonchains[0]];
    }

    return ans;

}

Compilation message

rings.cpp:1:9: warning: #pragma once in main file
 #pragma once
         ^~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 23808 KB Output is correct
2 Incorrect 19 ms 24060 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 552 ms 49016 KB Output is correct
2 Incorrect 1075 ms 63364 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 23808 KB Output is correct
2 Incorrect 19 ms 24060 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 23808 KB Output is correct
2 Incorrect 19 ms 24060 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 23808 KB Output is correct
2 Incorrect 19 ms 24060 KB Output isn't correct
3 Halted 0 ms 0 KB -