結果
| 問題 |
No.2418 情報通だよ!Nafmoくん
|
| コンテスト | |
| ユーザー |
shop_one
|
| 提出日時 | 2023-08-12 13:51:38 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 42 ms / 2,000 ms |
| コード長 | 1,526 bytes |
| コンパイル時間 | 1,186 ms |
| コンパイル使用メモリ | 111,352 KB |
| 最終ジャッジ日時 | 2025-02-16 03:47:53 |
|
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 21 |
ソースコード
// I SELL YOU...!
#include<iostream>
#include<vector>
#include<algorithm>
#include<functional>
#include<queue>
#include<chrono>
#include<iomanip>
#include<map>
#include<set>
using namespace std;
using ll = long long;
using P = pair<ll,ll>;
using TP = tuple<ll,ll,ll>;
void init_io(){
cin.tie(0);
ios::sync_with_stdio(false);
cout << fixed << setprecision(18);
}
class union_find{
int n;
vector<int> par,rk,size;
public:
union_find(int max_n){
n = max_n+1;
for(int i=0;i<=n;i++){
par.emplace_back(i);
rk.emplace_back(0);
size.emplace_back(1);
}
}
int root(int x){
vector<int> nodes;
while(x!=par[x]){
nodes.emplace_back(x);
x = par[x];
}
for(auto v:nodes){
par[v] = x;
}
return x;
}
void unite(int x,int y){
x = root(x);
y = root(y);
if(x==y) return;
if(rk[x]<rk[y]){
par[x] = y;
size[y] += size[x];
}else{
par[y] = x;
if(rk[x]==rk[y]) rk[x]++;
size[x] += size[y];
}
}
bool same(int x,int y){
return root(x)==root(y);
}
int treesize(int x){
return size[root(x)];
}
};
signed main(){
init_io();
ll n,m;
cin >> n >> m;
vector<ll> a(m), b(m);
vector<ll> used(2*n, false);
union_find uf(2*n);
for (int i=0;i<m;i++) {
cin >> a[i] >> b[i];
uf.unite(a[i]-1, b[i]-1);
}
ll ans = 0;
for (int i=0;i<2*n;i++) {
ll r = uf.root(i);
if (used[r]) continue;
used[r] = true;
ans += uf.treesize(r) % 2;
}
cout << ans/2 << endl;
}
shop_one