#include "bits/stdc++.h"
using namespace std;
using ll = long long;
const int MAXN = 2e5+5;
ll in[MAXN], sum[MAXN];
int ans[MAXN], par[MAXN];
vector<int> unprocessed[MAXN];
int find(int v)
{
if(v == par[v])
return v;
return par[v] = find(par[v]);
}
struct cmp
{
bool operator()(const int &a, const int &b)
const {
if(sum[a] != sum[b])
return sum[a] < sum[b];
return a < b;
}
};
int merge(int a, int b)
{
a = find(a), b = find(b);
if(a == b)
return a;
if(unprocessed[a].size() < unprocessed[b].size())
swap(a, b);
for(int e : unprocessed[b])
unprocessed[a].push_back(e);
sum[a] += sum[b];
sum[b] = 0;
par[b] = a;
unprocessed[b].clear();
return a;
}
vector < vector< int > > adj(MAXN);
int main()
{
set<int, cmp> available;
int n, m;
cin >> n >> m;
vector < pair<int, int> > sorted;
for(int i = 1; i <= n; i++) {
cin >> in[i];
sorted.push_back({in[i], i});
ans[i] = 1;
sum[i] = in[i];
par[i] = i;
unprocessed[i].push_back(i);
}
sort(sorted.begin(), sorted.end());
for(int i = 1; i <= m; i++)
{
int f, s;
cin >> f >> s;
adj[f].push_back(s);
adj[s].push_back(f);
}
int last = 0;
for(int i = 0; i < n; i++)
{
last = i;
while(i < n && sorted[last].first == sorted[i].first)
i++;
i--;
while(last <= i)
{
int idx = sorted[last].second;
//cout << in[idx] << " " << idx << "\n";
for(int e : adj[idx])
{
if(in[e] <= in[idx]) {
merge(e, idx);
//cout << e << " " << idx << "\n";
}
}
//cout << find(idx) << "\n";
available.insert(find(idx));
last++;
}
if(i < n - 1)
{
int pass = sorted[i + 1].first;
vector<int> remove;
for(int e : available)
{
//cout << i << " " << e << " " << sum[e] << "\n";
if(sum[e] < pass)
{
remove.push_back(e);
for(int j : unprocessed[e])
ans[j] = 0;
} else
break;
}
for(int e : remove)
{
available.erase(e);
}
}
}
for(int i = 1; i <= n; i++)
cout << ans[i];
cout << "\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
9684 KB |
Output is correct |
2 |
Correct |
5 ms |
9684 KB |
Output is correct |
3 |
Correct |
5 ms |
9684 KB |
Output is correct |
4 |
Incorrect |
8 ms |
9940 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
9684 KB |
Output is correct |
2 |
Correct |
5 ms |
9684 KB |
Output is correct |
3 |
Incorrect |
330 ms |
33632 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
9684 KB |
Output is correct |
2 |
Incorrect |
538 ms |
45560 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
9684 KB |
Output is correct |
2 |
Incorrect |
490 ms |
39116 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
9684 KB |
Output is correct |
2 |
Correct |
5 ms |
9684 KB |
Output is correct |
3 |
Correct |
5 ms |
9684 KB |
Output is correct |
4 |
Incorrect |
8 ms |
9940 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |