結果

問題 No.1424 Ultrapalindrome
ユーザー rokahikou1rokahikou1
提出日時 2021-03-13 02:34:12
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 60 ms / 2,000 ms
コード長 7,184 bytes
コンパイル時間 2,326 ms
コンパイル使用メモリ 181,272 KB
実行使用メモリ 22,656 KB
最終ジャッジ日時 2024-04-22 17:19:45
合計ジャッジ時間 4,410 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 3 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 37 ms
7,808 KB
testcase_10 AC 38 ms
7,808 KB
testcase_11 AC 24 ms
6,528 KB
testcase_12 AC 35 ms
7,552 KB
testcase_13 AC 10 ms
5,376 KB
testcase_14 AC 27 ms
6,912 KB
testcase_15 AC 2 ms
5,376 KB
testcase_16 AC 21 ms
6,016 KB
testcase_17 AC 25 ms
6,656 KB
testcase_18 AC 25 ms
8,448 KB
testcase_19 AC 42 ms
10,880 KB
testcase_20 AC 10 ms
5,376 KB
testcase_21 AC 33 ms
9,472 KB
testcase_22 AC 20 ms
7,040 KB
testcase_23 AC 5 ms
5,376 KB
testcase_24 AC 9 ms
5,376 KB
testcase_25 AC 8 ms
5,376 KB
testcase_26 AC 60 ms
12,288 KB
testcase_27 AC 52 ms
9,344 KB
testcase_28 AC 33 ms
9,088 KB
testcase_29 AC 53 ms
22,656 KB
testcase_30 AC 41 ms
17,172 KB
testcase_31 AC 45 ms
17,296 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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"));
    }
}
0