답안 #598032

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

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

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

struct DSUelement
{
    int par;
    int 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 (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;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 11220 KB Output is correct
2 Correct 6 ms 11220 KB Output is correct
3 Correct 6 ms 11296 KB Output is correct
4 Incorrect 7 ms 11348 KB Output isn't correct
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 11220 KB Output is correct
2 Correct 5 ms 11252 KB Output is correct
3 Incorrect 129 ms 27156 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 11220 KB Output is correct
2 Incorrect 177 ms 26848 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 11292 KB Output is correct
2 Incorrect 181 ms 26776 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 11220 KB Output is correct
2 Correct 6 ms 11220 KB Output is correct
3 Correct 6 ms 11296 KB Output is correct
4 Incorrect 7 ms 11348 KB Output isn't correct
5 Halted 0 ms 0 KB -