結果

問題 No.1424 Ultrapalindrome
ユーザー rym903rym903
提出日時 2021-03-17 22:00:10
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 93 ms / 2,000 ms
コード長 4,645 bytes
コンパイル時間 1,605 ms
コンパイル使用メモリ 131,000 KB
実行使用メモリ 11,196 KB
最終ジャッジ日時 2024-04-27 14:11:06
合計ジャッジ時間 3,818 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 2 ms
6,940 KB
testcase_04 AC 2 ms
6,940 KB
testcase_05 AC 2 ms
6,940 KB
testcase_06 AC 2 ms
6,940 KB
testcase_07 AC 2 ms
6,944 KB
testcase_08 AC 2 ms
6,940 KB
testcase_09 AC 78 ms
8,704 KB
testcase_10 AC 81 ms
8,832 KB
testcase_11 AC 53 ms
7,296 KB
testcase_12 AC 67 ms
8,448 KB
testcase_13 AC 18 ms
6,940 KB
testcase_14 AC 53 ms
7,552 KB
testcase_15 AC 2 ms
6,944 KB
testcase_16 AC 41 ms
6,944 KB
testcase_17 AC 51 ms
7,296 KB
testcase_18 AC 33 ms
6,940 KB
testcase_19 AC 52 ms
7,168 KB
testcase_20 AC 12 ms
6,944 KB
testcase_21 AC 43 ms
6,944 KB
testcase_22 AC 25 ms
6,940 KB
testcase_23 AC 6 ms
6,940 KB
testcase_24 AC 11 ms
6,940 KB
testcase_25 AC 10 ms
6,940 KB
testcase_26 AC 63 ms
7,808 KB
testcase_27 AC 93 ms
10,496 KB
testcase_28 AC 71 ms
9,088 KB
testcase_29 AC 73 ms
9,984 KB
testcase_30 AC 59 ms
11,072 KB
testcase_31 AC 69 ms
11,196 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<algorithm>
#include<bitset>
#include<cmath>
#include<complex>
#include<deque>
#include<functional>
#include<iomanip>
#include<iostream>
#include<iterator>
#include<map>
#include<numeric>
#include<queue>
#include<set>
#include<stack>
#include<string>
#include<unordered_map>
#include<unordered_set>
#include<utility>
#include<vector>
#include<chrono>
using namespace std;
#define rep(i,n) for(int i=0; i<(n); i++)
#define FOR(i,x,n) for(int i=x; i<(n); i++)
#define vint(a,n) vint a(n); rep(i, n) cin >> a[i];
#define vll(a,n) vll a(n); rep(i, n) cin >> a[i];
#define ALL(n) begin(n),end(n)
#define RALL(n) rbegin(n),rend(n)
#define MOD (1000000007)
// #define MOD (998244353)
#define INF (1e9+7)
#define INFL (2e18)

typedef long long ll;
typedef unsigned int ui;
typedef unsigned long long ull;
using vint=vector<int>;
using vll=vector<ll>;
using vbool=vector<bool>;
using P=pair<ll,ll>;
template<class T>using arr=vector<vector<T>>;
template<class T>int popcount(T &a){int c=0; rep(i, 8*(int)sizeof(a)){if((a>>i)&1) c++;} return c;}
template<class T>void pl(T x){cout << x << " ";}
template<class T>void pr(T x){cout << x << endl;}
template <class T, class... Ts> inline void pr(T Tar, Ts... ts) { std::cout << Tar << " "; pr(ts...); return; }
template<class T>void prvec(vector<T>& a){rep(i, (int)a.size()-1){pl(a[i]);} pr(a.back());}
template<class T>void prarr(arr<T>& a){rep(i, (int)a.size()) if(a[i].empty()) pr(""); else prvec(a[i]);}
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
template<typename A, size_t N, typename T> void Fill(A (&array)[N], const T &val){fill( (T*)array, (T*)(array+N), val );}
// dijkstra法。ただし、全ての枝の距離が1のとき、BFS
void dijkstra(int st, arr<P>& G, vll& dist, vbool& seen, bool is_all_one=false){
    int V = G.size();
    priority_queue<P, vector<P>, greater<P>> min_heap;
    min_heap.push({0, st});
    dist.assign(V, INFL);
    seen.assign(V, false);
    dist[st] = 0;

    // 全ての枝の距離が1なら、単純なbfsで求められる。
    if(is_all_one){
        queue<int> que;
        que.push(st);
        seen[st] = true;
        while(!que.empty()){
            int now = que.front(); que.pop();
            ll d = dist[now];
            for(auto p: G[now]){
                int nx = p.second;
                if(seen[nx]) continue;
                seen[nx] = true;
                dist[nx] = d + 1;
                que.push(nx);
            }
        }
        return;
    }
 
    int frm;
    ll d;
    while(!min_heap.empty()){
        tie(d, frm) = min_heap.top(); min_heap.pop();
        if(seen[frm]) continue;
 
        // printf("node=%d", frm);
        seen[frm] = true;
        dist[frm] = d;
 
        for(P e: G[frm]){
            if(dist[e.second] > d+e.first)
                min_heap.push({d+e.first, e.second});
                // printf("(%d, %d) pushed", d+e.first, e.second);
        }
    }
}
int main()
{
    int n; cin >> n;
    arr<int> g(n);
    vint in(n, 0);
    int st;
    rep(i, n-1){
        int a, b; cin >> a >> b;
        a--; b--;
        g[a].push_back(b);
        g[b].push_back(a);
        in[a]++;
        in[b]++;
    }
    vint v;
    rep(i, n) {
        if(in[i]==1) v.push_back(i);
    }
    if(v.size()==2){
        pr("Yes");
        return 0;
    }
    st = v[0];

    vint par(n, -1);
    queue<int> que;
    que.push(st);

    int dist = 0;
    while(!que.empty()){
        int now = que.front(); que.pop();
        for(int nx: g[now]){
            if(par[now]==nx) continue;
            par[nx] = now;
            que.push(nx);
        }
    }
    // prvec(par);

    vint depth(n, -1);
    rep(i, n){
        if(in[i]>1) continue;
        if(i==st) continue;
        que.push(i);
        depth[i] = 0;
    }

    int ans = -1;
    while(!que.empty()){
        int now = que.front(); que.pop();
        int p = par[now];
        int d = depth[now];
        if(p==-1) {
            if(ans!=-1 && d != ans){
                pr("No");
                return 0;
            }
            continue;
        }

        

        if(depth[p]==-1){
            depth[p] = d+1;
            que.push(p);
        }else{
            if(depth[p] != d+1){
                pr("No");
                return 0;
            }else if(ans!=-1){
                int t = (d+1)*2;
                if(ans != t){
                    pr("No");
                    return 0;
                }
            }
            ans = (d+1)*2;
        }
    }

    // prvec(depth);
    pr("Yes");
    return 0;}
0