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>
#define ll long long
#define ld long double
#define sp ' '
#define en '\n'
#define smin(a, b) a = min(a, b)
#define smax(a, b) a = max(a, b)
using namespace std;
const int N = 2e5 + 2;
int mod = 1000000007;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n, m;
cin >> n >> m;
vector<ll> s(n), sum(n);
for (int i = 0; i < n; i++) {
cin >> s[i];
sum[i] = s[i];
}
vector<int> a(m), b(m);
for (int i = 0; i < m; i++) {
cin >> a[i] >> b[i];
a[i] -= 1;
b[i] -= 1;
if (s[a[i]] > s[b[i]]) swap(a[i], b[i]);
}
vector<int> order(m);
iota(order.begin(), order.end(), 0);
sort(order.begin(), order.end(), [&] (int i, int j) {
return s[b[i]] < s[b[j]];
});
vector<int> p(n);
iota(p.begin(), p.end(), 0);
function<int(int)> Find = [&] (int x) {
if (x == p[x]) return x;
return p[x] = Find(p[x]);
};
vector<vector<int>> g(n);
function<void(int, int)> Unite = [&] (int u, int v) {
u = Find(u);
v = Find(v);
p[u] = v;
sum[v] += sum[u];
g[u].push_back(v);
};
vector<bool> can(n, 1);
for (int i : order) {
int x = Find(a[i]);
int y = Find(b[i]);
if (x == y) continue;
if (sum[x] < s[y]) can[x] = 0;
Unite(x, y);
}
vector<bool> was(n);
function<int(int)> Dfs = [&] (int s) {
if (was[s]) return can[s];
was[s] = 1;
for (int u : g[s]) {
if (Dfs(u) == 0) can[s] = 0;
}
return can[s];
};
for (int i = 0; i < n; i++) {
if (!was[i]) Dfs(i);
}
for (int i = 0; i < n; i++) cout << can[i];
cout << en;
return 0;
}
# | 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... |