結果

問題 No.1424 Ultrapalindrome
ユーザー eQeeQe
提出日時 2021-03-12 22:03:02
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 93 ms / 2,000 ms
コード長 2,005 bytes
コンパイル時間 4,250 ms
コンパイル使用メモリ 233,068 KB
実行使用メモリ 14,092 KB
最終ジャッジ日時 2024-04-22 17:08:15
合計ジャッジ時間 6,068 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
6,016 KB
testcase_01 AC 3 ms
6,016 KB
testcase_02 AC 3 ms
5,888 KB
testcase_03 AC 3 ms
6,016 KB
testcase_04 AC 3 ms
6,016 KB
testcase_05 AC 3 ms
5,888 KB
testcase_06 AC 3 ms
6,144 KB
testcase_07 AC 3 ms
5,888 KB
testcase_08 AC 4 ms
5,888 KB
testcase_09 AC 71 ms
10,584 KB
testcase_10 AC 73 ms
10,624 KB
testcase_11 AC 50 ms
9,216 KB
testcase_12 AC 67 ms
10,328 KB
testcase_13 AC 20 ms
7,168 KB
testcase_14 AC 54 ms
9,344 KB
testcase_15 AC 3 ms
5,888 KB
testcase_16 AC 42 ms
8,576 KB
testcase_17 AC 51 ms
9,344 KB
testcase_18 AC 35 ms
8,320 KB
testcase_19 AC 55 ms
9,344 KB
testcase_20 AC 14 ms
6,912 KB
testcase_21 AC 42 ms
8,332 KB
testcase_22 AC 28 ms
7,628 KB
testcase_23 AC 8 ms
6,528 KB
testcase_24 AC 13 ms
6,784 KB
testcase_25 AC 12 ms
6,656 KB
testcase_26 AC 67 ms
9,728 KB
testcase_27 AC 93 ms
12,728 KB
testcase_28 AC 70 ms
11,008 KB
testcase_29 AC 68 ms
11,136 KB
testcase_30 AC 68 ms
14,092 KB
testcase_31 AC 80 ms
14,088 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define pt(sth) cout << sth << "\n"
#define itr(x,c) for(auto x=c.begin();x!=c.end();x++)
#define all(a) (a.begin()),(a.end())
using namespace std;
#include <atcoder/all>
using namespace atcoder;
typedef long long ll;
typedef pair<ll, ll> pll;
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;}
static const ll INF=1e18;
static const ll MAX=101010;
//static const ll MOD=1e9+7;
//static const ll MOD=998244353;
//vector<ll> a(N);
//for(i=0;i<N;i++) cin>>a[i];

typedef vector<ll> v1d;
typedef vector<v1d> v2d;
typedef vector<v2d> v3d;


class Edge {
public:
  ll t, w;
  Edge() {}
  Edge(ll t, ll w): t(t), w(w) {}
};

vector<Edge> g[MAX];

vector<ll> dijkstra(ll s, ll n) {
  ll i;
  vector<ll> d(n,INF);
  priority_queue<pll> q;
  d[s]=0;
  q.push({0, s});
    
  while(q.size()) {
    ll du=-q.top().first;
    ll u=q.top().second;
    q.pop();
    
    if(d[u]<du) continue;
        
    for(i=0; i<g[u].size(); i++) {
      Edge e=g[u][i];
      
      if(chmin(d[e.t], du+e.w))
        q.push({-du-e.w, e.t});
    }
  }
  return d;
}

int main(){
  ll i,j;
  
  ll N;cin>>N;
  for(i=0;i<N-1;i++){
    ll a,b;cin>>a>>b;a--;b--;
    g[a].push_back(Edge(b, 1));g[b].push_back(Edge(a, 1));
  }
  
  v1d d=dijkstra(0, N);
  
  v1d cd;
  for(i=0;i<N;i++){
    if(g[i].size()>2) cd.push_back(d[i]);
  }
  
  if(cd.size()==0){
    pt("Yes");
  }else if(cd.size()>1){
    pt("No");
  }else if(cd.size()==1){
    vector<ll> v;
    for(i=0;i<N;i++){
      if(g[i].size()==1) v.push_back(d[i]);
    }
    sort(all(v));
    
    if(cd[0]==0){
      if(v[0]==v[(ll)v.size()-1]){
        pt("Yes");
      }else{
        pt("No");
      }
    }else{
      ll x=v[0]==v[1]?v[0]:v[(ll)v.size()-1];
      ll y=v[0]!=v[1]?v[0]:v[(ll)v.size()-1];
      
      if(x-cd[0]==cd[0]+y){
        pt("Yes");
      }else{
        pt("No");
      }
    }
    
    
  }
  
  
  
  
  
  
}
0