Submission #1350818

#TimeUsernameProblemLanguageResultExecution timeMemory
1350818cpismayilmmdv985Stranded Far From Home (BOI22_island)C++20
0 / 100
158 ms40376 KiB
#ifdef LOCAL
#include ".debug.hpp"
#else
#define debug(...) 42
#endif
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;
using ordered_set = tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>;

const int MXN = 2e5 + 5;
int64_t A[MXN];

/*** Basic DSU template ***/
struct DSU {
    int n;
    vector<int64_t> parent, component_size, component_sum; // # parameters
    DSU(int _n) {    // # initial defining parameters
        n = _n, parent.resize(n + 5), component_size.assign(n + 5, 1), component_sum.assign(n + 5, 0);
        for (int i = 1; i <= n; i++)    parent[i] = i, component_sum[i] = A[i];
    }
    inline int findpar(int node) { // # findpar function returns parent(ancestor) of node
        if (node == parent[node])   return node;
        return parent[node] = findpar(parent[node]);
    }
    inline bool unionset(int u, int v) { // # unionset function just unions two different components 
        u = findpar(u), v = findpar(v);
        if (u == v)  return false;
        if (A[u] <= A[v])   swap(u, v);
        else if (component_size[u] < component_size[v])  swap(u, v);
        component_size[u] += component_size[v], parent[v] = u, component_sum[u] += component_sum[v];
        return true;
    }
    inline int64_t total_sum(int u) {
       u = findpar(u);
      int64_t sum = component_sum[u];
      return sum;
    }
};

signed main() {
#ifndef VOID
   cin.tie(nullptr)->sync_with_stdio(false);
#endif

   int N, M;  cin >> N >> M;
   vector<vector<int>> adj(N + 1);
   map<int, vector<int>> mp;
   for (int i = 1; i <= N; i++)  cin >> A[i], mp[A[i]].push_back(i);
   DSU dsu(N + 5);
   for (int i = 0, U, V; i < M; i++)   cin >> U >> V, adj[U].push_back(V), adj[V].push_back(U);
   vector<int> mark(N + 1, 1);  
   for (auto &[w, vec] : mp) {
      for (auto &node : vec)  for (auto &child : adj[node])    if (A[child] <= w)   dsu.unionset(node, child);
      for (auto &node : vec)  for (auto &child : adj[node])    if (A[child] > dsu.total_sum(node)) mark[dsu.findpar(node)] = 0;
   }
   for (int i = 1; i <= N; i++)  cout << mark[i];

   return 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...