#pragma GCC optimize ("O3")
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = 2e5+5;
int n, m, a[N], p[N], sz[N], dp[N];
vector<int> adj[N];
pair<int, int> s[N];
bitset<N> ok;
set<pair<int, int>> mn[N];
int findSet(int x) {
return p[x] = (p[x] == x ? x : findSet(p[x]));
}
void unite(int x, int y) {
//cerr << "Unite -> " << x+1 << " " << y+1 << endl;
x = findSet(x);
y = findSet(y);
if (x == y) return;
int X = x;
if (mn[x].size() < mn[y].size()) swap(x, y);
p[y] = x;
sz[x] += sz[y];
for (pair<int, int> z : mn[y]) {
mn[x].insert(z);
}
while (*mn[x].begin() <= make_pair(a[X], X)) mn[x].erase(mn[x].begin());
}
signed main() {
ios::sync_with_stdio(false); cin.tie(nullptr);
cin >> n >> m;
a[n] = INT_MAX;
for (int i = 0; i < n; i++) {
cin >> a[i];
s[i] = make_pair(a[i], i);
}
sort(s, s+n);
for (int i = 0; i < m; i++) {
int x, y;
cin >> x >> y;
x--; y--;
adj[x].push_back(y);
adj[y].push_back(x);
}
for (int i = 0; i < n; i++) {
p[i] = i;
sz[i] = a[i];
mn[i].insert(make_pair(a[i], i));
mn[i].insert(make_pair(INT_MAX, -1));
mn[i].insert(make_pair(INT_MAX, -1));
for (int& j : adj[i]) {
if (a[j] >= a[i]) mn[i].insert(make_pair(a[j], j));
}
}
for (int i = 0; i < n; i++) {
// ver mi suma y el minimo mas grande
int x = s[i].second;
int y = a[x];
int nwMn = n;
for (int& j : adj[x]) {
int z = findSet(j);
if (make_pair(a[j], j) < make_pair(a[x], x)) y += sz[z];
//cerr << x+1 << " " << z+1 << " " << endl;
if (mn[z].begin()->second != x && mn[z].begin()->first < a[nwMn]) {
nwMn = mn[z].begin()->second;
}
else {
auto it = mn[z].begin();
++it;
if (it->first < a[nwMn]) {
nwMn = it->second;
}
}
}
//cerr << x+1 << " " << y << " " << nwMn+1 << endl;
if (nwMn == n) {
//cerr << "a" << endl;
dp[x] = x;
ok[x] = 1;
}
else if (a[nwMn] <= y) {
//cerr << "b" << endl;
dp[x] = nwMn;
}
else {
//cerr << "c" << endl;
dp[x] = x;
ok[x] = 0;
}
mn[x].erase(make_pair(a[x], x));
for (int& j : adj[x]) {
if (make_pair(a[j], j) < make_pair(a[x], x)) unite(x, j);
}
}
for (int i = n-1; i >= 0; i--) {
ok[i] = ok[dp[i]];
}
for (int i = 0; i < n; i++) {
cout << ok[dp[i]];
}
cout << "\n";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
14420 KB |
Output is correct |
2 |
Correct |
7 ms |
14420 KB |
Output is correct |
3 |
Correct |
7 ms |
14420 KB |
Output is correct |
4 |
Incorrect |
8 ms |
14932 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
14544 KB |
Output is correct |
2 |
Correct |
7 ms |
14420 KB |
Output is correct |
3 |
Incorrect |
213 ms |
73296 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
14420 KB |
Output is correct |
2 |
Incorrect |
327 ms |
71900 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
14420 KB |
Output is correct |
2 |
Incorrect |
389 ms |
74224 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
14420 KB |
Output is correct |
2 |
Correct |
7 ms |
14420 KB |
Output is correct |
3 |
Correct |
7 ms |
14420 KB |
Output is correct |
4 |
Incorrect |
8 ms |
14932 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |