This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <vector>
typedef long long ll;
typedef long double ld;
using namespace std;
const int maxn = 2e5 + 5;
struct comp
{
ll sum = 0;
vector<int> s;
};
vector<comp> c(maxn);
vector<int> a(maxn), p(maxn);
int root(int u)
{
return u == p[u] ? u : p[u] = root(p[u]);
}
int merge(int u, int v)
{
u = root(u), v = root(v);
if (u == v) return u;
if (c[u].s.size() < c[v].s.size()) swap(u, v);
p[v] = u;
c[u].sum += c[v].sum;
vector<int> un;
int iu = 0, iv = 0;
while (un.size() < c[u].s.size() + c[v].s.size())
{
if (iu < c[u].s.size() && (iv == c[v].s.size() || a[c[u].s[iu]] < a[c[v].s[iv]])) un.push_back(c[u].s[iu++]);
else un.push_back(c[v].s[iv++]);
}
c[u].s = un;
return u;
}
bool cmp(int i, int j)
{
return a[i] < a[j];
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n, m;
cin >> n >> m;
vector<pair<int, int> > w;
for (int i = 0; i < n; i++)
{
cin >> a[i];
w.push_back({ a[i], i });
c[i].sum = a[i];
p[i] = i;
}
vector<vector<int> > g(n);
for (int i = 0; i < m; i++)
{
int u, v;
cin >> u >> v;
u--, v--;
if (make_pair( a[u], u ) > make_pair( a[v], v )) swap(u, v);
c[u].s.push_back(v);
g[v].push_back(u);
}
for (int i = 0; i < n; i++) sort(c[i].s.begin(), c[i].s.end(), cmp);
vector<int> de(n, -1);
sort(w.begin(), w.end());
for (pair<int, int> p : w)
{
int u = p.second, cm = u;
for (int v : g[u]) cm = merge(cm, v);
vector<int> bad = c[cm].s;
c[cm].s.clear();
for (int i : bad) if (i != u) c[cm].s.push_back(i);
if (!c[cm].s.empty() && a[c[cm].s[0]] <= c[cm].sum) de[u] = c[cm].s[0];
}
reverse(w.begin(), w.end());
vector<bool> ans(n, false);
for (pair<int, int> p : w)
{
int u = p.second;
if (p == w[0]) ans[u] = true;
else if (de[u] != -1) ans[u] = ans[de[u]];
}
for (int i = 0; i < n; i++) cout << "01"[ans[i]];
cout << "\n";
return 0;
}
Compilation message (stderr)
island.cpp: In function 'int merge(int, int)':
island.cpp:42:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
42 | if (iu < c[u].s.size() && (iv == c[v].s.size() || a[c[u].s[iu]] < a[c[v].s[iv]])) un.push_back(c[u].s[iu++]);
| ~~~^~~~~~~~~~~~~~~
island.cpp:42:39: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
42 | if (iu < c[u].s.size() && (iv == c[v].s.size() || a[c[u].s[iu]] < a[c[v].s[iv]])) un.push_back(c[u].s[iu++]);
| ~~~^~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |