This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define REP(i, j, k) for (int i = j; i < k; i++)
#define RREP(i, j, k) for (int i = j; i >= k; i--)
template <class T>
inline bool mnto(T &a, const T b) {return a > b ? a = b, 1 : 0;}
template <class T>
inline bool mxto(T &a, const T b) {return a < b ? a = b, 1 : 0;}
typedef long long ll;
#define FI first
#define SE second
typedef pair<int, int> ii;
typedef pair<ll, ll> pll;
#define ALL(x) x.begin(), x.end()
#define SZ(x) (int) x.size()
#define pb push_back
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<ii> vii;
typedef tuple<int, int, int> iii;
#ifndef DEBUG
#define cerr if (0) cerr
#endif
const int MAXN = 300005;
const int INF = 1000000005;
const ll LINF = 1000000000000000005;
const int MAXL = 20;
int n, m;
int s[MAXN];
set<ii> adj[MAXN];
bool vis[MAXN];
int ap[MAXN];
bool ans[MAXN];
ll sm[MAXN];
int p[MAXN], rnk[MAXN];
void init() {
REP (i, 1, n + 1) {
sm[i] = s[i];
p[i] = i;
rnk[i] = 1;
}
}
int findp(int i) {
if (p[i] == i) return i;
return p[i] = findp(p[i]);
}
bool join(int a, int b) {
int pa = findp(a), pb = findp(b);
if (pa == pb) return 0;
if (rnk[pa] < rnk[pb]) swap(pa, pb);
if (rnk[pa] == rnk[pb]) rnk[pa]++;
p[pb] = pa;
sm[pa] += sm[pb];
if (adj[pa].size() < adj[pb].size()) {
swap(adj[pa], adj[pb]);
}
for (ii v : adj[pb]) {
adj[pa].insert(v);
}
return 1;
}
int main() {
#ifndef DEBUG
ios::sync_with_stdio(0), cin.tie(0);
#endif
cin >> n >> m;
REP (i, 1, n + 1) {
cin >> s[i];
}
REP (i, 0, m) {
int u, v; cin >> u >> v;
adj[u].insert({s[v], v});
adj[v].insert({s[u], u});
}
init();
vi id(n, 0);
iota(ALL(id), 1);
sort(ALL(id), [&] (int l, int r) {
return s[l] < s[r];
});
for (int u : id) {
vis[u] = 1;
vi toj;
for (auto [w, v] : adj[u]) {
if (vis[v]) {
toj.pb(v);
}
}
cerr << u << '\n';
for (int v : toj) {
cerr << u << " <-> " << v << '\n';
join(u, v);
}
int fp = findp(u);
while (!adj[fp].empty() && vis[findp(adj[fp].begin() -> SE)]) {
adj[fp].erase(adj[fp].begin());
}
ap[u] = -1;
if (!adj[fp].empty() && sm[fp] >= adj[fp].begin() -> FI) {
ap[u] = adj[fp].begin() -> SE;
}
}
reverse(ALL(id));
ans[id[0]] = 1;
for (int u : id) {
cerr << u << ": " << ap[u] << '\n';
if (ap[u] == -1) continue;
ans[u] = ans[ap[u]];
}
REP (i, 1, n + 1) {
cout << ans[i];
}
cout << '\n';
return 0;
}
# | 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... |