結果
問題 | No.1424 Ultrapalindrome |
ユーザー |
|
提出日時 | 2021-03-12 22:03:02 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 89 ms / 2,000 ms |
コード長 | 2,005 bytes |
コンパイル時間 | 4,053 ms |
コンパイル使用メモリ | 239,036 KB |
実行使用メモリ | 14,100 KB |
最終ジャッジ日時 | 2024-10-14 16:16:40 |
合計ジャッジ時間 | 5,612 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 29 |
ソースコード
#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");}}}}