結果

問題 No.977 アリス仕掛けの摩天楼
ユーザー ゆにぽけゆにぽけ
提出日時 2023-10-26 00:24:58
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 25 ms / 2,000 ms
コード長 1,976 bytes
コンパイル時間 1,527 ms
コンパイル使用メモリ 134,492 KB
実行使用メモリ 4,552 KB
最終ジャッジ日時 2023-10-26 00:25:01
合計ジャッジ時間 2,797 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 2 ms
4,348 KB
testcase_08 AC 2 ms
4,348 KB
testcase_09 AC 2 ms
4,348 KB
testcase_10 AC 2 ms
4,348 KB
testcase_11 AC 2 ms
4,348 KB
testcase_12 AC 2 ms
4,348 KB
testcase_13 AC 4 ms
4,348 KB
testcase_14 AC 4 ms
4,348 KB
testcase_15 AC 4 ms
4,348 KB
testcase_16 AC 4 ms
4,348 KB
testcase_17 AC 4 ms
4,348 KB
testcase_18 AC 9 ms
4,348 KB
testcase_19 AC 9 ms
4,348 KB
testcase_20 AC 12 ms
4,348 KB
testcase_21 AC 19 ms
4,348 KB
testcase_22 AC 23 ms
4,348 KB
testcase_23 AC 24 ms
4,552 KB
testcase_24 AC 24 ms
4,552 KB
testcase_25 AC 25 ms
4,552 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<vector>
#include<algorithm>
#include<cstring>
#include<cassert>
#include<cmath>
#include<ctime>
#include<iomanip>
#include<numeric>
#include<stack>
#include<queue>
#include<map>
#include<unordered_map>
#include<set>
#include<unordered_set>
#include<bitset>
#include<random>
#include<functional>
#include<utility>
using namespace std;

#include<algorithm>
#include<vector>
#include<cassert>
using namespace std;
struct UnionFind
{
	private:
	int n;
	vector<int> par,siz;

	public:
	UnionFind(int n) :n(n),par(n,-1),siz(n,1) {}

	int root(int u)
	{
		assert(0 <= u && u < n);
		return (par[u] < 0 ? u:par[u] = root(par[u]));
	}

	bool same(int u,int v)
	{
		assert(0 <= u && u < n && 0 <= v && v < n);
		return root(u) == root(v);
	}

	bool unite(int u,int v)
	{
		assert(0 <= u && u < n && 0 <= v && v < n);
		u = root(u),v = root(v);
		if(u == v) return false;

		if(siz[u] < siz[v]) swap(u,v);

		siz[u] += siz[v];
		par[v] = u;

		return true;
	}

	int size(int u)
	{
		assert(0 <= u && u < n);
		return siz[root(u)];
	}

	vector<vector<int>> components()
	{
		vector<vector<int>> ret(n);
		for(int u = 0;u < n;u++) ret[root(u)].push_back(u);

		ret.erase(remove_if(ret.begin(),ret.end(),[](vector<int> v) { return v.empty();}),ret.end());

		return ret;
	}
};
int N,deg[1 << 17];
void solve()
{
	cin >> N;
	UnionFind uf(N);
	for(int i = 1;i < N;i++)
	{
		int u,v;
		cin >> u >> v;
		uf.unite(u,v);
		deg[u]++,deg[v]++;
	}
	int count = 0;
	for(int i = 0;i < N;i++) if(uf.root(i) == i) count++;
	if(count >= 3) cout << "Alice" << endl;
	else if(count == 2)
	{
		int one = 0;
		for(int i = 0;i < N;i++) if(uf.size(i) == 1) one = 1;
		if(!one) cout << "Alice" << endl;
		else
		{
			int B = 1;
			for(int i = 0;i < N;i++) if(uf.size(i) > 1) B &= deg[i] == 2;
			cout << (B ? "Bob":"Alice") << endl;
		}
	}
	else cout << "Bob" << endl;
}
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	int tt = 1;
	//cin >> tt;
	while(tt--) solve();
}
0