#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
int N;
vector<vector<int>> adj;
vector<int> cnt;
struct DSU {
vector<int> parent, rank;
void init(int n) {
parent.resize(n);
rank.resize(n);
iota(parent.begin(), parent.end(), 0);
}
int get(int x) {
if (parent[x] == x) return x;
return parent[x] = get(parent[x]);
}
void merge(int a, int b) {
a = get(a);
b = get(b);
if (a != b) {
if (rank[a] < rank[b]) swap(a, b);
parent[b] = a;
if (rank[a] == rank[b]) rank[a]++;
}
}
bool in_union(int a, int b) {
return get(a) == get(b);
}
bool all_connected() {
int a = get(1);
for (int i = 2; i < (int)parent.size(); i++) {
if (get(i) != a) return false;
}
return true;
}
};
bool check(int a) {
priority_queue<tuple<int, int, int>> pq;
long long s = cnt[a];
for (auto &v: adj[a]) {
pq.push(make_tuple(-cnt[v], a, v));
}
DSU ds;
ds.init(N + 1);
while (!pq.empty()) {
int w, u, v;
tie(w, u, v) = pq.top(); pq.pop();
w = -w;
if (w > s || ds.in_union(a, v)) continue;
s += w;
for (auto &b: adj[v]) {
if (ds.in_union(a, b)) continue;
pq.push(make_tuple(-cnt[b], v, b));
}
ds.merge(u, v);
}
return ds.all_connected();
}
int main() {
ios::sync_with_stdio(0); cin.tie(0);
int M;
cin >> N >> M;
cnt.resize(N + 1);
for (int i = 1; i <= N; i++) cin >> cnt[i];
adj.resize(N + 1);
for (int i = 0, u, v; i < M; i++) {
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
string res;
for (int i = 1; i <= N; i++) {
res.push_back((check(i) ? '1' : '0'));
}
cout << res;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
238 ms |
424 KB |
Output is correct |
5 |
Correct |
190 ms |
460 KB |
Output is correct |
6 |
Correct |
445 ms |
456 KB |
Output is correct |
7 |
Correct |
243 ms |
432 KB |
Output is correct |
8 |
Correct |
216 ms |
420 KB |
Output is correct |
9 |
Correct |
355 ms |
468 KB |
Output is correct |
10 |
Correct |
85 ms |
452 KB |
Output is correct |
11 |
Correct |
84 ms |
436 KB |
Output is correct |
12 |
Correct |
103 ms |
452 KB |
Output is correct |
13 |
Correct |
153 ms |
436 KB |
Output is correct |
14 |
Correct |
120 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Execution timed out |
1083 ms |
14452 KB |
Time limit exceeded |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Execution timed out |
1066 ms |
13732 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Execution timed out |
1064 ms |
14300 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
238 ms |
424 KB |
Output is correct |
5 |
Correct |
190 ms |
460 KB |
Output is correct |
6 |
Correct |
445 ms |
456 KB |
Output is correct |
7 |
Correct |
243 ms |
432 KB |
Output is correct |
8 |
Correct |
216 ms |
420 KB |
Output is correct |
9 |
Correct |
355 ms |
468 KB |
Output is correct |
10 |
Correct |
85 ms |
452 KB |
Output is correct |
11 |
Correct |
84 ms |
436 KB |
Output is correct |
12 |
Correct |
103 ms |
452 KB |
Output is correct |
13 |
Correct |
153 ms |
436 KB |
Output is correct |
14 |
Correct |
120 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
17 |
Execution timed out |
1083 ms |
14452 KB |
Time limit exceeded |
18 |
Halted |
0 ms |
0 KB |
- |