#include <bits/stdc++.h>
using namespace std;
#define dbg(x) //x
#define prt(x) dbg(cerr << x)
#define pv(x) prt(#x << " = " << x << '\n')
#define parr(x) dbg(prt(#x << " = "); for (auto y : x) prt(y << ' '); prt('\n'));
#define parr2d(x) dbg(prt(#x << " = \n"); for (auto y : x) {parr(y)}; prt('\n'));
/*
which ties are possible?
a can convince b if sum(a) > sum(b)
note that nodes are weighted
note that the tie with the strictly least number can't be 1 in the end
one with strictly most can be 1 in the end
how to greedily make 1 the largest?
dfs from the node
and keep adding the smallest adj greedily
and make sure that the size of this color's comp is greater than any other color's comp
subtask 1: brute force
subtask 2: tree; every node's parent has greater value than self
you can always get everything in your own subtree
go to the root
and for every node on the path to the root
you also absorb the parent
and everything else in the parent's subtree - note that that is guaranteed to be possible
if you've already absorbed the parent
this dp cond is checkable as you go up the tree
(this node's subtree sum >= parent's value?)
all the values to the root have to be true
for a node to work
subtask 3: just some weird stuff with set ig
*/
int main() {
ios::sync_with_stdio(0); cin.tie(0);
int n, m;
cin >> n >> m;
vector<long long> s(n);
for (int i = 0; i < n; i++) {
cin >> s[i];
}
bool inc = false;
for (int i = 0; i < n - 1; i++) {
if (s[i + 1] > s[i]) {
inc = true;
}
}
bool dif1 = true;
vector<vector<int>> edges(n);
for (int i = 0; i < m; i++) {
int x, y;
cin >> x >> y;
x--; y--;
pv(x); pv(y); pv(abs(x - y));
if (abs(x - y) > 1) dif1 = false;
edges[x].push_back(y);
edges[y].push_back(x);
}
pv(dif1);
if (n <= 2000 && m <= 2000) {
for (int i = 0; i < n; i++) {
vector<bool> vis(n, false);
vis[i] = true;
priority_queue<array<long long, 2>> que;
que.push({-s[i], i});
bool ok = true;
long long tot = s[i];
while (que.size()) {
long long sz = -que.top()[0], node = que.top()[1];
que.pop();
if (sz > tot) {
ok = false;
break;
}
if (node != i) {
tot += sz;
}
for (auto next : edges[node]) {
if (!vis[next]) {
vis[next] = true;
que.push({-s[next], next});
}
}
}
cout << (ok ? 1 : 0);
}
cout << '\n';
} else if (m == n - 1 && !inc) {
vector<bool> ok(n, true);
vector<long long> sum = s;
function<void(int, int)> dfs1 = [&] (int node, int par) {
for (auto next : edges[node]) {
if (next != par) {
dfs1(next, node);
sum[node] += sum[next];
}
}
};
dfs1(0, 0);
function<void(int, int)> dfs2 = [&] (int node, int par) {
if (node != 0) {
ok[node] = ok[par];
if (s[par] > sum[node]) {
ok[node] = false;
}
}
for (auto next : edges[node]) {
if (next != par) {
dfs2(next, node);
}
}
};
dfs2(0, 0);
for (int i = 0; i < n; i++) {
cout << (ok[i] ? 1 : 0);
}
cout << '\n';
} else if (dif1) {
/*
sum(l + 1, r - 1) < both a[l] and a[r]
so for each l find the min r so that sum(l + 1, r - 1) >= a[l]
for each r find the max l so that sum(l + 1, r - 1) >= a[r]
if mxl[r] <= l && mnr[l] >= r, they work probably
so probably find the l with the max mnr
or just an increasing seq of them that you can binary search
so you have n ranges of invalid elems
probably use set to remove all the elems
implies that if we start in [6, 6] we can't escape to the left OR right
*/
vector<long long> ps = s;
for (int i = 1; i < n; i++) {
ps[i] += ps[i - 1];
}
vector<int> mxl(n), mnr(n);
for (int i = 1; i < n; i++) {
if (i > 0) mxl[i] = upper_bound(ps.begin(), ps.end(), ps[i - 1] - s[i]) - ps.begin();
if (i < n - 1) mnr[i] = lower_bound(ps.begin(), ps.end(), ps[i + 1] + s[i]) - ps.begin();
}
vector<int> ord(n);
iota(ord.begin(), ord.end(), 0);
sort(ord.begin(), ord.end(), [&] (int x, int y) {
return mxl[x] > mxl[y];
});
int ptr = n - 1;
vector<array<int, 2>> arr, bad;
array<int, 2> tmp;
for (int i = 0; i < n; i++) {
pv(i); pv(ord[i]);
while (ptr >= mxl[ord[i]]) {
while (arr.size() && -arr.back()[0] <= mnr[ptr]) arr.pop_back();
arr.push_back({-mnr[ptr], ptr});
ptr--;
}
parr2d(arr);
// GOAL: find the first >= i
// ACTUAL GOAL: find the last <= -i
tmp = {-mxl[ord[i]], (int) 1e9};
int lb = upper_bound(arr.begin(), arr.end(), tmp) - arr.begin() - 1;
if (lb != -1 && arr[lb][1] < ord[i] - 1) {
bad.push_back({arr[lb][1] + 1, ord[i ] - 1});
parr(bad.back());
}
}
for (int i = n - 1; i > 0; i--) {
if (s[i] > ps[i - 1]) {
bad.push_back({0, i - 1});
break;
}
}
for (int i = 0; i < n - 1; i++) {
if (s[i] > ps[n - 1] - ps[i]) {
bad.push_back({i + 1, n - 1});
break;
}
}
parr2d(bad);
set<int> st;
for (int i = 0; i < n; i++) {
st.insert(i);
}
for (auto [l, r] : bad) {
auto it = st.lower_bound(l);
while (it != st.end() && *it <= r) {
st.erase(it);
it = st.lower_bound(l);
}
}
parr(mxl); parr(mnr); parr(ord);
for (int i = 0; i < n; i++) {
cout << (st.count(i) ? 1 : 0);
}
cout << '\n';
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
135 ms |
604 KB |
Output is correct |
5 |
Correct |
115 ms |
604 KB |
Output is correct |
6 |
Correct |
199 ms |
576 KB |
Output is correct |
7 |
Correct |
133 ms |
600 KB |
Output is correct |
8 |
Correct |
92 ms |
348 KB |
Output is correct |
9 |
Correct |
206 ms |
616 KB |
Output is correct |
10 |
Correct |
47 ms |
600 KB |
Output is correct |
11 |
Correct |
45 ms |
604 KB |
Output is correct |
12 |
Correct |
42 ms |
600 KB |
Output is correct |
13 |
Correct |
82 ms |
604 KB |
Output is correct |
14 |
Correct |
85 ms |
604 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
108 ms |
22100 KB |
Output is correct |
4 |
Correct |
74 ms |
22100 KB |
Output is correct |
5 |
Correct |
91 ms |
14412 KB |
Output is correct |
6 |
Correct |
84 ms |
14664 KB |
Output is correct |
7 |
Correct |
84 ms |
14676 KB |
Output is correct |
8 |
Correct |
84 ms |
14668 KB |
Output is correct |
9 |
Correct |
72 ms |
14672 KB |
Output is correct |
10 |
Correct |
62 ms |
15304 KB |
Output is correct |
11 |
Correct |
54 ms |
15336 KB |
Output is correct |
12 |
Correct |
80 ms |
14928 KB |
Output is correct |
13 |
Correct |
83 ms |
33432 KB |
Output is correct |
14 |
Correct |
81 ms |
33364 KB |
Output is correct |
15 |
Correct |
96 ms |
33364 KB |
Output is correct |
16 |
Correct |
75 ms |
32940 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Incorrect |
147 ms |
27828 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Incorrect |
77 ms |
12852 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
135 ms |
604 KB |
Output is correct |
5 |
Correct |
115 ms |
604 KB |
Output is correct |
6 |
Correct |
199 ms |
576 KB |
Output is correct |
7 |
Correct |
133 ms |
600 KB |
Output is correct |
8 |
Correct |
92 ms |
348 KB |
Output is correct |
9 |
Correct |
206 ms |
616 KB |
Output is correct |
10 |
Correct |
47 ms |
600 KB |
Output is correct |
11 |
Correct |
45 ms |
604 KB |
Output is correct |
12 |
Correct |
42 ms |
600 KB |
Output is correct |
13 |
Correct |
82 ms |
604 KB |
Output is correct |
14 |
Correct |
85 ms |
604 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
344 KB |
Output is correct |
17 |
Correct |
108 ms |
22100 KB |
Output is correct |
18 |
Correct |
74 ms |
22100 KB |
Output is correct |
19 |
Correct |
91 ms |
14412 KB |
Output is correct |
20 |
Correct |
84 ms |
14664 KB |
Output is correct |
21 |
Correct |
84 ms |
14676 KB |
Output is correct |
22 |
Correct |
84 ms |
14668 KB |
Output is correct |
23 |
Correct |
72 ms |
14672 KB |
Output is correct |
24 |
Correct |
62 ms |
15304 KB |
Output is correct |
25 |
Correct |
54 ms |
15336 KB |
Output is correct |
26 |
Correct |
80 ms |
14928 KB |
Output is correct |
27 |
Correct |
83 ms |
33432 KB |
Output is correct |
28 |
Correct |
81 ms |
33364 KB |
Output is correct |
29 |
Correct |
96 ms |
33364 KB |
Output is correct |
30 |
Correct |
75 ms |
32940 KB |
Output is correct |
31 |
Correct |
0 ms |
348 KB |
Output is correct |
32 |
Incorrect |
147 ms |
27828 KB |
Output isn't correct |
33 |
Halted |
0 ms |
0 KB |
- |