이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define int long long int
void solve() {
int n, m, po = 0;
cin >> n >> m;
vector<int> s(n+1), ans(n+1, -1), vis(n+1, 0);
vector<pair<int, int>> ch;
vector<vector<int>> adj(n+1);
for (int i = 1; i <= n; i++) {
cin >> s[i];
po += s[i];
ch.push_back({s[i], i});
}
for (int i = 0; i < m; i++) {
int a, b;
cin >> a >> b;
adj[a].push_back(b);
adj[b].push_back(a);
}
sort(ch.begin(), ch.end());
for (int i = 1; i <= n; i++) {
if (ans[i] == -1) {
continue;
}
int inh = ch.back().first, ind = ch.back().second;
ch.pop_back();
priority_queue<pair<int, int>> pq;
vector<int> proc;
pq.push({0, ind});
while(!pq.empty()) {
auto [w, a] = pq.top();
pq.pop();
if (vis[a] == i) {
continue;
}
w = abs(w);
if (inh < w) {
break;
}
inh += w;
if (ans[a]) {
ans[ind] = 1;
break;
}
proc.push_back(a);
vis[a] = i;
for (int b : adj[a]) {
if (vis[b] == i) {
continue;
}
pq.push({-s[b], b});
}
}
if (po == inh || ans[ind] == 1) {
ans[ind] = 1;
} else {
ans[ind] = -1;
for (int j : proc) {
ans[j] = -1;
}
}
}
for (int i = 1; i <= n; i++) {
cout << max(ans[i], 0LL);
}
cout << endl;
}
int32_t main() {
ios::sync_with_stdio(0);
cin.tie(0);
int t;
t = 1;
while(t--) {
solve();
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |