Submission #598034

# Submission time Handle Problem Language Result Execution time Memory
598034 2022-07-17T12:09:57 Z boris_mihov Stranded Far From Home (BOI22_island) C++14
10 / 100
1000 ms 35532 KB
#include <algorithm>
#include <iostream>
#include <numeric>
#include <cassert>
#include <vector>

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

int n, m;
llong a[MAXN], s[MAXN], inS[MAXN];
std::vector < int > g[MAXN];

struct DSUelement
{
    int par;
    llong sum;
    std::vector < int > alive;
} 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 (c[x].alive.size() > c[y].alive.size()) std::swap(x, y);
    c[x].par = y;
    c[y].sum += c[x].sum;
    c[y].alive.reserve(c[x].alive.size() + c[y].alive.size());
    for (int i : c[x].alive)
    {
        c[y].alive.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];
        c[i].alive.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])
            {
                c[par].alive.clear();
            }

            connect(x, j);
        }
    }

    int findPar = find(1);
    std::fill(ans, ans+n, '0');
    for (int i : c[findPar].alive)
    {
        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;
}
# Verdict Execution time Memory Grader output
1 Correct 6 ms 12756 KB Output is correct
2 Correct 8 ms 12868 KB Output is correct
3 Correct 6 ms 12756 KB Output is correct
4 Correct 9 ms 13012 KB Output is correct
5 Correct 7 ms 13012 KB Output is correct
6 Correct 8 ms 13012 KB Output is correct
7 Correct 9 ms 13008 KB Output is correct
8 Correct 8 ms 13012 KB Output is correct
9 Correct 7 ms 13140 KB Output is correct
10 Correct 8 ms 13012 KB Output is correct
11 Correct 9 ms 12996 KB Output is correct
12 Correct 7 ms 13092 KB Output is correct
13 Correct 9 ms 13004 KB Output is correct
14 Correct 8 ms 13076 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 12756 KB Output is correct
2 Correct 7 ms 12756 KB Output is correct
3 Execution timed out 1088 ms 33172 KB Time limit exceeded
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 6 ms 12756 KB Output is correct
2 Correct 188 ms 33504 KB Output is correct
3 Correct 202 ms 33860 KB Output is correct
4 Correct 176 ms 35104 KB Output is correct
5 Correct 166 ms 35232 KB Output is correct
6 Correct 214 ms 33876 KB Output is correct
7 Execution timed out 1058 ms 31476 KB Time limit exceeded
8 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 6 ms 12756 KB Output is correct
2 Correct 948 ms 32188 KB Output is correct
3 Correct 487 ms 33312 KB Output is correct
4 Correct 261 ms 34164 KB Output is correct
5 Correct 590 ms 35532 KB Output is correct
6 Execution timed out 1070 ms 32120 KB Time limit exceeded
7 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 6 ms 12756 KB Output is correct
2 Correct 8 ms 12868 KB Output is correct
3 Correct 6 ms 12756 KB Output is correct
4 Correct 9 ms 13012 KB Output is correct
5 Correct 7 ms 13012 KB Output is correct
6 Correct 8 ms 13012 KB Output is correct
7 Correct 9 ms 13008 KB Output is correct
8 Correct 8 ms 13012 KB Output is correct
9 Correct 7 ms 13140 KB Output is correct
10 Correct 8 ms 13012 KB Output is correct
11 Correct 9 ms 12996 KB Output is correct
12 Correct 7 ms 13092 KB Output is correct
13 Correct 9 ms 13004 KB Output is correct
14 Correct 8 ms 13076 KB Output is correct
15 Correct 8 ms 12756 KB Output is correct
16 Correct 7 ms 12756 KB Output is correct
17 Execution timed out 1088 ms 33172 KB Time limit exceeded
18 Halted 0 ms 0 KB -