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 <bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<int, int>
#define vb vector<bool>
#define vi vector<ll>
#define vpii vector<pii>
#define vvpii vector<vpii>
#define vvi vector<vi>
#define tiii tuple<int, int, int>
#define vtiii vector<tiii>
#define sz(x) ((int)(x).size())
#define all(x) begin(x), end(x)
struct DSU
{
vector<int> arr;
vector<ll> sum;
vvi nodes;
void init(vi &s)
{
arr.resize(sz(s)), sum.resize(sz(s)), nodes.resize(sz(s));
for (int i = 0; i < sz(s); ++i)
arr[i] = i, sum[i] = s[i], nodes[i].push_back(i);
}
int find(int i)
{
if (arr[i] == i)
return i;
return arr[i] = find(arr[i]);
}
void clear(int i)
{
i = find(i);
nodes[i].clear();
}
void join(int a, int b)
{
a = find(a);
b = find(b);
if (a == b)
return;
if (sz(nodes[a]) > sz(nodes[b]))
swap(a, b);
sum[b] += sum[a];
for (int x : nodes[a])
nodes[b].push_back(x);
nodes[a].clear();
arr[a] = b;
}
ll getsum(int i)
{
i = find(i);
return sum[i];
}
void answer(int i, vi &ans)
{
i = find(i);
for (int j : nodes[i])
ans[j] = true;
}
};
int main()
{
ios::sync_with_stdio(false);
cout.tie(0);
cin.tie(0);
int n, m;
cin >> n >> m;
vi s(n);
vvi adj(n);
for (int i = 0; i < n; ++i)
cin >> s[i];
for (int i = 0, a, b; i < m; ++i)
{
cin >> a >> b;
--a, --b;
adj[a].push_back(b);
adj[b].push_back(a);
}
vi order(n);
iota(all(order), 0);
sort(all(order), [&](ll &a, ll &b)
{ return s[a] < s[b]; });
DSU dsu;
dsu.init(s);
for (int v : order)
{
for (int w : adj[v])
{
if (s[w] > s[v])
continue; // neighbor is larger, I will go the other direction later
if (dsu.getsum(w) < s[v])
{ // the nodes of the other subtree aren't enough to spread their tie
dsu.clear(w);
}
dsu.join(w, v);
}
/*
for (int tmp = 0; tmp < n; ++tmp)
{
cout << tmp << " : (" << dsu.find(tmp) << ") : " << dsu.getsum(tmp) << " mit ";
for (int w : dsu.nodes[dsu.find(tmp)])
cout << " " << w;
cout << endl;
}
*/
}
vi ans(n, 0);
dsu.answer(order.back(), ans);
for (int x : ans)
cout << x;
cout << endl;
}
# | 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... |