結果

問題 No.1424 Ultrapalindrome
ユーザー 👑 NachiaNachia
提出日時 2021-03-12 22:47:28
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,402 bytes
コンパイル時間 4,723 ms
コンパイル使用メモリ 258,704 KB
実行使用メモリ 11,356 KB
最終ジャッジ日時 2024-04-22 14:07:43
合計ジャッジ時間 6,416 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 AC 2 ms
5,248 KB
testcase_02 WA -
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 WA -
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 39 ms
9,080 KB
testcase_10 AC 37 ms
9,064 KB
testcase_11 AC 26 ms
7,424 KB
testcase_12 AC 36 ms
8,556 KB
testcase_13 AC 10 ms
5,376 KB
testcase_14 AC 28 ms
7,808 KB
testcase_15 AC 2 ms
5,376 KB
testcase_16 AC 22 ms
6,940 KB
testcase_17 AC 26 ms
7,552 KB
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 AC 50 ms
10,828 KB
testcase_28 AC 35 ms
10,420 KB
testcase_29 AC 36 ms
10,420 KB
testcase_30 WA -
testcase_31 AC 33 ms
11,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <atcoder/all>
#include <bits/stdc++.h>
using namespace std;
using ll=long long;
using ull=unsigned long long;
#define rep(i,n) for(int i=0; i<(n); i++)
using mll=atcoder::static_modint<1000000007>;

int N;
vector<vector<int>> E;
vector<int> I,P;
int diam, diamcent, diamcent2;
vector<int> D, pD;

int main(){
  scanf("%d",&N);
  E.resize(N);
  rep(i,N-1){
    int u,v; scanf("%d%d",&u,&v); u--; v--;
    E[u].push_back(v);
    E[v].push_back(u);
  }
  P.assign(N,-1);
  queue<int> Q; Q.push(0);
  while(Q.size()){
    int p=Q.front(); Q.pop();
    I.push_back(p);
    for(int e:E[p]) if(P[p]!=e){ P[e]=p; Q.push(e); }
  }
  reverse(I.begin(),I.end());

  D.assign(N,0);
  diam=0; diamcent=-1;
  pD.assign(N,-1);

  for(int i:I) if(i!=0){
    if(diam<D[i]+D[P[i]]+1){ diam=D[i]+D[P[i]]+1; diamcent=i; }
    if(D[P[i]]<D[i]+1){ D[P[i]]=D[i]+1; pD[P[i]]=i; }
  }
  if(diam<D[0]){ diam=D[0]; diamcent=0; }
  while(diam>D[diamcent]*2) diamcent=P[diamcent];
  while(diam<D[diamcent]*2) diamcent=pD[diamcent];

  if(diam%2==1){
    rep(p,N) if(E[p].size()>2){ printf("No\n"); return 0; }
    printf("Yes\n"); return 0;
  }

  P.assign(N,-1);
  D.assign(N,-1);
  Q.push(diamcent);
  while(Q.size()){
    int p=Q.front(); Q.pop();
    for(int e:E[p]) if(P[p]!=e){ P[e]=p; D[e]=D[p]-1; Q.push(e); }
    if(E[p].size()==1) if(D[p]!=0){ printf("No\n"); return 0; }
  }
  
  printf("Yes\n");
  return 0;
}
0