20240918

libohan0518發表於2024-10-01

Card Scoring

這題當 \(k = 3\) 時還無法解決,但是 \(k = 2\)\(k = 4\)\(k = 2\)時可以直接用字首和和 \(dp\) 解決,而 \(k = 4\) 時可以用李超線段樹

Marshmallow Molecules

這題直接啟發式合併

#include <bits/stdc++.h>

using namespace std;

#define int long long

const int N = 1e5 + 5;

int n, m, ans;

set<int> s[N];

signed main() {
  cin >> n >> m;
  for (int i = 1, u, v; i <= m; i++) {
    cin >> u >> v;
    s[u].insert(v);
  }
  for (int i = 1; i <= n; i++) {
    if (!s[i].size()) {
      continue;
    }
    ans += s[i].size();
    int x = *s[i].begin();
    s[i].erase(s[i].begin());
    if (s[i].size() > s[x].size()) {
      swap(s[i], s[x]);
    }
    while (!s[i].empty()) {
      s[x].insert(*s[i].begin());
      s[i].erase(s[i].begin());
    }
  }
  cout << ans;
  return 0;
}

然\

相關文章