Submission #877039

# Submission time Handle Problem Language Result Execution time Memory
877039 2023-11-22T18:42:50 Z andrei_iorgulescu Stranded Far From Home (BOI22_island) C++14
10 / 100
757 ms 154312 KB
#include <bits/stdc++.h>

using namespace std;

#define int long long

struct grup
{
    vector<int>noduri_alive;
    int nr_oameni;
    multiset<pair<int,int>> edges;
};

int n,m,s[200005];
vector<pair<int,int>>G[200005];
int t[200005],sz[200005];
grup g[200005];
bool sol_finala[200005];

bool cmp(int x,int y)
{
    if (g[x].nr_oameni != g[y].nr_oameni)
        return g[x].nr_oameni < g[y].nr_oameni;
    return x < y;
}

multiset<int,decltype(cmp)*>st(cmp);

int ancestor(int nod)
{
    while (nod != t[nod])
        nod = t[nod];
    return nod;
}

void join(int x,int y)
{
    x = ancestor(x);
    y = ancestor(y);
    if (x == y)
        return;
    if (sz[x] < sz[y])
        swap(x,y);
    sz[x] += sz[y];
    t[y] = x;
    g[x].nr_oameni += g[y].nr_oameni;
    for (auto it : g[y].noduri_alive)
        g[x].noduri_alive.push_back(it);
    for (auto it : g[y].edges)
        g[x].edges.insert(it);
    g[y].nr_oameni = 0;
    g[y].noduri_alive.clear();
    g[y].edges.clear();
    st.insert(x);
}

signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    cin >> n >> m;
    for (int i = 1; i <= n; i++)
        cin >> s[i];
    for (int i = 1; i <= m; i++)
    {
        int x,y;
        cin >> x >> y;
        G[x].push_back({s[y],y});
        G[y].push_back({s[x],x});
    }
    for (int i = 1; i <= n; i++)
    {
        t[i] = i;
        sz[i] = 1;
        g[i].noduri_alive = {i};
        g[i].nr_oameni = s[i];
        for (auto it : G[i])
            g[i].edges.insert(it);
        st.insert(i);
    }
    while (st.size() >= 2)
    {
        /*for (auto it : st)
        {
            cout << it << ' ' << g[it].nr_oameni << endl;
            for (auto itt : g[it].noduri_alive)
                cout << itt << ' ';
            cout << endl;
            for (auto itt : g[it].edges)
                cout << itt.first << ' ' << itt.second << endl;
            cout << endl;
        }
        cout << endl;*/
        int nod = *st.begin();
        st.erase(st.begin());
        //cout << nod << endl;
        while (true)
        {
            pair<int,int>aux = *(g[nod].edges.begin());
            if (ancestor(aux.second) == nod)
                g[nod].edges.erase(g[nod].edges.begin());
            else
                break;
        }
        pair<int,int>aux = *(g[nod].edges.begin());
        //cout << aux.first << ' ' << aux.second << endl;
        g[nod].edges.erase(g[nod].edges.begin());
        if (aux.first > g[nod].nr_oameni)
        {
            //cout << "caz1 " << nod << endl;
            g[nod].noduri_alive.clear();
        }
        else
        {
            //cout << "caz2 " << nod << ' ';
            int x = aux.second;
            x = ancestor(x);
            //cout << x << endl;
            //cout << st.size() << endl;
            if (st.find(x) != st.end())
                st.erase(st.find(x));
            join(nod,x);
        }
    }
    for (auto it : st)
    {
        for (auto x : g[it].noduri_alive)
            sol_finala[x] = true;
    }
    for (int i = 1; i <= n; i++)
    {
        if (sol_finala[i] == false)
            cout << 0;
        else
            cout << 1;
    }
    return 0;
}

/**
fiecare muchie devine defapt doua muchii, una x -> y cu cost s[y] si una y -> x cu cost s[x]

iau grupul cu macar un nod alive cu cei mai putini oameni si muchia lui spre un nod cu s cat mai mic
daca nu le pot conecta, marchez ca not alive toate nodurile alive din grup
daca le pot conecta, fac un small to large pe noduri (nu conteaza daca alive sau nu) pentru a le uni

ok, idee penala, acum ramane cum implementez

tin un dsu pentru grupuri, iar in tatal grupului tin:
-nodurile alive din grup
-cate alive am
-cati oameni am
-muchiile, sortate dupa cost

imi pot tine un set cu tatii grupurilor cu macar un nod alive, pentru fiecare retinand:
-ofc nodul tata
-numarul de oameni

o sa am ceva gen:

while (setul mai are >= 2 grupuri alive)
{
    iau si eu grupul din set cu numar minim de oameni
    lui ii iau muchia minima pentru care nu sunt ambele noduri conectate de muchie in grupul lui nod
    daca pe muchia asta nu pot conecta, atunci scot din set nodul nod (si atat)
    daca pe muchia asta pot conecta, am doua cazuri:
    1. nodul de care conectez nu e alive -> il scot pe asta din set, le dau join si bag combinata lor in set
    2. nodul de care conectez e alive -> ii scot pe ambii din set, le dau join si bag combinata lor in set
}

apoi, din grupul care mi-a mai ramas singur in set, afisez alea alive
**/
# Verdict Execution time Memory Grader output
1 Correct 6 ms 24924 KB Output is correct
2 Correct 6 ms 24924 KB Output is correct
3 Correct 4 ms 24924 KB Output is correct
4 Runtime error 25 ms 51292 KB Execution killed with signal 11
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 5 ms 24924 KB Output is correct
2 Correct 5 ms 24924 KB Output is correct
3 Correct 597 ms 85388 KB Output is correct
4 Correct 358 ms 78588 KB Output is correct
5 Correct 601 ms 81136 KB Output is correct
6 Correct 620 ms 82840 KB Output is correct
7 Correct 646 ms 83176 KB Output is correct
8 Correct 399 ms 78560 KB Output is correct
9 Correct 380 ms 82564 KB Output is correct
10 Correct 286 ms 75964 KB Output is correct
11 Correct 354 ms 80312 KB Output is correct
12 Correct 504 ms 79056 KB Output is correct
13 Correct 340 ms 78296 KB Output is correct
14 Correct 339 ms 78232 KB Output is correct
15 Correct 342 ms 80112 KB Output is correct
16 Correct 257 ms 77040 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 24924 KB Output is correct
2 Runtime error 757 ms 153424 KB Execution killed with signal 11
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 4 ms 24920 KB Output is correct
2 Runtime error 281 ms 154312 KB Execution killed with signal 11
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 6 ms 24924 KB Output is correct
2 Correct 6 ms 24924 KB Output is correct
3 Correct 4 ms 24924 KB Output is correct
4 Runtime error 25 ms 51292 KB Execution killed with signal 11
5 Halted 0 ms 0 KB -