제출 #635767

#제출 시각아이디문제언어결과실행 시간메모리
635767null_aweStranded Far From Home (BOI22_island)C++14
100 / 100
502 ms42576 KiB
#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<vector<int>> w;
vector<ll> vals;
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> arr(n); for (int i = 0; i < n; ++i) cin >> arr[i];
  set<int> nums; for (int num : arr) nums.insert(num);
  vector<int> copy(nums.begin(), nums.end());
  vector<vector<pii>> edges(copy.size());
  for (int i = 0; i < m; ++i) {
    int a, b; cin >> a >> b; --a, --b;
    edges[lower_bound(copy.begin(), copy.end(), max(arr[a], arr[b])) - copy.begin()].push_back({a, b});
  }
  w.resize(n), vals.resize(n), r.resize(n), p.resize(n);
  for (int i = 0; i < n; ++i) w[i].push_back(i), vals[i] = arr[i], p[i] = i;
  for (int i = 0; i < copy.size(); ++i) {
    for (pii edge : edges[i]) {
      int a = edge.first, b = edge.second;
      int pa = find(a), pb = find(b);
      if (pa == pb) continue;
      link(a, b);
      int np = find(a);
      if (vals[pa] < arr[b]) swap(w[np], w[pb]);
      else if (vals[pb] < arr[a]) swap(w[np], w[pa]);
      else {
        if (w[pa].size() < w[pb].size()) swap(w[pa], w[pb]);
        for (int v : w[pb]) w[pa].push_back(v);
        swap(w[np], w[pa]);
      }
      vals[np] = vals[pa] + vals[pb];
    }
  }
  vector<bool> ans(n);
  for (int v : w[find(0)]) ans[v] = true;
  for (int num : ans) cout << num;
  cout << '\n';
  /*
  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], n - i};
  sort(arr.begin(), arr.end()), reverse(arr.begin(), arr.end());
  for (int i = 0; i < n; ++i) arr[i].second = n - arr[i].second;
  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;
  for (int i = 1; i < n; ++i) {
    for (int u : adj[arr[i].second]) {
      if (io[u] > i || nd[u] == INF || nodes[u] > sizes[arr[i].second]) continue;
      nd[arr[i].second] = 0;
    }
  }
  queue<pii> q; q.push({arr[0].second, -1});
  while (q.size()) {
    int v = q.front().first, from = q.front().second, rn = max(nodes[v], nd[v]); q.pop();
    cout << v << ' ' << nd[v] << '\n';
    for (int u : adj[v]) {
      if (u == from) continue;
      ll need;
      if (nodes[u] >= nodes[v]) need = max(nd[v] - sizes[u] + sizes[v], 0LL);
      else need = max(max(nodes[v] - sizes[u], (ll) nd[v]), 0LL);
      if (need < nd[u]) {
        nd[u] = need;
        q.push({u, v});
      }
    }
  }
  //for (int num : nd) cout << num << '\n';
  for (int i = 0; i < n; ++i) cout << (nd[i] == 0);
  cout << '\n';
  */
  //for (ll sz : sizes) cout << sz << ' ';
  //cout << '\n';
  return 0;
}

컴파일 시 표준 에러 (stderr) 메시지

island.cpp: In function 'int main()':
island.cpp:39:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   39 |   for (int i = 0; i < copy.size(); ++i) {
      |                   ~~^~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...