#include<bits/stdc++.h>
using namespace std;
struct DSU {
vector<int> e;
vector<int> sum;
DSU(int N) {
e = vector<int>(N, -1);
sum=vector<int>(N,0);
}
int get(int x) { return e[x] < 0 ? x : e[x] = get(e[x]); }
bool same_set(int a, int b) { return get(a) == get(b); }
int size(int x) { return -e[get(x)]; }
bool unite(int x, int y) {
x = get(x), y = get(y);
if (x == y) return false;
if (e[x] > e[y]) swap(x, y);
sum[x]+=sum[y];
sum[y]=0;
e[x] += e[y]; e[y] = x; return true;
}
};
const int N=200'010;
int s[N];
vector<int> gg[N];
vector<int> g[N];
int main () {
cin.tie(0)->sync_with_stdio(0);
int n, m;
cin >> n >> m;
set<int> ss;
map<int,int> cc;
for(int i = 0;i<n;i++) {
cin >> s[i];
ss.insert(s[i]);
cc[s[i]]=1;
}
{
int cnt=0;
for(auto &i:cc) {
i.second=cnt++;
}
}
for(int i = 0;i<m;i++) {
int a, b;
cin >> a >> b;
a--,b--;
gg[a].push_back(b);
gg[b].push_back(a);
}
vector<int> ns[n];
for(int i = 0;i<n;i++) {
ns[cc[s[i]]].push_back(i);
}
DSU d(n);
vector<int> par(n);
iota(par.begin(),par.end(),0);
vector<int> res(n,1);
for(int i = 0;i<n;i++)d.sum[i]=s[i];
for(int i = 0;i<n;i++) {
for(int at:ns[i]) {
for(int to:gg[at]) {
if(cc[s[to]]<=i) {
d.unite(at,to);
}
}
for(int to:gg[at]) {
if(cc[s[to]]<=i and res[par[to]]) {
par[to]=d.get(at);
}
}
}
if(ns[i].size()) {
auto it=ss.upper_bound(s[ns[i][0]]);
if(it==ss.end())continue;
for(int at:ns[i]) {
if(d.sum[d.get(at)]<(*it)){
res[d.get(at)]=0;
par[at]=d.get(at);
}
}
}
}
for(int i = 0;i<n;i++) {
cout << res[par[i]] << "";
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
9684 KB |
Output is correct |
2 |
Correct |
7 ms |
9684 KB |
Output is correct |
3 |
Correct |
5 ms |
9672 KB |
Output is correct |
4 |
Incorrect |
9 ms |
10012 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
9684 KB |
Output is correct |
2 |
Correct |
5 ms |
9684 KB |
Output is correct |
3 |
Incorrect |
348 ms |
49736 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
9684 KB |
Output is correct |
2 |
Incorrect |
791 ms |
49528 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
9684 KB |
Output is correct |
2 |
Incorrect |
195 ms |
25844 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
9684 KB |
Output is correct |
2 |
Correct |
7 ms |
9684 KB |
Output is correct |
3 |
Correct |
5 ms |
9672 KB |
Output is correct |
4 |
Incorrect |
9 ms |
10012 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |