//#pragma GCC optimize("Ofast")
//#pragma GCC target("avx,avx2")
#include "bits/stdc++.h"
using namespace std;
using ll = long long;
int INF = 2e9;
struct dsu {
vector<int> p, size;
void init(int n) {
p.assign(n + 1, 0);
size.assign(n + 1, 1);
for (int i = 1; i <= n; i++)
p[i] = i;
}
int get(int a) {
return (p[a] == a ? a : get(p[a]));
}
void add(int a, int b) {
a = get(a);
b = get(b);
if (size[a] > size[b]) swap(a, b);
p[a] = b;
size[b] += size[a];
}
};
void solve() {
int n, m;
cin >> n >> m;
vector<ll> a(n + 1), sum(n + 1);
vector<vector<int>> g(n + 1);
vector<int> ans(n + 1, -1);
for (int i = 1; i <= n; i++) {
cin >> a[i];
sum[i] = a[i];
}
for (int i = 0; i < m; i++) {
int u, v;
cin >> u >> v;
g[u].emplace_back(v);
g[v].emplace_back(u);
}
dsu ds;
ds.init(n);
int no_ans = n + 1;
for (int i = n; i > 0; i--) {
for (int j : g[i]) {
if (i <= j) continue;
// cout << i << ' ' << j << endl;
// cout << '\t' << sum[i] << ' ' << a[j] << endl;
if (sum[i] < a[j]) {
for (int k = 1; k <= n; k++) {
if (ds.get(k) == ds.get(i))
ans[k] = 0;
}
}
ll x = sum[i] + sum[j];
sum[j] = x;
sum[i] = x;
ds.add(i, j);
}
}
// cout << no_ans << endl;
for (int i = 1; i <= n; i++) {
cout << (ans[i] == -1 ? 1 : ans[i]);
}
}
int main() {
ios_base::sync_with_stdio(false);
cout.tie(nullptr);
cin.tie(nullptr);
int queries = 1;
#ifdef test_cases
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
cin >> queries;
#else
// cin >> queries;
#endif
for (int test_case = 1; test_case <= queries; test_case++) {
solve();
// cout << '\n';
}
}
Compilation message
island.cpp: In function 'void solve()':
island.cpp:48:9: warning: unused variable 'no_ans' [-Wunused-variable]
48 | int no_ans = n + 1;
| ^~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Incorrect |
31 ms |
436 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Execution timed out |
1094 ms |
16752 KB |
Time limit exceeded |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Incorrect |
112 ms |
16776 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Execution timed out |
1087 ms |
16616 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Incorrect |
31 ms |
436 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |