結果
| 問題 |
No.2403 "Eight" Bridges of Königsberg
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2023-08-04 23:38:35 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 2,039 bytes |
| コンパイル時間 | 1,956 ms |
| コンパイル使用メモリ | 211,240 KB |
| 最終ジャッジ日時 | 2025-02-15 23:19:23 |
|
ジャッジサーバーID (参考情報) |
judge4 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 20 WA * 11 |
ソースコード
typedef long long ll;
typedef long double ld;
#include <bits/stdc++.h>
using namespace std;
#define int long long
// Union-Find
struct UnionFind {
// core member
vector<int> par;
// constructor
UnionFind() { }
UnionFind(int n) : par(n, -1) { }
void init(int n) { par.assign(n, -1); }
// core methods
int root(int x) {
if (par[x] < 0) return x;
else return par[x] = root(par[x]);
}
bool same(int x, int y) {
return root(x) == root(y);
}
bool merge(int x, int y) {
x = root(x), y = root(y);
if (x == y) return false;
if (par[x] > par[y]) swap(x, y); // merge technique
par[x] += par[y];
par[y] = x;
return true;
}
int size(int x) {
return -par[root(x)];
}
// debug
friend ostream& operator << (ostream &s, UnionFind uf) {
map<int, vector<int>> groups;
for (int i = 0; i < uf.par.size(); ++i) {
int r = uf.root(i);
groups[r].push_back(i);
}
for (const auto &it : groups) {
s << "group: ";
for (auto v : it.second) s << v << " ";
s << endl;
}
return s;
}
};
signed main(){
ll n,m;
std::cin >> n>>m;
vector<ll> nyu(n),shu(n);
UnionFind uf(n);
vector<ll> jiko(n);
for (int i = 0; i < m; i++) {
ll u,v;
std::cin >> u>>v;
u--;v--;
uf.merge(u,v);
shu[u]++;
nyu[v]++;
if(u==v){
jiko[u]++;
}
}
set<ll> kan;
map<ll,ll> ma;
ll cnt = 0;
set<ll> uni;
for (int i = 0; i < n; i++) {
uni.insert(uf.root(i));
if(shu[i]-nyu[i]>0){
ma[uf.root(i)] += shu[i]-nyu[i];
}
if(shu[i]==0&&nyu[i]==0)cnt++;
}
ll ans = 0;
for (auto e : ma) {
ans += e.second-1;
}
ans += ma.size()-1;
ans += max(0ll,(ll)uni.size()-(ll)ma.size()-cnt-1);
std::cout << ans << std::endl;
};