結果

問題 No.1424 Ultrapalindrome
ユーザー ysystem7ysystem7
提出日時 2021-03-12 22:04:22
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 4,133 bytes
コンパイル時間 3,673 ms
コンパイル使用メモリ 234,720 KB
実行使用メモリ 34,256 KB
最終ジャッジ日時 2024-04-22 13:02:21
合計ジャッジ時間 5,675 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,248 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 WA -
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 WA -
testcase_09 AC 42 ms
8,832 KB
testcase_10 AC 46 ms
8,832 KB
testcase_11 AC 27 ms
7,296 KB
testcase_12 AC 38 ms
8,576 KB
testcase_13 AC 10 ms
5,376 KB
testcase_14 AC 31 ms
7,552 KB
testcase_15 AC 2 ms
5,376 KB
testcase_16 AC 23 ms
6,784 KB
testcase_17 AC 30 ms
7,296 KB
testcase_18 AC 32 ms
13,952 KB
testcase_19 AC 55 ms
19,200 KB
testcase_20 AC 12 ms
7,168 KB
testcase_21 AC 43 ms
16,000 KB
testcase_22 AC 26 ms
11,520 KB
testcase_23 AC 4 ms
5,376 KB
testcase_24 AC 12 ms
6,912 KB
testcase_25 AC 11 ms
6,656 KB
testcase_26 AC 69 ms
22,288 KB
testcase_27 WA -
testcase_28 AC 63 ms
34,256 KB
testcase_29 AC 64 ms
34,256 KB
testcase_30 AC 29 ms
11,288 KB
testcase_31 AC 62 ms
32,256 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function 'int main()':
main.cpp:123:11: warning: 'root' may be used uninitialized [-Wmaybe-uninitialized]
  123 |     d[root]=0;
      |           ^
main.cpp:113:9: note: 'root' was declared here
  113 |     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]++;
    }
    if(n==2){
        cout<<"Yes"<<endl;
        return 0;
    }
    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);
    V<int> vec;
    rep(i,n){
        if(deg[i]==1){
            st.insert(d[i]);
            cnt[d[i]]++;
            vec.pb(i);
        }
    }
    bool ok=1;
    if(st.size()>2) ok=0;
    if(st.size()==2){
        int c_u=*begin(st);
        int c_v=*rbegin(st);
        LCA lca(G,n,root);
        sort(all(vec),[&](auto a,auto b){
            return d[a] < d[b];
        });
        int sz=vec.size();
        set<int> ST;
        int u=vec[0],v=vec[sz-1];
        int z=lca.get(u,v);
        ST.insert(d[v]+d[u]-2*d[z]);
        if(cnt[c_u]>1){
            int vv=vec[1];
            int uu=lca.get(vv,u);
            ST.insert(d[vv]+d[u]-2*d[uu]);
        }
        if(cnt[c_v]>1){
            int vv=vec[sz-2];
            int uu=lca.get(vv,v);
            ST.insert(d[vv]+d[v]-2*d[uu]);
        }
        if(ST.size()>1) ok=0;
    }
    
    YN(ok);
}
0