Submission #233348

# Submission time Handle Problem Language Result Execution time Memory
233348 2020-05-20T09:44:20 Z Mercenary Parachute rings (IOI12_rings) C++14
100 / 100
710 ms 41592 KB
#include<bits/stdc++.h>

using namespace std;
#define taskname "A"
#define pb	push_back
#define mp  make_pair
typedef pair<int,int> ii;

typedef long double ld;
typedef long long ll;

const int maxn = 1e6 + 5;

int adj[maxn][2];

int n;

struct G{
    bitset<maxn> is_link;
    int lab[maxn];
    int banned = -1;
    bool ok = 1;
    void init(int u){
        banned = u;
        for(int i = 0 ; i < n ; ++i)lab[i] = -1;
        for(int i = 0 ; i < n ; ++i){
            for(int j = 0 ; j < 2 ; ++j){
                if(adj[i][j] != -1 && i > adj[i][j]){
                    Connect(i,adj[i][j]);
                }
            }
        }
    }
    int FindLab(int u){return lab[u] < 0 ? u : lab[u] = FindLab(lab[u]);}
    void Connect(int u , int v){
        if(ok == 0 && banned != -1)return;
        if(u == banned)is_link[v] = 1;
        if(v == banned)is_link[u] = 1;
        if(u == banned || v == banned)return;
        u = FindLab(u);v = FindLab(v);
        if(u == v){
            ok = 0;
            return;
        }
        if(lab[u] > lab[v])swap(u , v);
        lab[u] += lab[v];
        lab[v] = u;
    }

}a[4];

void Init(int _n){
    memset(adj,-1,sizeof adj);
    n = _n;
    for(int i = 0 ; i < n ; ++i)a[0].lab[i] = -1;
}

int cycle = 0;
int CurState = 0;

int dfs(int u , int b){
    int res = 1;
    int par = b;
    while(u != b){
        res++;
        for(int i = 0 ; i < 2 ; ++i){
            if(adj[u][i] != -1 && adj[u][i] != par){
                par = u;u = adj[u][i];
                break;
            }
        }
    }
    return res;
}

int deg[maxn];

void Link(int a , int b){
    if(CurState == 4)return;
    if(deg[a] < deg[b])swap(a,b);
    deg[a]++;deg[b]++;
    if(CurState == 3){
        for(int i = 0 ; i < 4 ; ++i){
            ::a[i].Connect(a,b);
            if((a != ::a[i].banned && deg[a] - ::a[i].is_link[a] > 2) ||
               (b != ::a[i].banned && deg[b] - ::a[i].is_link[b] > 2))::a[i].ok = 0;
        }
        return;
    }
    if(deg[a] == 3){
        CurState = 3;
        for(int i = 0 ; i < 2 ; ++i){
            ::a[i].init(adj[a][i]);
        }
        ::a[2].init(b);
        ::a[3].init(a);
        for(int i = 0 ; i < 4 ; ++i){
            ::a[i].Connect(a,b);
            if((a != ::a[i].banned && deg[a] - ::a[i].is_link[a] > 2) ||
               (b != ::a[i].banned && deg[b] - ::a[i].is_link[b] > 2)){
                    ::a[i].ok = 0;
//                    cout << ::a[i].banned << endl;
               }
        }
        return;
    }
    for(int i = 0 ; i < 2 ; ++i){
        if(adj[a][i] == -1){
            adj[a][i] = b;
            break;
        }
    }
    for(int i = 0 ; i < 2 ; ++i){
        if(adj[b][i] == -1){
            adj[b][i] = a;
            break;
        }
    }
    if(::a[0].FindLab(a) == ::a[0].FindLab(b)){
        if(CurState == 2)CurState = 4;
        else cycle = dfs(a , b) , CurState = 2;
    }else{
         ::a[0].Connect(a , b);
    }

}

int CountCritical(){
    if(CurState == 4)return 0;
    if(CurState == 3){
        int res = 0;
        for(int i = 0 ; i < 4 ; ++i){
            res += a[i].ok;
        }
        if(res == 0)CurState = 4;
        return res;
    }
//    cout << CurState << endl;
    if(CurState == 2)return cycle;
    return n;
}

#ifdef LOCAL

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>

#define inbuf_len 1 << 16
#define outbuf_len 1 << 16

void Init(int N);
int CountCritical();
void Link(int a, int b);

int main() {
    freopen("A.INP","r",stdin);
  int tmp;
  /* Set input and output buffering */
  char *inbuf, *outbuf;
  inbuf = (char*) malloc(inbuf_len * sizeof(char));
  outbuf = (char*) malloc(outbuf_len * sizeof(char));
  tmp = setvbuf(stdin, inbuf, _IOFBF, inbuf_len);
  tmp = setvbuf(stdout, outbuf, _IOFBF, outbuf_len);

  int N, L;
  tmp = scanf("%d %d", &N, &L);
  assert(tmp == 2);
  Init(N);

  int i;
  for (i = 0; i < L; i++) {
    int A, B;
    tmp = scanf("%d", &A);
    if (A == -1) {
      int critical;
      critical = CountCritical();
      printf("%d\n", critical);
    } else {
      tmp = scanf("%d", &B);
      assert(tmp == 1);
      Link(A, B);
    }
  }

  return 0;

}
#endif // LOCAL
# Verdict Execution time Memory Grader output
1 Correct 9 ms 8704 KB Output is correct
2 Correct 10 ms 8832 KB Output is correct
3 Correct 10 ms 8832 KB Output is correct
4 Correct 9 ms 8704 KB Output is correct
5 Correct 10 ms 8832 KB Output is correct
6 Correct 10 ms 8832 KB Output is correct
7 Correct 9 ms 8832 KB Output is correct
8 Correct 10 ms 8832 KB Output is correct
9 Correct 11 ms 8832 KB Output is correct
10 Correct 12 ms 8832 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 241 ms 19704 KB Output is correct
2 Correct 490 ms 35192 KB Output is correct
3 Correct 475 ms 41056 KB Output is correct
4 Correct 576 ms 29944 KB Output is correct
5 Correct 588 ms 29908 KB Output is correct
6 Correct 583 ms 29476 KB Output is correct
7 Correct 450 ms 40344 KB Output is correct
8 Correct 704 ms 39544 KB Output is correct
9 Correct 710 ms 41592 KB Output is correct
10 Correct 461 ms 29176 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 8704 KB Output is correct
2 Correct 10 ms 8832 KB Output is correct
3 Correct 10 ms 8832 KB Output is correct
4 Correct 9 ms 8704 KB Output is correct
5 Correct 10 ms 8832 KB Output is correct
6 Correct 10 ms 8832 KB Output is correct
7 Correct 9 ms 8832 KB Output is correct
8 Correct 10 ms 8832 KB Output is correct
9 Correct 11 ms 8832 KB Output is correct
10 Correct 12 ms 8832 KB Output is correct
11 Correct 11 ms 8832 KB Output is correct
12 Correct 12 ms 9088 KB Output is correct
13 Correct 13 ms 9088 KB Output is correct
14 Correct 11 ms 8960 KB Output is correct
15 Correct 12 ms 9216 KB Output is correct
16 Correct 12 ms 8960 KB Output is correct
17 Correct 12 ms 8960 KB Output is correct
18 Correct 13 ms 9344 KB Output is correct
19 Correct 12 ms 8960 KB Output is correct
20 Correct 13 ms 9088 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 8704 KB Output is correct
2 Correct 10 ms 8832 KB Output is correct
3 Correct 10 ms 8832 KB Output is correct
4 Correct 9 ms 8704 KB Output is correct
5 Correct 10 ms 8832 KB Output is correct
6 Correct 10 ms 8832 KB Output is correct
7 Correct 9 ms 8832 KB Output is correct
8 Correct 10 ms 8832 KB Output is correct
9 Correct 11 ms 8832 KB Output is correct
10 Correct 12 ms 8832 KB Output is correct
11 Correct 11 ms 8832 KB Output is correct
12 Correct 12 ms 9088 KB Output is correct
13 Correct 13 ms 9088 KB Output is correct
14 Correct 11 ms 8960 KB Output is correct
15 Correct 12 ms 9216 KB Output is correct
16 Correct 12 ms 8960 KB Output is correct
17 Correct 12 ms 8960 KB Output is correct
18 Correct 13 ms 9344 KB Output is correct
19 Correct 12 ms 8960 KB Output is correct
20 Correct 13 ms 9088 KB Output is correct
21 Correct 23 ms 9728 KB Output is correct
22 Correct 35 ms 10240 KB Output is correct
23 Correct 42 ms 10744 KB Output is correct
24 Correct 43 ms 11512 KB Output is correct
25 Correct 22 ms 11008 KB Output is correct
26 Correct 43 ms 11640 KB Output is correct
27 Correct 43 ms 10620 KB Output is correct
28 Correct 39 ms 11520 KB Output is correct
29 Correct 36 ms 11512 KB Output is correct
30 Correct 45 ms 10744 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 8704 KB Output is correct
2 Correct 10 ms 8832 KB Output is correct
3 Correct 10 ms 8832 KB Output is correct
4 Correct 9 ms 8704 KB Output is correct
5 Correct 10 ms 8832 KB Output is correct
6 Correct 10 ms 8832 KB Output is correct
7 Correct 9 ms 8832 KB Output is correct
8 Correct 10 ms 8832 KB Output is correct
9 Correct 11 ms 8832 KB Output is correct
10 Correct 12 ms 8832 KB Output is correct
11 Correct 241 ms 19704 KB Output is correct
12 Correct 490 ms 35192 KB Output is correct
13 Correct 475 ms 41056 KB Output is correct
14 Correct 576 ms 29944 KB Output is correct
15 Correct 588 ms 29908 KB Output is correct
16 Correct 583 ms 29476 KB Output is correct
17 Correct 450 ms 40344 KB Output is correct
18 Correct 704 ms 39544 KB Output is correct
19 Correct 710 ms 41592 KB Output is correct
20 Correct 461 ms 29176 KB Output is correct
21 Correct 11 ms 8832 KB Output is correct
22 Correct 12 ms 9088 KB Output is correct
23 Correct 13 ms 9088 KB Output is correct
24 Correct 11 ms 8960 KB Output is correct
25 Correct 12 ms 9216 KB Output is correct
26 Correct 12 ms 8960 KB Output is correct
27 Correct 12 ms 8960 KB Output is correct
28 Correct 13 ms 9344 KB Output is correct
29 Correct 12 ms 8960 KB Output is correct
30 Correct 13 ms 9088 KB Output is correct
31 Correct 23 ms 9728 KB Output is correct
32 Correct 35 ms 10240 KB Output is correct
33 Correct 42 ms 10744 KB Output is correct
34 Correct 43 ms 11512 KB Output is correct
35 Correct 22 ms 11008 KB Output is correct
36 Correct 43 ms 11640 KB Output is correct
37 Correct 43 ms 10620 KB Output is correct
38 Correct 39 ms 11520 KB Output is correct
39 Correct 36 ms 11512 KB Output is correct
40 Correct 45 ms 10744 KB Output is correct
41 Correct 166 ms 18300 KB Output is correct
42 Correct 402 ms 34296 KB Output is correct
43 Correct 249 ms 31992 KB Output is correct
44 Correct 330 ms 39032 KB Output is correct
45 Correct 358 ms 37776 KB Output is correct
46 Correct 428 ms 27896 KB Output is correct
47 Correct 510 ms 28128 KB Output is correct
48 Correct 288 ms 36216 KB Output is correct
49 Correct 431 ms 29304 KB Output is correct
50 Correct 429 ms 29012 KB Output is correct
51 Correct 241 ms 29304 KB Output is correct
52 Correct 314 ms 33912 KB Output is correct
53 Correct 263 ms 35604 KB Output is correct
54 Correct 562 ms 36728 KB Output is correct
55 Correct 501 ms 38392 KB Output is correct