답안 #880184

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
880184 2023-11-29T02:12:39 Z abcvuitunggio 낙하산 고리들 (IOI12_rings) C++17
100 / 100
1397 ms 110980 KB
#include <iostream>
#include <vector>
using namespace std;
int n,focused=-1,ch[5],cnt,mx,deg[1000001],deg2[1000001],p[1000001][5],cycle,cyclesize,sz[1000001][5];
vector <int> candidates,ke[1000001];
vector <pair <int, int>> edge;
void reset(int j){
    for (int i=0;i<n;i++){
        p[i][j]=i;
        sz[i][j]=1;
    }
    ch[j]=1;
}
int f(int i, int j){
    return (p[i][j]==i?i:p[i][j]=f(p[i][j],j));
}
bool unite(int u, int v, int idx){
    u=f(u,idx);
    v=f(v,idx);
    if (u==v)
        return 0;
    if (sz[u][idx]<sz[v][idx])
        swap(u,v);
    sz[u][idx]+=sz[v][idx];
    p[v][idx]=u;
    return 1;
}
void focus(int node, int j=0){
    reset(j);
    if (focused!=-1)
        for (int i=0;i<n;i++)
            deg[i]=0;
    for (auto [u,v]:edge)
        if (u!=node&&v!=node){
            ch[j]&=unite(u,v,j);
            if (focused!=-1){
                deg[u]++;
                deg[v]++;
                if (max(deg[u],deg[v])>2)
                    ch[j]=0;
            }
        }
}
void Init(int N){
    n=N;
    for (int i=0;i<5;i++)
        reset(i);
}
void Link(int a, int b){
    edge.push_back({a,b});
    if (focused!=-1){
        if (a!=focused&&b!=focused){
            ch[0]&=unite(a,b,0);
            deg[a]++;
            deg[b]++;
            if (max(deg[a],deg[b])>2)
                ch[0]=0;
        }
        return;
    }
    deg[a]++;
    deg[b]++;
    ke[a].push_back(b);
    ke[b].push_back(a);
    if (deg[a]==4){
        focused=a;
        focus(a);
        return;
    }
    if (deg[b]==4){
        focused=b;
        focus(b);
        return;
    }
    int cnt2=cnt;
    if (deg[a]==3){
        cnt2++;
        for (int v:ke[a])
            if (deg[v]>2&&v!=b){
                deg2[a]++;
                deg2[v]++;
            }
    }
    if (deg[b]==3){
        cnt2++;
        for (int v:ke[b])
            if (deg[v]>2){
                deg2[b]++;
                deg2[v]++;
            }
    }
    if (cnt2>4){
        cnt=cnt2;
        return;
    }
    if (!cnt2){
        if (deg[a]==2&&deg[b]==2&&f(a,0)==f(b,0)){
            cycle++;
            cyclesize=sz[f(a,0)][0];
        }
        unite(a,b,0);
    }
    else if (cnt2==1){
        if (!cnt){
            if (deg[a]<deg[b])
                swap(a,b);
            candidates.push_back(a);
            for (int v:ke[a])
                candidates.push_back(v);
            for (int i=0;i<candidates.size();i++)
                focus(candidates[i],i);
        }
        else{
            for (int i=0;i<candidates.size();i++)
                if (a!=candidates[i]&&b!=candidates[i])
                    ch[i]&=unite(a,b,i);
        }
    }
    else if (cnt2==2){
        if (cnt<2){
            candidates.clear();
            int u=-1,v=-1;
            for (int i=0;i<n;i++)
                if (deg[i]>2)
                    (u==-1?u=i:v=i);
            for (int i=0;i<n;i++){
                int cnt3=(i==u||i==v);
                for (int j:ke[i])
                    cnt3+=(j==u||j==v);
                if (cnt3==2)
                    candidates.push_back(i);
            }
            for (int i=0;i<candidates.size();i++)
                focus(candidates[i],i);
        }
        else{
            for (int i=0;i<candidates.size();i++)
                if (a!=candidates[i]&&b!=candidates[i])
                    ch[i]&=unite(a,b,i);
        }
    }
    else if (cnt<cnt2){
        candidates.clear();
        for (int i=0;i<n;i++)
            if (deg[i]>2&&deg2[i]==cnt2-1)
                candidates.push_back(i);
        for (int i=0;i<candidates.size();i++)
                focus(candidates[i],i);
    }
    else{
        for (int i=0;i<candidates.size();i++)
            if (a!=candidates[i]&&b!=candidates[i])
                ch[i]&=unite(a,b,i);
    }
    cnt=cnt2;
}
int CountCritical(){
    if (focused!=-1)
        return ch[0];
    if (cnt>4)
        return 0;
    if (cnt){
        int res=0;
        for (int i=0;i<candidates.size();i++)
            res+=ch[i];
        return res;
    }
    return (cycle>1?0:(cycle==1?cyclesize:n));
}

Compilation message

rings.cpp: In function 'void Link(int, int)':
rings.cpp:110:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  110 |             for (int i=0;i<candidates.size();i++)
      |                          ~^~~~~~~~~~~~~~~~~~
rings.cpp:114:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  114 |             for (int i=0;i<candidates.size();i++)
      |                          ~^~~~~~~~~~~~~~~~~~
rings.cpp:133:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  133 |             for (int i=0;i<candidates.size();i++)
      |                          ~^~~~~~~~~~~~~~~~~~
rings.cpp:137:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  137 |             for (int i=0;i<candidates.size();i++)
      |                          ~^~~~~~~~~~~~~~~~~~
rings.cpp:147:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  147 |         for (int i=0;i<candidates.size();i++)
      |                      ~^~~~~~~~~~~~~~~~~~
rings.cpp:151:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  151 |         for (int i=0;i<candidates.size();i++)
      |                      ~^~~~~~~~~~~~~~~~~~
rings.cpp: In function 'int CountCritical()':
rings.cpp:164:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  164 |         for (int i=0;i<candidates.size();i++)
      |                      ~^~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 29276 KB Output is correct
2 Correct 8 ms 29532 KB Output is correct
3 Correct 10 ms 29276 KB Output is correct
4 Correct 6 ms 29328 KB Output is correct
5 Correct 8 ms 29276 KB Output is correct
6 Correct 9 ms 29528 KB Output is correct
7 Correct 7 ms 29276 KB Output is correct
8 Correct 7 ms 29276 KB Output is correct
9 Correct 8 ms 31580 KB Output is correct
10 Correct 8 ms 29464 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 337 ms 72276 KB Output is correct
2 Correct 637 ms 85648 KB Output is correct
3 Correct 452 ms 78512 KB Output is correct
4 Correct 925 ms 108132 KB Output is correct
5 Correct 948 ms 107960 KB Output is correct
6 Correct 916 ms 106672 KB Output is correct
7 Correct 447 ms 79976 KB Output is correct
8 Correct 1221 ms 106096 KB Output is correct
9 Correct 1397 ms 108448 KB Output is correct
10 Correct 610 ms 105872 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 29276 KB Output is correct
2 Correct 8 ms 29532 KB Output is correct
3 Correct 10 ms 29276 KB Output is correct
4 Correct 6 ms 29328 KB Output is correct
5 Correct 8 ms 29276 KB Output is correct
6 Correct 9 ms 29528 KB Output is correct
7 Correct 7 ms 29276 KB Output is correct
8 Correct 7 ms 29276 KB Output is correct
9 Correct 8 ms 31580 KB Output is correct
10 Correct 8 ms 29464 KB Output is correct
11 Correct 8 ms 31576 KB Output is correct
12 Correct 10 ms 32088 KB Output is correct
13 Correct 11 ms 31856 KB Output is correct
14 Correct 7 ms 29276 KB Output is correct
15 Correct 8 ms 31408 KB Output is correct
16 Correct 9 ms 29788 KB Output is correct
17 Correct 9 ms 29788 KB Output is correct
18 Correct 9 ms 31580 KB Output is correct
19 Correct 9 ms 29788 KB Output is correct
20 Correct 10 ms 29788 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 29276 KB Output is correct
2 Correct 8 ms 29532 KB Output is correct
3 Correct 10 ms 29276 KB Output is correct
4 Correct 6 ms 29328 KB Output is correct
5 Correct 8 ms 29276 KB Output is correct
6 Correct 9 ms 29528 KB Output is correct
7 Correct 7 ms 29276 KB Output is correct
8 Correct 7 ms 29276 KB Output is correct
9 Correct 8 ms 31580 KB Output is correct
10 Correct 8 ms 29464 KB Output is correct
11 Correct 8 ms 31576 KB Output is correct
12 Correct 10 ms 32088 KB Output is correct
13 Correct 11 ms 31856 KB Output is correct
14 Correct 7 ms 29276 KB Output is correct
15 Correct 8 ms 31408 KB Output is correct
16 Correct 9 ms 29788 KB Output is correct
17 Correct 9 ms 29788 KB Output is correct
18 Correct 9 ms 31580 KB Output is correct
19 Correct 9 ms 29788 KB Output is correct
20 Correct 10 ms 29788 KB Output is correct
21 Correct 19 ms 32980 KB Output is correct
22 Correct 26 ms 35780 KB Output is correct
23 Correct 31 ms 36572 KB Output is correct
24 Correct 25 ms 34784 KB Output is correct
25 Correct 15 ms 33884 KB Output is correct
26 Correct 25 ms 35024 KB Output is correct
27 Correct 36 ms 37832 KB Output is correct
28 Correct 37 ms 38344 KB Output is correct
29 Correct 25 ms 34776 KB Output is correct
30 Correct 61 ms 38360 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 29276 KB Output is correct
2 Correct 8 ms 29532 KB Output is correct
3 Correct 10 ms 29276 KB Output is correct
4 Correct 6 ms 29328 KB Output is correct
5 Correct 8 ms 29276 KB Output is correct
6 Correct 9 ms 29528 KB Output is correct
7 Correct 7 ms 29276 KB Output is correct
8 Correct 7 ms 29276 KB Output is correct
9 Correct 8 ms 31580 KB Output is correct
10 Correct 8 ms 29464 KB Output is correct
11 Correct 337 ms 72276 KB Output is correct
12 Correct 637 ms 85648 KB Output is correct
13 Correct 452 ms 78512 KB Output is correct
14 Correct 925 ms 108132 KB Output is correct
15 Correct 948 ms 107960 KB Output is correct
16 Correct 916 ms 106672 KB Output is correct
17 Correct 447 ms 79976 KB Output is correct
18 Correct 1221 ms 106096 KB Output is correct
19 Correct 1397 ms 108448 KB Output is correct
20 Correct 610 ms 105872 KB Output is correct
21 Correct 8 ms 31576 KB Output is correct
22 Correct 10 ms 32088 KB Output is correct
23 Correct 11 ms 31856 KB Output is correct
24 Correct 7 ms 29276 KB Output is correct
25 Correct 8 ms 31408 KB Output is correct
26 Correct 9 ms 29788 KB Output is correct
27 Correct 9 ms 29788 KB Output is correct
28 Correct 9 ms 31580 KB Output is correct
29 Correct 9 ms 29788 KB Output is correct
30 Correct 10 ms 29788 KB Output is correct
31 Correct 19 ms 32980 KB Output is correct
32 Correct 26 ms 35780 KB Output is correct
33 Correct 31 ms 36572 KB Output is correct
34 Correct 25 ms 34784 KB Output is correct
35 Correct 15 ms 33884 KB Output is correct
36 Correct 25 ms 35024 KB Output is correct
37 Correct 36 ms 37832 KB Output is correct
38 Correct 37 ms 38344 KB Output is correct
39 Correct 25 ms 34776 KB Output is correct
40 Correct 61 ms 38360 KB Output is correct
41 Correct 172 ms 65416 KB Output is correct
42 Correct 532 ms 90476 KB Output is correct
43 Correct 178 ms 72136 KB Output is correct
44 Correct 390 ms 85932 KB Output is correct
45 Correct 473 ms 94336 KB Output is correct
46 Correct 570 ms 109488 KB Output is correct
47 Correct 804 ms 110424 KB Output is correct
48 Correct 294 ms 81232 KB Output is correct
49 Correct 579 ms 110980 KB Output is correct
50 Correct 603 ms 109996 KB Output is correct
51 Correct 176 ms 68500 KB Output is correct
52 Correct 371 ms 101808 KB Output is correct
53 Correct 283 ms 81796 KB Output is correct
54 Correct 989 ms 105900 KB Output is correct
55 Correct 498 ms 85944 KB Output is correct