#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
#include <queue>
using namespace std;
#define ll long long
#define pii pair<int, int>
const int INF = 1e9;
vector<int> r, p;
int find(int a) { return a == p[a] ? a : p[a] = find(p[a]); }
void link(int a, int b) {
a = find(a), b = find(b);
if (a == b) return;
if (r[a] < r[b]) swap(a, b);
if (r[a] == r[b]) ++r[a];
p[b] = a;
}
int main() {
int n, m; cin >> n >> m;
vector<int> nodes(n); for (int i = 0; i < n; ++i) cin >> nodes[i];
vector<pii> arr(n); for (int i = 0; i < n; ++i) arr[i] = {nodes[i], i};
sort(arr.begin(), arr.end()), reverse(arr.begin(), arr.end());
vector<int> io(n); for (int i = 0; i < n; ++i) io[arr[i].second] = i;
vector<vector<int>> adj(n);
for (int i = 0; i < m; ++i) {
int a, b; cin >> a >> b; --a, --b;
adj[a].push_back(b), adj[b].push_back(a);
}
r.resize(n), p.resize(n); for (int i = 0; i < n; ++i) p[i] = i;
vector<ll> sizes(n), psizes(n);
for (int i = n - 1; i >= 0; --i) {
int v = arr[i].second;
sizes[v] = nodes[v];
bool bs = false;
set<int> done;
for (int u : adj[v]) {
if (io[u] < io[v] && nodes[u] == nodes[v]) bs = true;
if (io[u] < io[v] || done.count(find(u))) continue;
sizes[v] += psizes[find(u)], done.insert(find(u));
}
if (!bs) for (int u : adj[v]) if (nodes[u] == nodes[v]) sizes[u] = sizes[v];
for (int u : adj[v]) if (io[u] > io[v]) link(u, v);
psizes[find(v)] = sizes[v];
}
vector<int> nd(n, INF); nd[arr[0].second] = 0;
queue<pii> q; q.push({arr[0].second, -1});
while (q.size()) {
int v = q.front().first, from = q.front().second, rn = max(nodes[v], nd[v]); q.pop();
//cout << v << ' ' << nd[v] << '\n';
for (int u : adj[v]) {
if (u == from) continue;
ll need = max(rn - sizes[u] + (nodes[u] >= nodes[v] ? sizes[v] : 0), 0LL);
if (need < nd[u]) {
nd[u] = need;
q.push({u, v});
}
}
}
//for (int num : nd) cout << num << '\n';
for (int i = 0; i < n; ++i) cout << (nd[i] == 0);
cout << '\n';
//for (ll sz : sizes) cout << sz << ' ';
//cout << '\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Incorrect |
1 ms |
296 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Incorrect |
334 ms |
21224 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Incorrect |
393 ms |
21316 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Incorrect |
1 ms |
296 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |