답안 #654368

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
654368 2022-10-31T07:58:45 Z erto Stranded Far From Home (BOI22_island) C++17
0 / 100
361 ms 45136 KB
#include <bits/stdc++.h>
typedef long long int ll;
typedef long double ld;
#define INF (int)(1e9 + 7)
#define INF2 998244353
#define N (ll)(2e5 + 5)
//#define f first
//#define s second
#define int ll
#define lsb(x) (x & (-x))
using namespace std;

int n, m, g, h;
int ans[N];
set<pair<int, int>> v[N];
int a[N];
int p[N], sz[N];

int get(int x){
    return (p[x] == x ? x : get(p[x]));
}

void unite(int x, int y){
    int a = get(x), b = get(y);
    if(a == b)return;
    if(sz[a] < sz[b])swap(a, b);
    sz[a] += sz[b];
    p[b] = a;
}

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

    for(int i=1; i<=m; i++){
        cin >> g >> h;
        v[g].insert({a[h], h});
        v[h].insert({a[g], g});
    }

    priority_queue<pair<int, int>> pq;
    for(int i=1; i<=n; i++){
        pq.push({-a[i], i});
    }

    for(int i=1; i<=n; i++){
        sz[i] = a[i];
        p[i] = i;
    }

    while(!pq.empty()){
        auto p = pq.top();
        pq.pop();
        int i = p.second;
        bool bb = 1;
        //cout << i << ' ';

        for(auto z : v[i]){
            int u = z.second; 
            //if(i == 4)cout << u << a[u] << sz[get(i)] << '\n';
            if(sz[get(i)] < a[u]){
                bb=0;
            }
            unite(i, u);
        }
        if(bb==0)ans[i] = -1;
        else{
            ans[i] = 1;
        }
    }

    for(int i=1; i<=n; i++){
        if(ans[i] == -1)ans[i] = 0;
        else{
            ans[i] = ans[get(i)];
        }
    }

    for(int i=1; i<=n; i++){
        cout << ans[i];
    }
}   


signed main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int T = 1;
    //cin>>T;
    for(int i=1; i<=T; i++){
        solve();
    }
}
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 9684 KB Output is correct
2 Correct 7 ms 9736 KB Output is correct
3 Correct 6 ms 9684 KB Output is correct
4 Incorrect 7 ms 10112 KB Output isn't correct
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 9744 KB Output is correct
2 Correct 5 ms 9684 KB Output is correct
3 Correct 218 ms 45120 KB Output is correct
4 Incorrect 232 ms 45136 KB Output isn't correct
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 9732 KB Output is correct
2 Incorrect 282 ms 44808 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 9684 KB Output is correct
2 Incorrect 361 ms 45132 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 9684 KB Output is correct
2 Correct 7 ms 9736 KB Output is correct
3 Correct 6 ms 9684 KB Output is correct
4 Incorrect 7 ms 10112 KB Output isn't correct
5 Halted 0 ms 0 KB -