#include<bits/stdc++.h>
using namespace std;
using ll = long long;
struct dsu {
vector<int> par;
vector<vector<int>> el;
dsu(int n) : par(n), el(n) {
for (int i = 0; i < n; i++) par[i] = i, el[i] = {i};
}
int get(int i) {
return par[i] == i ? par[i] : par[i] = get(par[i]);
}
void merge(int a, int b) {
a = get(a);
b = get(b);
if (el[a].size() < el[b].size())
swap(a, b);
if (a != b) {
par[b] =a;
for (int x : el[b])
el[a].push_back(x);
}
}
};
int main() {
int n, m;
cin >> n >> m;
vector s(n, 0);
for (int& i : s) cin >> i;
vector<int> ix(n);
for (int i{}; i < n ;i++) ix[i] = i;
stable_sort(ix.begin(), ix.end(), [&](int a, int b) {
return s[a] > s[b];
});
vector<int> jx(n);
for (int i = 0; i < n; i ++)
jx[ix[i]] = i;
stable_sort(s.begin(), s.end(), greater{});
vector<vector<int>> adj(n);
for (int i = 0; i < m ;i++) {
int u, v;
cin >> u >> v;
u--;v--;
adj[jx[u]].push_back(jx[v]);
adj[jx[v]].push_back(jx[u]);
}
//for (int i : ix) cout << i << ' ';cout << endl;
//for (int i : jx) cout << i << ' ';cout << endl;
//for (int i : s) cout << i << ' ';cout << endl;
string res(n, '?');
vector<int> back(n, -1);
dsu cc(n);
for (int i =n -1; i >= 0;i --) {
for (int j : adj[i] ){
if (j > i)
cc.merge(i, j);
}
ll sum = 0;
for (int j : cc.el[cc.get(i)]) {
sum += s[j];
for (int k : adj[j]) {
if (k < i)
back[i] = max(back[i], k);
}
}
if (back[i] != -1 && s[back[i]] > sum)
back[i] = -1;
if (back[i] == -1) {
if (i == 0)
res[i] = '1';
else res[i] = '0';
}
}
string ans(n, '?');
for (int i = 1; i < n;i++) {
if (back[i] != -1) res[i] = res[back[i]];
}
for (int i = 0; i < n; i++) {
ans[ix[i]] = res[i];
}
cout << ans << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
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 |
Correct |
10 ms |
468 KB |
Output is correct |
5 |
Correct |
5 ms |
468 KB |
Output is correct |
6 |
Correct |
9 ms |
504 KB |
Output is correct |
7 |
Correct |
9 ms |
520 KB |
Output is correct |
8 |
Correct |
7 ms |
468 KB |
Output is correct |
9 |
Correct |
8 ms |
616 KB |
Output is correct |
10 |
Correct |
3 ms |
596 KB |
Output is correct |
11 |
Correct |
3 ms |
596 KB |
Output is correct |
12 |
Correct |
3 ms |
596 KB |
Output is correct |
13 |
Correct |
7 ms |
468 KB |
Output is correct |
14 |
Correct |
4 ms |
556 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Execution timed out |
1086 ms |
26640 KB |
Time limit exceeded |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
514 ms |
34520 KB |
Output is correct |
3 |
Correct |
468 ms |
34624 KB |
Output is correct |
4 |
Execution timed out |
1080 ms |
26516 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Execution timed out |
1081 ms |
28444 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 |
Correct |
10 ms |
468 KB |
Output is correct |
5 |
Correct |
5 ms |
468 KB |
Output is correct |
6 |
Correct |
9 ms |
504 KB |
Output is correct |
7 |
Correct |
9 ms |
520 KB |
Output is correct |
8 |
Correct |
7 ms |
468 KB |
Output is correct |
9 |
Correct |
8 ms |
616 KB |
Output is correct |
10 |
Correct |
3 ms |
596 KB |
Output is correct |
11 |
Correct |
3 ms |
596 KB |
Output is correct |
12 |
Correct |
3 ms |
596 KB |
Output is correct |
13 |
Correct |
7 ms |
468 KB |
Output is correct |
14 |
Correct |
4 ms |
556 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Execution timed out |
1086 ms |
26640 KB |
Time limit exceeded |
18 |
Halted |
0 ms |
0 KB |
- |