답안 #557573

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
557573 2022-05-05T13:34:32 Z timreizin 낙하산 고리들 (IOI12_rings) C++17
100 / 100
2453 ms 121620 KB
#include <vector>
#include <numeric>
#include <array>
#include <algorithm>

using namespace std;

struct DSU
{
    int n, comps;
    vector<int> parent, size;
    vector<int> adj;
    array<int, 5> counter;
    int res, v;

    DSU(int n = 0, int v = -1) : n(n), v(v)
    {
        parent.resize(n);
        size.resize(n, 1);
        iota(parent.begin(), parent.end(), 0);
        comps = n;
        adj.resize(n);
        counter.fill(0);
        counter[0] = n;
        res = (v == -1 ? n : 1);
    }

    int getParent(int i)
    {
        if (parent[i] == i) return i;
        return parent[i] = getParent(parent[i]);
    }

    void unite(int a, int b)
    {
        if (res == 0) return;
        if (a == v || b == v) return;
        int wasCyc = comps - counter[0] - counter[1] / 2;
        --counter[min(adj[a], 4)];
        --counter[min(adj[b], 4)];
        ++adj[a];
        ++adj[b];
        ++counter[min(adj[a], 4)];
        ++counter[min(adj[b], 4)];
        if (counter[3] + counter[4] > 0)
        {
            res = 0;
            return;
        }
        a = getParent(a);
        b = getParent(b);
        if (a != b) --comps;
        int newCyc = comps - counter[0] - counter[1] / 2;
        if (v != -1 && newCyc > 0)
        {
            res = 0;
            return;
        }
        else if (v == -1)
        {
            if (newCyc >= 2)
            {
                res = 0;
                return;
            }
            if (newCyc == 1 && wasCyc == 0)
            {
                vector<int> cnt(n);
                for (int i = 0; i < n; ++i)
                {
                    if (adj[i] < 2) cnt[getParent(i)] = -1e9;
                    else ++cnt[getParent(i)];
                }
                res = 0;
                for (int i = 0; i < n; ++i) res = max(res, cnt[i]);
            }
        }
        if (a != b)
        {
            if (size[a] < size[b]) swap(a, b);
            size[a] += size[b];
            parent[b] = a;
        }
    }

};

int n;
vector<vector<int>> adj;
array<DSU, 8> dsu;
array<int, 5> counter;
vector<int> curs, dop;
vector<pair<int, int>> edges;

void Init(int N_)
{
    n = N_;
    adj.resize(n);
    dsu[0] = DSU(n);
    counter[0] = n;
    curs.push_back(-1);
}

void Link(int a, int b)
{
    int was3 = counter[3], was4 = counter[4];
    --counter[min((int)adj[a].size(), 4)];
    --counter[min((int)adj[b].size(), 4)];
    adj[a].push_back(b);
    adj[b].push_back(a);
    ++counter[min((int)adj[a].size(), 4)];
    ++counter[min((int)adj[b].size(), 4)];
    if (counter[4] > 1) return;
    if (counter[4] > was4)
    {
        curs.clear();
        dop.clear();
        for (int i = 0; i < 8; ++i) dsu[i].res = 0;
        if (adj[a].size() == 4) curs.push_back(a);
        if (adj[b].size() == 4) curs.push_back(b);
        dsu[0] = DSU(n, curs[0]);
        for (auto [a, b] : edges) dsu[0].unite(a, b);
    }
    else if (counter[3] > was3 && counter[4] == 0)
    {
        if (counter[3] > 4 && was3 <= 4)
        {
            curs.clear();
            dop.clear();
            for (int i = 0; i < 8; ++i) dsu[i].res = 0;
        }
        if (counter[3] <= 4)
        {
            if (was3 == 0) curs.clear();
            if (adj[a].size() == 3) curs.push_back(a);
            if (adj[b].size() == 3) curs.push_back(b);
            for (int i = 0; i < 8; ++i) dsu[i].res = 0;
            dop.clear();
            for (int i = 0; i < curs.size(); ++i)
            {
                dsu[i] = DSU(n, curs[i]);
                for (auto [a, b]: edges) dsu[i].unite(a, b);
            }
            if (curs.size() < 3)
            {
                for (int i = 0; i < n; ++i)
                {
                    if (adj[i].size() < 3)
                    {
                        bool was0 = false, was1 = curs.size() == 1;
                        for (int j : adj[i])
                        {
                            was0 = was0 || (j == curs[0]);
                            if (curs.size() == 2) was1 = was1 || (j == curs[1]);
                        }
                        if (was0 && was1) dop.push_back(i);
                    }
                }
            }
            for (int i = 0; i < dop.size(); ++i)
            {
                dsu[i + curs.size()] = DSU(n, dop[i]);
                for (auto [a, b]: edges) dsu[i + curs.size()].unite(a, b);
            }
        }
    }
    for (int i = 0; i < curs.size(); ++i) dsu[i].unite(a, b);
    for (int i = 0; i < dop.size(); ++i) dsu[i + curs.size()].unite(a, b);
    edges.emplace_back(a, b);
}

int CountCritical()
{
    if (counter[4] > 1) return 0;
    int res = 0;
    for (int i = 0; i < 8; ++i) res += dsu[i].res;
    return res;
}

Compilation message

rings.cpp: In function 'void Link(int, int)':
rings.cpp:139:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  139 |             for (int i = 0; i < curs.size(); ++i)
      |                             ~~^~~~~~~~~~~~~
rings.cpp:160:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  160 |             for (int i = 0; i < dop.size(); ++i)
      |                             ~~^~~~~~~~~~~~
rings.cpp:167:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  167 |     for (int i = 0; i < curs.size(); ++i) dsu[i].unite(a, b);
      |                     ~~^~~~~~~~~~~~~
rings.cpp:168:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  168 |     for (int i = 0; i < dop.size(); ++i) dsu[i + curs.size()].unite(a, b);
      |                     ~~^~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 2 ms 724 KB Output is correct
3 Correct 2 ms 888 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 468 KB Output is correct
6 Correct 3 ms 724 KB Output is correct
7 Correct 2 ms 724 KB Output is correct
8 Correct 2 ms 596 KB Output is correct
9 Correct 4 ms 852 KB Output is correct
10 Correct 4 ms 844 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 453 ms 39160 KB Output is correct
2 Correct 1169 ms 87872 KB Output is correct
3 Correct 675 ms 98356 KB Output is correct
4 Correct 1114 ms 78672 KB Output is correct
5 Correct 1099 ms 78632 KB Output is correct
6 Correct 1045 ms 77044 KB Output is correct
7 Correct 669 ms 98100 KB Output is correct
8 Correct 2035 ms 112184 KB Output is correct
9 Correct 2453 ms 121620 KB Output is correct
10 Correct 855 ms 76616 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 2 ms 724 KB Output is correct
3 Correct 2 ms 888 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 468 KB Output is correct
6 Correct 3 ms 724 KB Output is correct
7 Correct 2 ms 724 KB Output is correct
8 Correct 2 ms 596 KB Output is correct
9 Correct 4 ms 852 KB Output is correct
10 Correct 4 ms 844 KB Output is correct
11 Correct 4 ms 980 KB Output is correct
12 Correct 9 ms 1664 KB Output is correct
13 Correct 8 ms 1620 KB Output is correct
14 Correct 3 ms 1236 KB Output is correct
15 Correct 3 ms 2132 KB Output is correct
16 Correct 5 ms 1092 KB Output is correct
17 Correct 5 ms 1484 KB Output is correct
18 Correct 5 ms 2300 KB Output is correct
19 Correct 5 ms 1236 KB Output is correct
20 Correct 7 ms 1628 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 2 ms 724 KB Output is correct
3 Correct 2 ms 888 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 468 KB Output is correct
6 Correct 3 ms 724 KB Output is correct
7 Correct 2 ms 724 KB Output is correct
8 Correct 2 ms 596 KB Output is correct
9 Correct 4 ms 852 KB Output is correct
10 Correct 4 ms 844 KB Output is correct
11 Correct 4 ms 980 KB Output is correct
12 Correct 9 ms 1664 KB Output is correct
13 Correct 8 ms 1620 KB Output is correct
14 Correct 3 ms 1236 KB Output is correct
15 Correct 3 ms 2132 KB Output is correct
16 Correct 5 ms 1092 KB Output is correct
17 Correct 5 ms 1484 KB Output is correct
18 Correct 5 ms 2300 KB Output is correct
19 Correct 5 ms 1236 KB Output is correct
20 Correct 7 ms 1628 KB Output is correct
21 Correct 18 ms 3528 KB Output is correct
22 Correct 29 ms 5204 KB Output is correct
23 Correct 35 ms 6484 KB Output is correct
24 Correct 38 ms 9100 KB Output is correct
25 Correct 15 ms 8648 KB Output is correct
26 Correct 38 ms 9868 KB Output is correct
27 Correct 58 ms 6824 KB Output is correct
28 Correct 40 ms 10140 KB Output is correct
29 Correct 33 ms 9576 KB Output is correct
30 Correct 55 ms 8200 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 2 ms 724 KB Output is correct
3 Correct 2 ms 888 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 468 KB Output is correct
6 Correct 3 ms 724 KB Output is correct
7 Correct 2 ms 724 KB Output is correct
8 Correct 2 ms 596 KB Output is correct
9 Correct 4 ms 852 KB Output is correct
10 Correct 4 ms 844 KB Output is correct
11 Correct 453 ms 39160 KB Output is correct
12 Correct 1169 ms 87872 KB Output is correct
13 Correct 675 ms 98356 KB Output is correct
14 Correct 1114 ms 78672 KB Output is correct
15 Correct 1099 ms 78632 KB Output is correct
16 Correct 1045 ms 77044 KB Output is correct
17 Correct 669 ms 98100 KB Output is correct
18 Correct 2035 ms 112184 KB Output is correct
19 Correct 2453 ms 121620 KB Output is correct
20 Correct 855 ms 76616 KB Output is correct
21 Correct 4 ms 980 KB Output is correct
22 Correct 9 ms 1664 KB Output is correct
23 Correct 8 ms 1620 KB Output is correct
24 Correct 3 ms 1236 KB Output is correct
25 Correct 3 ms 2132 KB Output is correct
26 Correct 5 ms 1092 KB Output is correct
27 Correct 5 ms 1484 KB Output is correct
28 Correct 5 ms 2300 KB Output is correct
29 Correct 5 ms 1236 KB Output is correct
30 Correct 7 ms 1628 KB Output is correct
31 Correct 18 ms 3528 KB Output is correct
32 Correct 29 ms 5204 KB Output is correct
33 Correct 35 ms 6484 KB Output is correct
34 Correct 38 ms 9100 KB Output is correct
35 Correct 15 ms 8648 KB Output is correct
36 Correct 38 ms 9868 KB Output is correct
37 Correct 58 ms 6824 KB Output is correct
38 Correct 40 ms 10140 KB Output is correct
39 Correct 33 ms 9576 KB Output is correct
40 Correct 55 ms 8200 KB Output is correct
41 Correct 230 ms 28272 KB Output is correct
42 Correct 809 ms 78760 KB Output is correct
43 Correct 244 ms 76380 KB Output is correct
44 Correct 554 ms 93624 KB Output is correct
45 Correct 866 ms 92788 KB Output is correct
46 Correct 753 ms 64412 KB Output is correct
47 Correct 929 ms 66688 KB Output is correct
48 Correct 423 ms 88884 KB Output is correct
49 Correct 720 ms 66924 KB Output is correct
50 Correct 760 ms 66088 KB Output is correct
51 Correct 281 ms 65212 KB Output is correct
52 Correct 462 ms 80860 KB Output is correct
53 Correct 402 ms 88224 KB Output is correct
54 Correct 1638 ms 96120 KB Output is correct
55 Correct 934 ms 96908 KB Output is correct