結果

問題 No.763 Noelちゃんと木遊び
ユーザー tanimani364tanimani364
提出日時 2020-06-30 14:08:29
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,982 bytes
コンパイル時間 2,447 ms
コンパイル使用メモリ 216,636 KB
実行使用メモリ 14,104 KB
最終ジャッジ日時 2023-10-01 01:20:28
合計ジャッジ時間 5,518 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 39 ms
14,104 KB
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i, a) for (int i = (int)0; i < (int)a; ++i)
#define rrep(i, a) for (int i = (int)a - 1; i >= 0; --i)
#define REP(i, a, b) for (int i = (int)a; i < (int)b; ++i)
#define RREP(i, a, b) for (int i = (int)a - 1; i >= b; --i)
#define pb push_back
#define eb emplace_back
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define popcount __builtin_popcount
using ll = long long;
constexpr ll mod = 1e9 + 7;
constexpr ll INF = 1LL << 60;

template <class T>
inline bool chmin(T &a, T b)
{
	if (a > b)
	{
		a = b;
		return true;
	}
	return false;
}
template <class T>
inline bool chmax(T &a, T b)
{
	if (a < b)
	{
		a = b;
		return true;
	}
	return false;
}

ll gcd(ll n, ll m)
{
	ll tmp;
	while (m != 0)
	{
		tmp = n % m;
		n = m;
		m = tmp;
	}
	return n;
}

ll lcm(ll n, ll m)
{
	return abs(n) / gcd(n, m) * abs(m); //gl=xy
}

using namespace std;

 struct UF {
	vector<int>par;//親
	vector<int>Rank;//木の深さ
    vector<int>sz;//要素数

    UF(int n){init(n);}

	//初期化
	void init(int n) {
		par.resize(n); Rank.resize(n);sz.resize(n);
		for (int i = 0; i < n; i++) {
			par[i]= i;
			Rank[i] = 0;
            sz[i]=1;
		}
	}

	//木の根を求める
	int find(int x) {
		if (par[x] == x) {
			return x;
		}
		else {
			return par[x] = find(par[x]);
		}
	}

	//xとyの属する集合を併合
	bool unite(int x, int y) {
		x = find(x);
		y = find(y);
		if (x == y)return false;//すでに同じ集合に属している

		if (Rank[x] < Rank[y]) {//高さが小さい方を下に
			par[x] = y;
            sz[y]+=sz[x];
		}
		else {
			par[y] = x;
			if (Rank[x] == Rank[y])Rank[x]++;//同じだとrankが変わらないままになるので増やす(それ以外は大きい方のrankが採用される)
            sz[x]+=sz[y];
		}
		return true;
	}

	//xとyが同じ集合に属するかを判定
	bool same(int x, int y) {
		return find(x) == find(y);
	}

    int size(int k){//要素数を求める
        return sz[find(k)];
    }
};

void solve()
{
	int n;
	cin>>n;
	vector<vector<int>>g(n),g2(n);
	vector<int>u(n-1),v(n-1);
	vector<pair<int,int>>cnt(n);
	rep(i,n-1){
		cin>>u[i]>>v[i];
		u[i]--;v[i]--;
		cnt[u[i]].first++;
		cnt[v[i]].first++;
		g[u[i]].eb(v[i]);
		g[v[i]].eb(u[i]);
	}
	rep(i,n)cnt[i].second=i;
	sort(rall(cnt));
	rep(i,n){
		if(cnt[i].first>=3){
			for(auto x:g[cnt[i].second]){
				cnt[x].first--;
			}
			cnt[i].first=-1;
		}
	}
	vector<int>cnt2(n);
	rep(i,n){
		cnt2[cnt[i].second]=cnt[i].first;
	}
	UF tree(n);
	rep(i,n-1){
		if(cnt2[u[i]]<=-1||cnt2[v[i]]<=-1)continue;
		if(cnt2[u[i]]<=2&&cnt2[v[i]]<=2){
			tree.unite(u[i],v[i]);
		}
	}
	int ans=0;
	vector<bool>used(n);
	rep(i,n){
		if(cnt2[i]<=-1)continue;
		if(cnt2[i]<=2){
			if(used[tree.find(i)])continue;
			ans+=(tree.size(tree.find(i))+1)/2;
			used[tree.find(i)]=true;
		}
	}
	cout<<ans<<"\n";
}

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout << fixed << setprecision(15);
	solve();
	return 0;
}
0