Submission #261140

# Submission time Handle Problem Language Result Execution time Memory
261140 2020-08-11T12:32:42 Z georgerapeanu Parachute rings (IOI12_rings) C++11
55 / 100
4000 ms 75096 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(data.size());
            }
            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
         ^~~~
# Verdict Execution time Memory Grader output
1 Correct 16 ms 23808 KB Output is correct
2 Correct 19 ms 23968 KB Output is correct
3 Correct 21 ms 24064 KB Output is correct
4 Correct 17 ms 23808 KB Output is correct
5 Correct 18 ms 23936 KB Output is correct
6 Correct 20 ms 24056 KB Output is correct
7 Correct 18 ms 23928 KB Output is correct
8 Correct 22 ms 23936 KB Output is correct
9 Correct 20 ms 24064 KB Output is correct
10 Correct 20 ms 24064 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 560 ms 48120 KB Output is correct
2 Correct 1070 ms 58204 KB Output is correct
3 Correct 1358 ms 75052 KB Output is correct
4 Correct 1408 ms 72568 KB Output is correct
5 Correct 1373 ms 72652 KB Output is correct
6 Correct 1459 ms 75096 KB Output is correct
7 Correct 1279 ms 73616 KB Output is correct
8 Correct 1243 ms 69880 KB Output is correct
9 Correct 1398 ms 73472 KB Output is correct
10 Correct 1185 ms 70800 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 16 ms 23808 KB Output is correct
2 Correct 19 ms 23968 KB Output is correct
3 Correct 21 ms 24064 KB Output is correct
4 Correct 17 ms 23808 KB Output is correct
5 Correct 18 ms 23936 KB Output is correct
6 Correct 20 ms 24056 KB Output is correct
7 Correct 18 ms 23928 KB Output is correct
8 Correct 22 ms 23936 KB Output is correct
9 Correct 20 ms 24064 KB Output is correct
10 Correct 20 ms 24064 KB Output is correct
11 Correct 75 ms 24056 KB Output is correct
12 Correct 234 ms 24424 KB Output is correct
13 Correct 238 ms 24496 KB Output is correct
14 Correct 260 ms 24184 KB Output is correct
15 Correct 668 ms 24520 KB Output is correct
16 Correct 249 ms 24328 KB Output is correct
17 Correct 241 ms 24284 KB Output is correct
18 Correct 655 ms 24728 KB Output is correct
19 Correct 267 ms 24360 KB Output is correct
20 Correct 276 ms 24628 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 16 ms 23808 KB Output is correct
2 Correct 19 ms 23968 KB Output is correct
3 Correct 21 ms 24064 KB Output is correct
4 Correct 17 ms 23808 KB Output is correct
5 Correct 18 ms 23936 KB Output is correct
6 Correct 20 ms 24056 KB Output is correct
7 Correct 18 ms 23928 KB Output is correct
8 Correct 22 ms 23936 KB Output is correct
9 Correct 20 ms 24064 KB Output is correct
10 Correct 20 ms 24064 KB Output is correct
11 Correct 75 ms 24056 KB Output is correct
12 Correct 234 ms 24424 KB Output is correct
13 Correct 238 ms 24496 KB Output is correct
14 Correct 260 ms 24184 KB Output is correct
15 Correct 668 ms 24520 KB Output is correct
16 Correct 249 ms 24328 KB Output is correct
17 Correct 241 ms 24284 KB Output is correct
18 Correct 655 ms 24728 KB Output is correct
19 Correct 267 ms 24360 KB Output is correct
20 Correct 276 ms 24628 KB Output is correct
21 Execution timed out 4059 ms 24912 KB Time limit exceeded
22 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 16 ms 23808 KB Output is correct
2 Correct 19 ms 23968 KB Output is correct
3 Correct 21 ms 24064 KB Output is correct
4 Correct 17 ms 23808 KB Output is correct
5 Correct 18 ms 23936 KB Output is correct
6 Correct 20 ms 24056 KB Output is correct
7 Correct 18 ms 23928 KB Output is correct
8 Correct 22 ms 23936 KB Output is correct
9 Correct 20 ms 24064 KB Output is correct
10 Correct 20 ms 24064 KB Output is correct
11 Correct 560 ms 48120 KB Output is correct
12 Correct 1070 ms 58204 KB Output is correct
13 Correct 1358 ms 75052 KB Output is correct
14 Correct 1408 ms 72568 KB Output is correct
15 Correct 1373 ms 72652 KB Output is correct
16 Correct 1459 ms 75096 KB Output is correct
17 Correct 1279 ms 73616 KB Output is correct
18 Correct 1243 ms 69880 KB Output is correct
19 Correct 1398 ms 73472 KB Output is correct
20 Correct 1185 ms 70800 KB Output is correct
21 Correct 75 ms 24056 KB Output is correct
22 Correct 234 ms 24424 KB Output is correct
23 Correct 238 ms 24496 KB Output is correct
24 Correct 260 ms 24184 KB Output is correct
25 Correct 668 ms 24520 KB Output is correct
26 Correct 249 ms 24328 KB Output is correct
27 Correct 241 ms 24284 KB Output is correct
28 Correct 655 ms 24728 KB Output is correct
29 Correct 267 ms 24360 KB Output is correct
30 Correct 276 ms 24628 KB Output is correct
31 Execution timed out 4059 ms 24912 KB Time limit exceeded
32 Halted 0 ms 0 KB -