結果

問題 No.1424 Ultrapalindrome
ユーザー ysystem7ysystem7
提出日時 2021-03-12 21:49:40
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
RE  
実行時間 -
コード長 3,488 bytes
コンパイル時間 3,243 ms
コンパイル使用メモリ 219,980 KB
実行使用メモリ 10,772 KB
最終ジャッジ日時 2024-04-22 12:42:53
合計ジャッジ時間 4,779 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 RE -
testcase_04 AC 2 ms
5,376 KB
testcase_05 WA -
testcase_06 WA -
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 39 ms
8,448 KB
testcase_10 AC 40 ms
8,320 KB
testcase_11 AC 29 ms
6,912 KB
testcase_12 AC 39 ms
8,320 KB
testcase_13 AC 11 ms
5,376 KB
testcase_14 AC 29 ms
7,296 KB
testcase_15 AC 2 ms
5,376 KB
testcase_16 AC 23 ms
6,400 KB
testcase_17 AC 28 ms
7,040 KB
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 AC 4 ms
5,376 KB
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 AC 52 ms
10,112 KB
testcase_28 AC 34 ms
9,984 KB
testcase_29 AC 34 ms
9,984 KB
testcase_30 AC 29 ms
10,772 KB
testcase_31 AC 33 ms
10,768 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function 'int main()':
main.cpp:119:11: warning: 'root' may be used uninitialized [-Wmaybe-uninitialized]
  119 |     d[root]=0;
      |           ^
main.cpp:109:9: note: 'root' was declared here
  109 |     int root;
      |         ^~~~

ソースコード

diff #

#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<n;i++)
#define cinf(n,x) for(int i=0;i<(n);i++)cin>>x[i];
#define ft first
#define sc second
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define all(v) (v).begin(),(v).end()
#define LB(a,x) lb(all(a),x)-a.begin()
#define UB(a,x) ub(all(a),x)-a.begin()
#define mod 1000000007
//#define mod 998244353
#define FS fixed<<setprecision(15)
using namespace std;
typedef long long ll;
const double pi=3.141592653589793;
template<class T> using V=vector<T>;
using P=pair<ll,ll>;
typedef unsigned long long ull;
typedef long double ldouble;
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline void out(T a){ cout << a << '\n'; }
void YN(bool ok){if(ok) cout << "Yes" << endl; else cout << "No" << endl;}
//void YN(bool ok){if(ok) cout << "YES" << endl; else cout << "NO" << endl;}


const ll INF=1e18;
const int mx=200005;
class LCA{
public:
    V<V<int>> G;
    int n;
    int root;
    V<V<int>> par;
    V<int> depth;
    int MX_LOG=24;
    explicit LCA(V<V<int>> g,int sz,int root_){
        G=g;
        n=sz;
        root=root_;
        par.resize(MX_LOG,V<int>(n,0));
        depth.resize(n,0);
        init();
    }

    void dfs(int v,int p,int d){
        par[0][v]=p;
        depth[v]=d;
        for(int i=0;i<G[v].size();i++){
            if(G[v][i]!=p) dfs(G[v][i],v,d+1);
        }
    }

    void init(){
        //par[0]とdepthを初期化する
        dfs(root,-1,0);
        //parを初期化
        for(int k=0;k+1<MX_LOG;k++){
            for(int v=0;v<n;v++){
                if(par[k][v]<0) par[k+1][v]=-1;
                else par[k+1][v]=par[k][par[k][v]];
            }
        }
    }

    int get(int u,int v){
        //uとvの深さが同じになるまで親をたどる
        if(depth[u]>depth[v]) swap(u,v);
        for(int k=0;k<MX_LOG;k++){
            if((depth[v]-depth[u])>>k&1){
                v=par[k][v];
            }
        }
        if(u==v) return u;
        //二分探索でLCAを求める
        for(int k=MX_LOG-1;k>=0;k--){
            if(par[k][u]!=par[k][v]){
                u=par[k][u];
                v=par[k][v];
            }
        }
        return par[0][u];
    }

    ll dist(int u){
        return (ll)depth[u];
    }
};

int main(){
    //オーバーフローは大丈夫ですか??
    cin.tie(0);ios::sync_with_stdio(false);
    int n;
    cin>>n;
    V<V<int>> G(n);
    V<int> deg(n,0);
    rep(i,n-1){
        int a,b;
        cin>>a>>b;
        a--;b--;
        G[a].pb(b);
        G[b].pb(a);
        deg[a]++;
        deg[b]++;
    }
    int root;
    rep(i,n){
        if(deg[i]>1){
            root=i;
            break;
        }
    }
    V<int> d(n,-1);
    queue<int> que;
    que.push(root);
    d[root]=0;
    while(que.size()){
        int v=que.front();
        que.pop();
        for(int nv:G[v]){
            if(d[nv]!=-1) continue;
            d[nv]=d[v]+1;
            que.push(nv);
        }
    }
    set<int> st;
    V<int> cnt(n,0);
    rep(i,n){
        if(deg[i]==1){
            st.insert(d[i]);
            cnt[d[i]]++;
        }
    }
    bool ok=1;
    if(st.size()>2) ok=0;
    if(st.size()==2){
        int u=*begin(st);
        int v=*rbegin(st);
        if(cnt[u]>1||cnt[v]>1) ok=0;
    }
    YN(ok);
}
0