結果

問題 No.1424 Ultrapalindrome
ユーザー umezoumezo
提出日時 2021-03-12 23:29:42
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
(最新)
AC  
(最初)
実行時間 -
コード長 1,465 bytes
コンパイル時間 2,614 ms
コンパイル使用メモリ 205,320 KB
実行使用メモリ 12,484 KB
最終ジャッジ日時 2024-04-22 17:16:17
合計ジャッジ時間 4,496 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 2 ms
6,944 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 2 ms
6,944 KB
testcase_04 AC 2 ms
6,940 KB
testcase_05 AC 2 ms
6,940 KB
testcase_06 AC 1 ms
6,944 KB
testcase_07 AC 2 ms
6,940 KB
testcase_08 AC 2 ms
6,944 KB
testcase_09 AC 64 ms
8,192 KB
testcase_10 AC 64 ms
8,064 KB
testcase_11 AC 44 ms
6,940 KB
testcase_12 AC 57 ms
7,936 KB
testcase_13 AC 16 ms
6,944 KB
testcase_14 AC 48 ms
6,944 KB
testcase_15 AC 2 ms
6,944 KB
testcase_16 AC 37 ms
6,940 KB
testcase_17 AC 43 ms
6,944 KB
testcase_18 AC 35 ms
6,944 KB
testcase_19 AC 53 ms
6,944 KB
testcase_20 AC 13 ms
6,940 KB
testcase_21 AC 41 ms
6,940 KB
testcase_22 AC 28 ms
6,944 KB
testcase_23 AC 7 ms
6,944 KB
testcase_24 AC 12 ms
6,940 KB
testcase_25 AC 11 ms
6,940 KB
testcase_26 AC 66 ms
7,552 KB
testcase_27 WA -
testcase_28 AC 72 ms
9,088 KB
testcase_29 AC 73 ms
9,312 KB
testcase_30 AC 70 ms
12,484 KB
testcase_31 AC 84 ms
12,480 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define ALL(v) v.begin(), v.end()
typedef long long ll;

#include <bits/stdc++.h>
using namespace std;

const int INF=1e9;

struct Edge{
  int to;
  int w;
  Edge(int to,ll w) : to(to),w(w) {}
};

using Graph=vector<vector<Edge>>;
using pli=pair<ll,int>;

int dist[100010];
int dim[100010];

template<class T> bool chmin(T& a,T b){
  if(a>b){
    a=b;
    return true;
  }
  return false;
}

int main(){
  int n;
  cin>>n;
  
  Graph G(n);
  rep(i,n-1){
    int a,b;
    cin>>a>>b;
    a--,b--;
    G[a].push_back(Edge(b,1));
    G[b].push_back(Edge(a,1));
  }
  
  rep(i,n) dim[i]=G[i].size();
  int cnt=0;
  int mid;
  rep(i,n){
    if(dim[i]>2){
      cnt++;
      mid=i;
    }
  }
  if(cnt>2){
    cout<<"No"<<endl;
    return 0;
  }
  else if(cnt==0){
    cout<<"Yes"<<endl;
    return 0;
  }

  int s=mid;
  rep(i,n) dist[i]=INF;
  dist[s]=0;
  
  priority_queue<pli,vector<pli>,greater<pli>> que;
  que.push({dist[s],s});
  
  while(!que.empty()){
    int v=que.top().second;
    ll d=que.top().first;
    que.pop();
    
    if(d>dist[v]) continue;
    
    for(auto e:G[v]){
      if(chmin(dist[e.to],dist[v]+e.w)){
        que.push({dist[e.to],e.to});
      }
    }
  }
  
  bool b=true;
  int tmp=-1;
  rep(i,n){
    if(dim[i]!=1) continue;
    if(tmp==-1) tmp=dist[i];
    else if(dist[i]!=tmp){
      b=false;
      break;
    }
  }
  if(b) cout<<"Yes"<<endl;
  else cout<<"No"<<endl;

  return 0;
}
0