結果
問題 | No.2418 情報通だよ!Nafmoくん |
ユーザー | FplusFplusF |
提出日時 | 2023-08-12 13:43:09 |
言語 | C++17(gcc12) (gcc 12.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 24 ms / 2,000 ms |
コード長 | 1,611 bytes |
コンパイル時間 | 2,020 ms |
コンパイル使用メモリ | 204,448 KB |
実行使用メモリ | 6,820 KB |
最終ジャッジ日時 | 2024-11-14 12:20:57 |
合計ジャッジ時間 | 3,173 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 21 |
ソースコード
#include <bits/stdc++.h> using namespace std; using ll=long long; using pll=pair<ll,ll>; using tll=tuple<ll,ll,ll>; using ld=long double; const ll INF=(1ll<<60); #define rep(i,n) for (ll i=0;i<(ll)(n);i++) #define all(v) v.begin(),v.end() template<class T> void chmin(T &a,T b){ if(a>b){ a=b; } } template<class T> void chmax(T &a,T b){ if(a<b){ a=b; } } struct union_find{ int n; vector<int> par,sz; union_find(int x):par(x),sz(x,1){ n=x; for(int i=0;i<n;i++){ par[i]=i; } } int root(int x){ if(par[x]==x) return x; return par[x]=root(par[x]); } void unite(int x,int y){ int rx=root(x); int ry=root(y); if(rx==ry) return; if(sz[rx]<sz[ry]) swap(rx,ry); par[ry]=rx; sz[rx]+=sz[ry]; } bool same(int x,int y){ int rx=root(x); int ry=root(y); return rx==ry; } int size(int x){ return sz[root(x)]; } vector<int> all_size(){ vector<int> cnt(n,0); for(int i=0;i<n;i++){ cnt[root(i)]++; } vector<int> ret; for(int i=0;i<n;i++){ if(0<cnt[i]) ret.push_back(cnt[i]); } return ret; } }; int main(){ ios::sync_with_stdio(false); cin.tie(nullptr); ll n,m; cin >> n >> m; union_find uf(2*n); while(m--){ ll a,b; cin >> a >> b; a--; b--; uf.unite(a,b); } vector<int> v=uf.all_size(); ll ans=n; for(auto &i:v) ans-=i/2; cout << ans << '\n'; }