답안 #635702

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
635702 2022-08-26T17:24:53 Z null_awe Stranded Far From Home (BOI22_island) C++14
0 / 100
405 ms 24516 KB
#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
#include <queue>
using namespace std;

#define ll long long
#define pii pair<int, int>

const int INF = 1e9;

vector<int> r, p;

int find(int a) { return a == p[a] ? a : p[a] = find(p[a]); }

void link(int a, int b) {
  a = find(a), b = find(b);
  if (a == b) return;
  if (r[a] < r[b]) swap(a, b);
  if (r[a] == r[b]) ++r[a];
  p[b] = a;
}

int main() {
  int n, m; cin >> n >> m;
  vector<int> nodes(n); for (int i = 0; i < n; ++i) cin >> nodes[i];
  vector<pii> arr(n); for (int i = 0; i < n; ++i) arr[i] = {nodes[i], i};
  sort(arr.begin(), arr.end()), reverse(arr.begin(), arr.end());
  vector<int> io(n); for (int i = 0; i < n; ++i) io[arr[i].second] = i;
  vector<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);
  }
  r.resize(n), p.resize(n); for (int i = 0; i < n; ++i) p[i] = i;
  vector<ll> sizes(n), psizes(n);
  for (int i = n - 1; i >= 0; --i) {
    int v = arr[i].second;
    sizes[v] = nodes[v];
    bool bs = false;
    set<int> done;
    for (int u : adj[v]) {
      if (io[u] < io[v] && nodes[u] == nodes[v]) bs = true;
      if (io[u] < io[v] || done.count(find(u))) continue;
      sizes[v] += psizes[find(u)], done.insert(find(u));
    }
    if (!bs) for (int u : adj[v]) if (nodes[u] == nodes[v]) sizes[u] = sizes[v];
    for (int u : adj[v]) if (io[u] > io[v]) link(u, v);
    psizes[find(v)] = sizes[v];
  }
  vector<int> nd(n, INF); nd[arr[0].second] = 0;
  queue<int> q; q.push(arr[0].second);
  while (q.size()) {
    int v = q.front(), rn = nodes[v] + nd[v]; q.pop();
    for (int u : adj[v]) {
      ll need = max(rn - sizes[u], 0LL);
      if (need < nd[u]) {
        nd[u] = need;
        q.push(u);
      }
    }
  }
  for (int i = 0; i < n; ++i) cout << (nd[i] == 0);
  cout << '\n';
  //for (ll sz : sizes) cout << sz << ' ';
  //cout << '\n';
  return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Incorrect 3 ms 468 KB Output isn't correct
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 300 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Incorrect 310 ms 24516 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Incorrect 405 ms 24296 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Incorrect 392 ms 24440 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Incorrect 3 ms 468 KB Output isn't correct
5 Halted 0 ms 0 KB -