結果
問題 | No.1424 Ultrapalindrome |
ユーザー | TrpFrog |
提出日時 | 2021-03-12 22:29:46 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 109 ms / 2,000 ms |
コード長 | 3,202 bytes |
コンパイル時間 | 2,048 ms |
コンパイル使用メモリ | 202,104 KB |
最終ジャッジ日時 | 2025-01-19 15:13:18 |
ジャッジサーバーID (参考情報) |
judge5 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 29 |
ソースコード
#include <bits/stdc++.h> using namespace std; #define pb push_back #define eb emplace_back #define unused [[maybe_unused]] #define tempT template<class T> #define ALL(obj) (obj).begin(), (obj).end() #define rALL(obj) (obj).rbegin(), (obj).rend() #define debug(x) cerr << #x << ": " << x << endl #define ans_exit(x) { cout << x << endl; exit(0); } #define ans_return(x) { cout << x << endl; return; } #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define r_rep(i, n) for (int i = (int)(n) - 1; i >= 0; i--) #define reps(i, n, m) for (ll i = (ll)(n); i < (ll)(m); i++) #define r_reps(i, n, m) for (ll i = (ll)(m) - 1; i >= (ll)(n); i--) #define debugarr(a) {cerr << #a << ":"; \ for(auto _e:a){cerr << " " <<_e;} cerr << endl;} using ll unused = long long; using ld unused = long double; using vl unused = vector<ll>; using vvl unused = vector<vl>; using vvvl unused = vector<vvl>; using lp unused = pair<ll, ll>; using lmap unused = map<int, int>; unused constexpr ll MOD = 1e9 + 7; unused constexpr ll INF = 1 << 29; unused constexpr ll LINF = 1LL << 61; unused constexpr int DX[8] = {0, 1, 0,-1, 1, 1,-1,-1}; unused constexpr int DY[8] = {1, 0,-1, 0, 1,-1, 1,-1}; unused inline bool bit(ll b, ll i) { return b & (1LL << i); } unused inline ll ceiv(ll a, ll b) { return (a + b - 1) / b; } unused inline ll mod(ll a, ll m = MOD) { return (a % m + m) % m; } unused inline void modadd(ll &a, ll b, ll m = MOD) { a = mod(a + b, m); } unused inline ll floordiv(ll a, ll b) { return a / b - (a < 0 && a % b); } unused inline ll ceildiv(ll a, ll b) { return floordiv(a + b - 1, b); } tempT unused inline bool in_range(T a, T x, T b) { return a <= x && x < b; } unused inline bool in_range(ll x, ll b) { return in_range(0LL, x, b); } tempT unused bool chmin(T &a, T b) { if(a > b) {a = b; return 1;} return 0; } tempT unused bool chmax(T &a, T b) { if(a < b) {a = b; return 1;} return 0; } int count_leafs(vector<set<int>> &graph) { int leafs = 0; rep(i, graph.size()) { if((int)graph[i].size() == 1) leafs++; } return leafs; } bool is_path(vector<set<int>> &graph) { return count_leafs(graph) == 2; } int find_center(vector<set<int>> &graph) { int leafs = count_leafs(graph); int center = INF; rep(i, graph.size()) { if((int)graph[i].size() == leafs) { if(center == INF) { center = i; } else { center = INF; break; } } } if(center == INF) ans_exit("No"); return center; } int depth(vector<set<int>> &graph, int i, int prev, int d) { for(int nxt : graph[i]) { if(nxt == prev) continue; return depth(graph, nxt, i, d + 1); } return d; } int main() { int n; cin >> n; vector<set<int>> graph(n); rep(i, n - 1) { int a, b; cin >> a >> b; a--; b--; graph[a].insert(b); graph[b].insert(a); } if(is_path(graph)) ans_exit("Yes"); int center = find_center(graph); set<int> d; for(int i : graph[center]) { d.insert(depth(graph, i, center, 0)); } cout << ((int)d.size() == 1 ? "Yes" : "No") << endl; }