結果

問題 No.1424 Ultrapalindrome
ユーザー eQeeQe
提出日時 2021-03-12 22:03:02
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 100 ms / 2,000 ms
コード長 2,005 bytes
コンパイル時間 4,128 ms
コンパイル使用メモリ 237,216 KB
実行使用メモリ 14,196 KB
最終ジャッジ日時 2023-08-04 19:03:35
合計ジャッジ時間 6,417 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
5,940 KB
testcase_01 AC 3 ms
5,760 KB
testcase_02 AC 3 ms
6,032 KB
testcase_03 AC 3 ms
5,804 KB
testcase_04 AC 3 ms
5,768 KB
testcase_05 AC 3 ms
5,748 KB
testcase_06 AC 3 ms
5,772 KB
testcase_07 AC 3 ms
5,916 KB
testcase_08 AC 3 ms
5,760 KB
testcase_09 AC 73 ms
10,516 KB
testcase_10 AC 72 ms
10,556 KB
testcase_11 AC 52 ms
9,128 KB
testcase_12 AC 70 ms
10,280 KB
testcase_13 AC 19 ms
7,032 KB
testcase_14 AC 56 ms
9,232 KB
testcase_15 AC 3 ms
5,756 KB
testcase_16 AC 44 ms
8,568 KB
testcase_17 AC 51 ms
9,204 KB
testcase_18 AC 34 ms
8,180 KB
testcase_19 AC 58 ms
9,128 KB
testcase_20 AC 15 ms
6,756 KB
testcase_21 AC 42 ms
8,436 KB
testcase_22 AC 26 ms
7,360 KB
testcase_23 AC 8 ms
6,140 KB
testcase_24 AC 13 ms
6,588 KB
testcase_25 AC 12 ms
6,536 KB
testcase_26 AC 66 ms
9,744 KB
testcase_27 AC 100 ms
12,492 KB
testcase_28 AC 65 ms
11,000 KB
testcase_29 AC 65 ms
10,984 KB
testcase_30 AC 68 ms
13,976 KB
testcase_31 AC 78 ms
14,196 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