#include <bits/stdc++.h>
using namespace std;
struct DSU {
vector<int> e;
DSU(int N) { e = vector<int>(N, -1); }
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, vector<long long> &strength) { // union by size
x = get(x), y = get(y);
if (x == y) return false;
if (e[x] > e[y]) swap(x, y);
e[x] += e[y]; e[y] = x;
return true;
strength[x] += strength[y];
}
};
void dfs(int u, int works, vector<int> &winnable, vector<vector<int> > &val){
winnable[u] &= works;
for(int v : val[u]){
dfs(v, winnable[u], winnable, val);
}
}
int main(){
int n, m;
cin >> n >> m;
int arr[n];
for(int i = 0; i < n; i++){
cin >> arr[i];
}
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);
}
DSU dsu(n);
vector<int> winnable(n, 1);
vector<bool> visited(n);
vector<int> inserted(n);
vector<int> comp(n);
vector<long long> strength(n);
vector<vector<int> > mark(n);
vector<pair<int, int> > order(n);
for(int i = 0; i < n; i++){
strength[i] = arr[i];
order[i] = make_pair(arr[i], i);
}
sort(order.begin(), order.end());
for(auto t : order){
int u = t.second;
for(int v : adj[u]){
if(inserted[v]){
int w = comp[dsu.get(v)];
if(strength[dsu.get(v)] < arr[u]){
winnable[w] = 0;
}
if(!visited[w]){
mark[u].push_back(w);
visited[w] = 1;
}
}
}
for(int v : adj[u]){
if(inserted[v]){
visited[comp[dsu.get(v)]] = 0;
}
}
for(int v : adj[u]){
if(inserted[v]){
dsu.unite(u, v, strength);
}
}
comp[dsu.get(u)] = u;
inserted[u] = 1;
}
dfs(order[n - 1].second, 1, winnable, mark);
for(int i = 0; i < n; i++){
cout << winnable[i];
}
cout << "\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
304 KB |
Output is correct |
2 |
Incorrect |
386 ms |
31652 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Incorrect |
367 ms |
31980 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |