답안 #598041

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
598041 2022-07-17T12:23:46 Z boris_mihov Stranded Far From Home (BOI22_island) C++14
100 / 100
215 ms 31068 KB
#include <algorithm>
#include <iostream>
#include <numeric>
#include <cassert>
#include <vector>

typedef long long llong;
const int MAXN = 200000 + 10;
const int INF = 1e9 + 10;

int n, m;
int a[MAXN], s[MAXN], inS[MAXN];
std::vector < int > g[MAXN];
std::vector < int > alive[MAXN];
struct DSUelement
{
    int par;
    int sum;
} c[MAXN];

int find(int x)
{
    if (c[x].par == x) return x;
    return c[x].par = find(c[x].par);
}

void connect(int x, int y)
{
    x = find(x);
    y = find(y);
    if (alive[x].size() > alive[y].size()) std::swap(x, y);
    c[x].par = y;
    c[y].sum += c[x].sum;
    c[y].sum = std::min(c[y].sum, INF);
    alive[y].reserve(alive[x].size());
    for (int i : alive[x])
    {
        alive[y].push_back(i);
    }
}

bool areConnected(int x, int y)
{
    return find(x) == find(y);
}

char ans[MAXN];
void solve()
{
    std::iota(s+1, s+n+1, 1);
    std::sort(s+1, s+n+1, [&](int x, int y)
    {
        return a[x] < a[y];
    });

    for (int i = 1 ; i <= n ; ++i)
    {
        inS[s[i]] = i;
        c[i].par = i;
        c[i].sum = a[i];
        alive[i].push_back(i);
    }

    for (int i = 1 ; i <= n ; ++i)
    {
        int x = s[i];
        for (const int &j : g[x])
        {
            if (inS[j] > i || areConnected(x, j)) continue;
            int par = find(j);
            if (c[par].sum < a[x])
            {
                alive[par].clear();
            }

            connect(x, j);
        }
    }

    int findPar = find(1);
    std::fill(ans, ans+n, '0');
    for (int i : alive[findPar])
    {
        ans[i-1] = '1';
    }

    ans[n] = '\0';
    std::cout << ans << '\n';
}

void read()
{
    std::cin >> n >> m;
    for (int i = 1 ; i <= n ; ++i)
    {
        std::cin >> a[i];
    }

    for (int i = 1 ; i <= m ; ++i)
    {
        int x, y;
        std::cin >> x >> y;
        g[x].push_back(y);
        g[y].push_back(x);
    }
}

void fastIO()
{
    std::ios_base :: sync_with_stdio(0);
    std::cout.tie(nullptr);
    std::cin.tie(nullptr);
}

int main()
{
    fastIO();
    read();
    solve();

    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 9684 KB Output is correct
2 Correct 5 ms 9684 KB Output is correct
3 Correct 5 ms 9684 KB Output is correct
4 Correct 5 ms 9812 KB Output is correct
5 Correct 6 ms 9940 KB Output is correct
6 Correct 5 ms 9812 KB Output is correct
7 Correct 5 ms 9812 KB Output is correct
8 Correct 5 ms 9752 KB Output is correct
9 Correct 5 ms 9812 KB Output is correct
10 Correct 6 ms 9940 KB Output is correct
11 Correct 6 ms 9940 KB Output is correct
12 Correct 5 ms 9940 KB Output is correct
13 Correct 5 ms 9940 KB Output is correct
14 Correct 6 ms 9908 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 9684 KB Output is correct
2 Correct 4 ms 9684 KB Output is correct
3 Correct 131 ms 27316 KB Output is correct
4 Correct 137 ms 30496 KB Output is correct
5 Correct 166 ms 28864 KB Output is correct
6 Correct 151 ms 29488 KB Output is correct
7 Correct 170 ms 29412 KB Output is correct
8 Correct 173 ms 28568 KB Output is correct
9 Correct 142 ms 30664 KB Output is correct
10 Correct 95 ms 27320 KB Output is correct
11 Correct 111 ms 28472 KB Output is correct
12 Correct 175 ms 28216 KB Output is correct
13 Correct 134 ms 30888 KB Output is correct
14 Correct 136 ms 30484 KB Output is correct
15 Correct 136 ms 27432 KB Output is correct
16 Correct 79 ms 27200 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 9684 KB Output is correct
2 Correct 185 ms 30224 KB Output is correct
3 Correct 215 ms 30120 KB Output is correct
4 Correct 141 ms 31068 KB Output is correct
5 Correct 134 ms 30932 KB Output is correct
6 Correct 182 ms 30320 KB Output is correct
7 Correct 160 ms 27432 KB Output is correct
8 Correct 158 ms 27416 KB Output is correct
9 Correct 79 ms 27188 KB Output is correct
10 Correct 134 ms 28448 KB Output is correct
11 Correct 144 ms 29960 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 9684 KB Output is correct
2 Correct 179 ms 27996 KB Output is correct
3 Correct 198 ms 28488 KB Output is correct
4 Correct 182 ms 29388 KB Output is correct
5 Correct 172 ms 30260 KB Output is correct
6 Correct 154 ms 27896 KB Output is correct
7 Correct 106 ms 28424 KB Output is correct
8 Correct 92 ms 29176 KB Output is correct
9 Correct 93 ms 19616 KB Output is correct
10 Correct 154 ms 31064 KB Output is correct
11 Correct 140 ms 29952 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 9684 KB Output is correct
2 Correct 5 ms 9684 KB Output is correct
3 Correct 5 ms 9684 KB Output is correct
4 Correct 5 ms 9812 KB Output is correct
5 Correct 6 ms 9940 KB Output is correct
6 Correct 5 ms 9812 KB Output is correct
7 Correct 5 ms 9812 KB Output is correct
8 Correct 5 ms 9752 KB Output is correct
9 Correct 5 ms 9812 KB Output is correct
10 Correct 6 ms 9940 KB Output is correct
11 Correct 6 ms 9940 KB Output is correct
12 Correct 5 ms 9940 KB Output is correct
13 Correct 5 ms 9940 KB Output is correct
14 Correct 6 ms 9908 KB Output is correct
15 Correct 5 ms 9684 KB Output is correct
16 Correct 4 ms 9684 KB Output is correct
17 Correct 131 ms 27316 KB Output is correct
18 Correct 137 ms 30496 KB Output is correct
19 Correct 166 ms 28864 KB Output is correct
20 Correct 151 ms 29488 KB Output is correct
21 Correct 170 ms 29412 KB Output is correct
22 Correct 173 ms 28568 KB Output is correct
23 Correct 142 ms 30664 KB Output is correct
24 Correct 95 ms 27320 KB Output is correct
25 Correct 111 ms 28472 KB Output is correct
26 Correct 175 ms 28216 KB Output is correct
27 Correct 134 ms 30888 KB Output is correct
28 Correct 136 ms 30484 KB Output is correct
29 Correct 136 ms 27432 KB Output is correct
30 Correct 79 ms 27200 KB Output is correct
31 Correct 5 ms 9684 KB Output is correct
32 Correct 185 ms 30224 KB Output is correct
33 Correct 215 ms 30120 KB Output is correct
34 Correct 141 ms 31068 KB Output is correct
35 Correct 134 ms 30932 KB Output is correct
36 Correct 182 ms 30320 KB Output is correct
37 Correct 160 ms 27432 KB Output is correct
38 Correct 158 ms 27416 KB Output is correct
39 Correct 79 ms 27188 KB Output is correct
40 Correct 134 ms 28448 KB Output is correct
41 Correct 144 ms 29960 KB Output is correct
42 Correct 6 ms 9684 KB Output is correct
43 Correct 179 ms 27996 KB Output is correct
44 Correct 198 ms 28488 KB Output is correct
45 Correct 182 ms 29388 KB Output is correct
46 Correct 172 ms 30260 KB Output is correct
47 Correct 154 ms 27896 KB Output is correct
48 Correct 106 ms 28424 KB Output is correct
49 Correct 92 ms 29176 KB Output is correct
50 Correct 93 ms 19616 KB Output is correct
51 Correct 154 ms 31064 KB Output is correct
52 Correct 140 ms 29952 KB Output is correct
53 Correct 5 ms 9684 KB Output is correct
54 Correct 5 ms 9684 KB Output is correct
55 Correct 5 ms 9684 KB Output is correct
56 Correct 6 ms 9812 KB Output is correct
57 Correct 7 ms 9812 KB Output is correct
58 Correct 6 ms 9812 KB Output is correct
59 Correct 6 ms 9812 KB Output is correct
60 Correct 5 ms 9812 KB Output is correct
61 Correct 6 ms 9812 KB Output is correct
62 Correct 6 ms 9940 KB Output is correct
63 Correct 6 ms 9940 KB Output is correct
64 Correct 6 ms 9940 KB Output is correct
65 Correct 6 ms 9940 KB Output is correct
66 Correct 6 ms 9812 KB Output is correct
67 Correct 124 ms 27380 KB Output is correct
68 Correct 126 ms 30400 KB Output is correct
69 Correct 142 ms 28820 KB Output is correct
70 Correct 155 ms 29480 KB Output is correct
71 Correct 162 ms 29344 KB Output is correct
72 Correct 159 ms 28596 KB Output is correct
73 Correct 127 ms 30580 KB Output is correct
74 Correct 97 ms 27264 KB Output is correct
75 Correct 102 ms 28412 KB Output is correct
76 Correct 151 ms 28236 KB Output is correct
77 Correct 124 ms 30972 KB Output is correct
78 Correct 135 ms 30688 KB Output is correct
79 Correct 133 ms 27388 KB Output is correct
80 Correct 81 ms 27208 KB Output is correct
81 Correct 182 ms 30144 KB Output is correct
82 Correct 187 ms 30120 KB Output is correct
83 Correct 132 ms 31036 KB Output is correct
84 Correct 125 ms 30812 KB Output is correct
85 Correct 183 ms 30280 KB Output is correct
86 Correct 146 ms 27456 KB Output is correct
87 Correct 138 ms 27444 KB Output is correct
88 Correct 137 ms 28456 KB Output is correct
89 Correct 146 ms 29848 KB Output is correct
90 Correct 176 ms 27996 KB Output is correct
91 Correct 172 ms 28488 KB Output is correct
92 Correct 178 ms 29308 KB Output is correct
93 Correct 166 ms 30264 KB Output is correct
94 Correct 159 ms 27984 KB Output is correct
95 Correct 118 ms 28420 KB Output is correct
96 Correct 93 ms 29184 KB Output is correct
97 Correct 89 ms 19596 KB Output is correct
98 Correct 174 ms 31044 KB Output is correct
99 Correct 151 ms 30000 KB Output is correct
100 Correct 39 ms 12108 KB Output is correct
101 Correct 189 ms 28752 KB Output is correct
102 Correct 134 ms 24260 KB Output is correct
103 Correct 158 ms 24532 KB Output is correct
104 Correct 194 ms 27380 KB Output is correct