#include <bits/stdc++.h>
using namespace std;
typedef int64_t ll;
const int N = 200'005;
int par[N], sz[N], bad[N];
ll cnt[N];
vector<int> adj[N];
pair<int, int> find(int u) {
if (par[u] == u) {
return {u, bad[u]};
}
pair<int, int> v = find(par[u]);
bad[u] |= v.second;
par[u] = v.first;
return {par[u], bad[u]};
}
void join(int u, int v) {
u = find(u).first, v = find(v).first;
if (u == v) return;
par[v] = u, sz[u] += sz[v], cnt[u] += cnt[v];
for (auto nxt : adj[v]) {
int x = find(nxt).first;
if (x != u) {
adj[u].push_back(x);
}
}
adj[v].clear();
}
int main() {
ios_base::sync_with_stdio(0), cin.tie(0);
int n, m;
cin >> n >> m;
for (int i = 0; i < n; i++) {
par[i] = i, sz[i] = 1, bad[i] = 0;
cin >> cnt[i];
}
for (int i = 0, u, v; i < m; i++) {
cin >> u >> v;
u--, v--;
adj[u].push_back(v);
adj[v].push_back(u);
}
priority_queue<pair<ll, int>> pq;
for (int i = 0; i < n; i++) {
pq.push({-cnt[i], i});
}
while (pq.size()) {
int node = pq.top().second;
ll val = -pq.top().first;
pq.pop();
if (node != par[node]) {
continue;
}
if (val != cnt[node]) {
continue;
}
if (sz[node] == n) {
continue;
}
int x = -1;
for (auto nxt : adj[node]) {
if (find(nxt).first != node && val >= cnt[find(nxt).first]) {
x = find(nxt).first;
break;
}
}
if (x == -1) {
bad[node] = 1;
continue;
}
join(node, x);
int New = find(node).first;
pq.push({-cnt[New], New});
}
for (int i = 0; i < n; i++) {
cout << !find(i).second;
}
cout << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8952 KB |
Output is correct |
2 |
Correct |
2 ms |
8792 KB |
Output is correct |
3 |
Correct |
1 ms |
8792 KB |
Output is correct |
4 |
Incorrect |
3 ms |
8796 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8792 KB |
Output is correct |
2 |
Correct |
1 ms |
8792 KB |
Output is correct |
3 |
Incorrect |
173 ms |
20784 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8792 KB |
Output is correct |
2 |
Incorrect |
229 ms |
20928 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8792 KB |
Output is correct |
2 |
Incorrect |
280 ms |
20676 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8952 KB |
Output is correct |
2 |
Correct |
2 ms |
8792 KB |
Output is correct |
3 |
Correct |
1 ms |
8792 KB |
Output is correct |
4 |
Incorrect |
3 ms |
8796 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |