結果
| 問題 |
No.977 アリス仕掛けの摩天楼
|
| ユーザー |
Shun Takase
|
| 提出日時 | 2020-02-01 02:59:17 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 150 ms / 2,000 ms |
| コード長 | 4,497 bytes |
| コンパイル時間 | 2,424 ms |
| コンパイル使用メモリ | 196,008 KB |
| 実行使用メモリ | 22,428 KB |
| 最終ジャッジ日時 | 2024-09-18 18:23:56 |
| 合計ジャッジ時間 | 4,372 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 26 |
ソースコード
#include <bits/stdc++.h>
#define PI 3.14159265358979323846
#define MAXINF (1e18L)
#define INF (1e9L)
#define EPS (1e-9)
#define MOD ((ll)(1e9+7))
#define REP(i, n) for(int i=0;i<int(n);++i)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define RREP(i, n) for(int i=int(n)-1;i>=0;--i)
#define ALL(v) v.begin(),v.end()
#define FIND(v,x) (binary_search(ALL(v),(x)))
#define SORT(v) sort(ALL(v))
#define RSORT(v) sort(ALL(v));reverse(ALL(v))
#define DEBUG(x) cerr<<#x<<": "<<x<<endl;
#define DEBUG_VEC(v) cerr<<#v<<":";for(int i=0;i<v.size();i++) cerr<<" "<<v[i]; cerr<<endl
#define Yes(n) cout<<((n)?"Yes":"No")<<endl
#define YES(n) cout<<((n)?"YES":"NO")<<endl
#define pb push_back
#define fi first
#define se second
using namespace std;
template<class A>void pr(A a){cout << (a) << endl;}
template<class A,class B>void pr(A a,B b){cout << a << " " ;pr(b);}
template<class A,class B,class C>void pr(A a,B b,C c){cout << a << " " ;pr(b,c);}
template<class A,class B,class C,class D>void pr(A a,B b,C c,D d){cout << a << " " ;pr(b,c,d);}
template<class T> inline bool chmin(T& a, T b){return a>b ? a=b, true : false;}
template<class T> inline bool chmax(T& a, T b){return a<b ? a=b, true : false;}
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll,ll> pll;
struct union_find {
vector<int> parent; //親の番号を格納する。親だった場合は-(その集合のサイズ)
union_find(int N) {
parent = vector<int>(N, -1); //parentの値を-1にする(すべてバラバラ)
}
int root(int A) { //Aがどのグループに属しているか調べる
if (parent[A] < 0) return A;
return parent[A] = root(parent[A]);
}
int size(int A) { //自分のいるグループの頂点数を調べる
return -parent[root(A)];//親をとってきたい
}
bool connect(int A, int B) { //AとBをくっ付ける.すでにつながっていた場合false
//AとBを直接つなぐのではなく、root(A)にroot(B)をくっつける
A = root(A);
B = root(B);
if (A == B) return false; //すでにくっついてるからくっ付けない
if (size(A) < size(B)) swap(A, B); //大小が逆だったらひっくり返しちゃう。
parent[A] += parent[B]; //Aのサイズを更新する
parent[B] = A; //Bの親をAに変更する
return true;
}
};
// Low Linkを計算して橋と関節点を見つける
// ref : http://kagamiz.hatenablog.com/entry/2013/10/05/005213
struct low_link
{
int n, m;
vector<vector<int>> G;
vector<pair<int, int>> bridge;
vector<int> articulation;
vector<int> ord, low;
vector<bool> vis;
low_link(vector<vector<int>> G, int n, int m) : G(G), n(n), m(m)
{
ord.resize(n);
low.resize(n);
vis.resize(n);
int k = 0;
REP(i, n){
if(!vis[i]) dfs(i, -1, k);
}
SORT(bridge);
SORT(articulation);
}
vector<pair<int, int>> get_bridge(){
return bridge;
}
vector<int> get_articulation(){
return articulation;
}
private:
void dfs(int v, int p, int &k)
{
vis[v] = true;
ord[v] = k++;
low[v] = ord[v];
bool isArticulation = false;
int ct = 0;
// for (int i = 0; i < G[v].size(); i++){
for (auto&& c : G[v]){
if (!vis[c]){
ct++;
dfs(c, v, k);
low[v] = min(low[v], low[c]);
if (~p && ord[v] <= low[c]) isArticulation = true;
if (ord[v] < low[c]) bridge.push_back(make_pair(min(v, c), max(v, c)));
}
else if (c != p){
low[v] = min(low[v], ord[c]);
}
}
if (p == -1 && ct > 1) isArticulation = true;
if (isArticulation) articulation.push_back(v);
}
};
int main(void)
{
int N;
cin >> N;
union_find uf(N);
vector<vector<int>> G(N);
REP(i, N-1){
int a,b;
cin >> a >> b;
uf.connect(a,b);
G[a].pb(b);
G[b].pb(a);
}
low_link low(G, N, N-1);
map<int, int> m;
REP(i, N){
m[(uf.root(i)<0?i:uf.root(i))]++;
}
if(m.size()==1){
pr("Bob");
}else if(m.size()==2){
auto b = low.get_bridge();
if(b.size() == 0){
pr("Bob");
}else{
pr("Alice");
}
}else{
pr("Alice");
}
}
Shun Takase