Submission #1029980

#TimeUsernameProblemLanguageResultExecution timeMemory
1029980hasan2006Stranded Far From Home (BOI22_island)C++17
100 / 100
415 ms104636 KiB
#include <bits/stdc++.h>

using namespace std;

#define TL ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
#define rall(s) s.rbegin(),s.rend()
#define all(s) s.begin(),s.end()
#define pb push_back
#define se second
#define fi first
#define ll long long
#define ld long double
#define YES cout<<"YES\n"
#define Yes cout<<"Yes\n"
#define yes cout<<"yes\n"
#define NO cout<<"NO\n"
#define No cout<<"No\n"
#define no cout<<"no\n"


const int N = 5e5 + 9 , mod = 1e9 + 7;
ll a[N] , b[N] , dp[N] , c[N] , d[N] , p[N] , sz[N];
vector<int>v[N];
set<int>st[N];

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

void join(int x , int y , int k){
    x = get(x);
    y = get(y);
    if(c[x] >= k){
        if(st[x].size() > st[y].size())
            swap(st[x] , st[y]);
        for(auto to : st[x])
            st[y].insert(to);
    }
    if(x != y){
        if(sz[x] > sz[y])
            swap( x, y) , swap(st[x] , st[y]);
        p[x] = y;
        sz[y] += sz[x];
        c[y] += c[x];
    }
}

void solve()
{
    ll n , q , i , j ,l ,r , x , y , s = 0 , f , k , m , mn = 1e18, mx = 0 ;
    cin>>n>>m;
    vector<pair<int,int>>vc;
    for(i = 1;i <= n; i++){
        cin>>a[i];
        p[i] = i , sz[i] = 1, c[i] = a[i] , st[i].insert(i);
        vc.pb({a[i] , i});
    }
    for(i = 1; i <= m; i++){
        cin>>l>>r;
        v[l].pb(r);
        v[r].pb(l);
    }
    sort(all(vc));
    for(auto o : vc){
        x = o.se ,  y =o.fi;
        //cout<<x<<" ";
        d[x] = 1;
        for(auto to : v[x])
            if(get(to) != get(x) && d[to] == 1)
                join(to , x , y);
    }
    for(i = 1; i <= n; i++)
        cout<<(st[get(i)].find(i) != st[get(i)].end());
}

int main(){
    TL;

    /*#ifndef ONLINE_JUDGE
        freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
    #endif*/
    int t = 1;
    //cin>>t;
    while(t--)
     {
        solve();
     }
}
// Author : حسن

Compilation message (stderr)

island.cpp: In function 'void solve()':
island.cpp:50:12: warning: unused variable 'q' [-Wunused-variable]
   50 |     ll n , q , i , j ,l ,r , x , y , s = 0 , f , k , m , mn = 1e18, mx = 0 ;
      |            ^
island.cpp:50:20: warning: unused variable 'j' [-Wunused-variable]
   50 |     ll n , q , i , j ,l ,r , x , y , s = 0 , f , k , m , mn = 1e18, mx = 0 ;
      |                    ^
island.cpp:50:38: warning: unused variable 's' [-Wunused-variable]
   50 |     ll n , q , i , j ,l ,r , x , y , s = 0 , f , k , m , mn = 1e18, mx = 0 ;
      |                                      ^
island.cpp:50:46: warning: unused variable 'f' [-Wunused-variable]
   50 |     ll n , q , i , j ,l ,r , x , y , s = 0 , f , k , m , mn = 1e18, mx = 0 ;
      |                                              ^
island.cpp:50:50: warning: unused variable 'k' [-Wunused-variable]
   50 |     ll n , q , i , j ,l ,r , x , y , s = 0 , f , k , m , mn = 1e18, mx = 0 ;
      |                                                  ^
island.cpp:50:58: warning: unused variable 'mn' [-Wunused-variable]
   50 |     ll n , q , i , j ,l ,r , x , y , s = 0 , f , k , m , mn = 1e18, mx = 0 ;
      |                                                          ^~
island.cpp:50:69: warning: unused variable 'mx' [-Wunused-variable]
   50 |     ll n , q , i , j ,l ,r , x , y , s = 0 , f , k , m , mn = 1e18, mx = 0 ;
      |                                                                     ^~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...