結果
| 問題 |
No.1424 Ultrapalindrome
|
| コンテスト | |
| ユーザー |
rokahikou1
|
| 提出日時 | 2021-03-13 02:34:12 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 52 ms / 2,000 ms |
| コード長 | 7,184 bytes |
| コンパイル時間 | 2,057 ms |
| コンパイル使用メモリ | 185,772 KB |
| 実行使用メモリ | 22,784 KB |
| 最終ジャッジ日時 | 2024-10-14 16:30:45 |
| 合計ジャッジ時間 | 3,950 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 29 |
ソースコード
#include <bits/stdc++.h>
#define rep(i, n) for(int(i) = 0; (i) < (n); (i)++)
#define FOR(i, m, n) for(int(i) = (m); (i) < (n); (i)++)
#define ALL(v) (v).begin(), (v).end()
#define LLA(v) (v).rbegin(), (v).rend()
#define SZ(v) (int)(v).size()
#define INT(...) \
int __VA_ARGS__; \
read(__VA_ARGS__)
#define LL(...) \
ll __VA_ARGS__; \
read(__VA_ARGS__)
#define DOUBLE(...) \
double __VA_ARGS__; \
read(__VA_ARGS__)
#define CHAR(...) \
char __VA_ARGS__; \
read(__VA_ARGS__)
#define STRING(...) \
string __VA_ARGS__; \
read(__VA_ARGS__)
#define VEC(type, name, size) \
vector<type> name(size); \
read(name)
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using Graph = vector<vector<int>>;
template <typename T> struct edge {
int from, to;
T cost;
edge(int f, int t, T c) : from(f), to(t), cost(c) {}
};
template <typename T> using WGraph = vector<vector<edge<T>>>;
const int INF = 1 << 30;
const ll LINF = 1LL << 60;
const int MOD = 1e9 + 7;
const char newl = '\n';
template <class T> inline vector<T> make_vec(size_t a, T val) {
return vector<T>(a, val);
}
template <class... Ts> inline auto make_vec(size_t a, Ts... ts) {
return vector<decltype(make_vec(ts...))>(a, make_vec(ts...));
}
void read() {}
template <class T> inline void read(T &a) { cin >> a; }
template <class T, class S> inline void read(pair<T, S> &p) {
read(p.first), read(p.second);
}
template <class T> inline void read(vector<T> &v) {
for(auto &a : v)
read(a);
}
template <class Head, class... Tail>
inline void read(Head &head, Tail &...tail) {
read(head), read(tail...);
}
template <class T> void write(const T &a) { cout << a << '\n'; }
template <class T> void write(const vector<T> &a) {
for(int i = 0; i < a.size(); i++)
cout << a[i] << (i + 1 == a.size() ? '\n' : ' ');
}
template <class Head, class... Tail>
void write(const Head &head, const Tail &...tail) {
cout << head << ' ';
write(tail...);
}
template <class T> void writel(const T &a) { cout << a << '\n'; }
template <class T> void writel(const vector<T> &a) {
for(int i = 0; i < a.size(); i++)
cout << a[i] << '\n';
}
template <class Head, class... Tail>
void writel(const Head &head, const Tail &...tail) {
cout << head << '\n';
write(tail...);
}
template <class T> auto sum(const T &a) { return accumulate(ALL(a), T(0)); }
template <class T> auto min(const T &a) { return *min_element(ALL(a)); }
template <class T> auto max(const T &a) { return *max_element(ALL(a)); }
template <class T> inline void chmax(T &a, T b) { (a < b ? a = b : a); }
template <class T> inline void chmin(T &a, T b) { (a > b ? a = b : a); }
struct IO {
IO() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout << fixed << setprecision(10);
}
} io;
class HLDecomposition {
private:
Graph g; // グラフ(木)
int t;
vector<int> sz; // もとの木の部分木のサイズ
vector<int> par; // もとの木での親
vector<int>
idx; // vの新たなidx(行きがけ順) segtree等に乗せたときのidxに対応
vector<int> head; // 連結成分のうち最もidxが若い(浅い)頂点
vector<int> depth; // もとの木での深さ
public:
HLDecomposition(Graph &g, int root = 0)
: g(g), sz(g.size()), par(g.size()), idx(g.size()), head(g.size()),
depth(g.size()), t(0) {
dfs_sz(root, 0, -1);
dfs_hld(root, -1, 0);
}
int dfs_sz(int v, int d, int p) {
par[v] = p;
if(sz[v] != 0)
return sz[v];
sz[v] = 1;
depth[v] = d;
for(auto nv : g[v]) {
if(p == nv)
continue;
sz[v] += dfs_sz(nv, d + 1, v);
}
return sz[v];
}
void dfs_hld(int v, int p, int h) {
idx[v] = t++;
head[v] = h;
if(par[v] != -1 && g[v].size() == 1)
return;
int m = 0;
int nxt = -1;
for(auto nv : g[v]) {
if(nv == p)
continue;
if(sz[nv] > m) {
m = sz[nv];
nxt = nv;
}
}
dfs_hld(nxt, v, h);
for(auto nv : g[v]) {
if(p == nv)
continue;
if(nv != nxt)
dfs_hld(nv, v, nv);
}
}
vector<pii> query(int u, int v, bool is_edge = false) {
vector<pii> ret;
while(head[u] != head[v]) {
if(depth[head[u]] <= depth[head[v]]) {
ret.push_back({idx[head[v]], idx[v]});
v = par[head[v]];
} else {
ret.push_back({idx[head[u]], idx[u]});
u = par[head[u]];
}
}
if(!(is_edge && idx[u] == idx[v]))
ret.push_back({min(idx[u], idx[v]) + is_edge, max(idx[u], idx[v])});
return ret;
}
template <typename Monoid, typename Query, typename Function>
Monoid query(int u, int v, const Monoid &e, const Query &q,
const Function &f, bool is_edge = false) {
Monoid l = e, r = e;
for(;; v = par[head[v]]) {
if(idx[u] > idx[v]) {
swap(u, v), swap(l, r);
}
if(head[v] == head[u])
break;
l = f(q(idx[head[v]], idx[v] + 1), l);
}
return f(f(q(idx[u] + is_edge, idx[v] + 1), l), r);
}
int lca(int u, int v) {
for(;; v = par[head[v]]) {
if(idx[u] > idx[v])
swap(u, v);
if(head[u] == head[v])
return u;
}
}
int get_idx(int v) { return idx[v]; }
int get_depth(int v) { return depth[v]; }
int get_size(int v) { return sz[v]; }
};
int main() {
INT(n);
Graph g(n);
vector<int> in(n);
int cnt = 0;
rep(i, n - 1) {
INT(a, b);
a--, b--;
g[a].push_back(b);
g[b].push_back(a);
in[a]++, in[b]++;
}
int root = -1;
rep(i, n) {
if(in[i] > 2) {
root = i;
cnt++;
}
}
if(cnt == 0) {
write("Yes");
} else if(cnt >= 2)
write("No");
else {
HLDecomposition hld(g, root);
unordered_set<int> se;
rep(i, n) {
if(g[i].size() == 1) {
se.insert(hld.get_depth(i));
}
}
write((se.size() == 1 ? "Yes" : "No"));
}
}
rokahikou1