結果
| 問題 |
No.3024 全単射的
|
| コンテスト | |
| ユーザー |
Kude
|
| 提出日時 | 2025-02-14 22:49:44 |
| 言語 | C++23 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 199 ms / 5,000 ms |
| コード長 | 2,777 bytes |
| コンパイル時間 | 4,028 ms |
| コンパイル使用メモリ | 315,936 KB |
| 実行使用メモリ | 23,716 KB |
| 最終ジャッジ日時 | 2025-02-14 22:49:52 |
| 合計ジャッジ時間 | 6,262 ms |
|
ジャッジサーバーID (参考情報) |
judge6 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 22 |
ソースコード
#include<bits/stdc++.h>
namespace {
#pragma GCC diagnostic ignored "-Wunused-function"
#include<atcoder/all>
#pragma GCC diagnostic warning "-Wunused-function"
using namespace std;
using namespace atcoder;
#define rep(i,n) for(int i = 0; i < (int)(n); i++)
#define rrep(i,n) for(int i = (int)(n) - 1; i >= 0; i--)
#define all(x) begin(x), end(x)
#define rall(x) rbegin(x), rend(x)
template<class T> bool chmax(T& a, const T& b) { if (a < b) { a = b; return true; } else return false; }
template<class T> bool chmin(T& a, const T& b) { if (b < a) { a = b; return true; } else return false; }
using ll = long long;
using P = pair<int,int>;
using VI = vector<int>;
using VVI = vector<VI>;
using VL = vector<ll>;
using VVL = vector<VL>;
template <class S>
struct value_compression : vector<S> {
bool built = false;
using VS = vector<S>;
using VS::VS;
value_compression(vector<S> v) : vector<S>(move(v)) {}
void build() {
sort(this->begin(), this->end());
this->erase(unique(this->begin(), this->end()), this->end());
built = true;
}
template <class T>
void convert(T first, T last) {
assert(built);
for (; first != last; ++first) *first = (*this)(*first);
}
int operator()(S x) {
assert(built);
return lower_bound(this->begin(), this->end(), x) - this->begin();
}
void clear() {
this->clear();
built = false;
}
};
} int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n;
ll m;
cin >> n >> m;
VL x(n), y(n);
value_compression<ll> vc;
rep(i, n) cin >> x[i] >> y[i], vc.emplace_back(x[i]), vc.emplace_back(y[i]);
vc.build();
m = n;
n = vc.size();
// int n, m;
// cin >> n >> m;
set<P> es;
vector<char> alive(n, true);
int ans = 0;
rep(i, m) {
int u = vc(x[i]), v = vc(y[i]);
if (u > v) swap(u, v);
if (u == v) {
ans += alive[u];
alive[u] = false;
} else if (es.count({u, v})) {
es.erase({u, v});
ans += alive[u] + alive[v];
alive[u] = alive[v] = false;
} else {
es.emplace(u, v);
}
}
VVI to(n);
for (auto [u, v] : es) {
if (alive[u] && alive[v]) {
to[u].emplace_back(v);
to[v].emplace_back(u);
} else if (alive[u] || alive[v]) {
if (alive[v]) swap(u, v);
auto dfs = [&](auto&& self, int u) -> void {
if (!alive[u]) return;
ans++;
alive[u] = false;
for (int v : to[u]) self(self, v);
};
dfs(dfs, u);
}
}
dsu uf(n);
VI ecnt(n);
rep(u, n) if (alive[u]) for (int v : to[u]) if (u < v && alive[v]) {
int lu = uf.leader(u), lv = uf.leader(v);
if (lu == lv) ecnt[lu]++;
else ecnt[uf.merge(lu, lv)] = ecnt[lu] + ecnt[lv] + 1;
}
rep(i, n) if (uf.leader(i) == i) ans += min(ecnt[i], uf.size(i));
cout << ans << '\n';
}
Kude