結果
問題 |
No.2403 "Eight" Bridges of Königsberg
|
ユーザー |
|
提出日時 | 2023-08-04 23:49:39 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 91 ms / 2,000 ms |
コード長 | 1,985 bytes |
コンパイル時間 | 2,091 ms |
コンパイル使用メモリ | 206,676 KB |
最終ジャッジ日時 | 2025-02-15 23:20:59 |
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 31 |
ソースコード
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]++; } } ll p = 0; set<ll> uni; set<ll> pr; ll cnt = 0; for (int i = 0; i < n; i++) { if(shu[i]-nyu[i]>0){ p += shu[i]-nyu[i]; uni.insert(uf.root(i)); pr.insert(uf.root(i)); } if(shu[i]==nyu[i]&&shu[i]!=0){ uni.insert(uf.root(i)); } } std::cout << max(0ll,p-(ll)pr.size())+uni.size()-1 << std::endl; };