답안 #652035

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
652035 2022-10-21T07:49:00 Z Vladth11 Stranded Far From Home (BOI22_island) C++14
0 / 100
358 ms 27232 KB
#include <bits/stdc++.h>
#define debug(x) cerr << #x << " " << x << "\n"

using namespace std;

const int NMAX = 200001;

class DSU{
public:
    int parent[NMAX], cnt[NMAX], suma[NMAX];
    vector <int> comp[NMAX];
    int root(int x){
        if(x == parent[x])
            return x;
        return parent[x] = root(parent[x]);
    }
    void merge(int a, int b){
        a = root(a);
        b = root(b);
        if(a == b)
            return;
        if(cnt[a] < cnt[b])
            swap(a, b);
        cnt[a] += cnt[b];
        parent[b] = a;
        suma[a] += suma[b];
        for(auto x : comp[b])
            comp[a].push_back(x);
        comp[b].clear();
    }
}dsu;

vector <int> v[NMAX];


int rez[NMAX];
int p[NMAX];
int idx[NMAX];

bool cmp(int a, int b){
    return p[a] < p[b];
}

int main()
{
    int n, m, i;
    cin >> n >> m;
    for(i = 1; i <= n; i++){
        cin >> p[i];
        rez[i] = 1;
        dsu.parent[i] = i;
        dsu.cnt[i] = 1;
        dsu.suma[i] = p[i];
        dsu.comp[i].clear();
        dsu.comp[i].push_back(i);
        idx[i] = i;
    }
    for(i = 1; i <= m; i++){
        int a, b;
        cin >> a >> b;
        v[a].push_back(b);
        v[b].push_back(a);
    }
    sort(idx + 1, idx + n + 1, cmp);
    for(i = 1; i <= n; i++){
        int x = idx[i];
        for(auto vecin : v[x]){
            if(p[vecin] < p[x]){
                int rV = dsu.root(vecin);
                if(dsu.suma[rV] < p[x]){
                    for(auto y : dsu.comp[rV]){
                        rez[y] = 0;
                    }
                    dsu.comp[rV].clear();
                }
            }
            if(p[vecin] <= p[x]){
                dsu.merge(x, vecin);
            }
        }
    }
    set <int> st;
    for(i = 1; i <= n; i++)
        st.insert(dsu.root(i));
    for(i = 1; i <= n; i++){
        cout << rez[i] * (st.size() == 1);
    }
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 9684 KB Output is correct
2 Correct 7 ms 9684 KB Output is correct
3 Correct 5 ms 9684 KB Output is correct
4 Incorrect 8 ms 9812 KB Output isn't correct
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 9636 KB Output is correct
2 Correct 6 ms 9684 KB Output is correct
3 Incorrect 288 ms 27204 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 9684 KB Output is correct
2 Incorrect 358 ms 27232 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 9684 KB Output is correct
2 Incorrect 346 ms 27160 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 9684 KB Output is correct
2 Correct 7 ms 9684 KB Output is correct
3 Correct 5 ms 9684 KB Output is correct
4 Incorrect 8 ms 9812 KB Output isn't correct
5 Halted 0 ms 0 KB -