結果
| 問題 |
No.1424 Ultrapalindrome
|
| コンテスト | |
| ユーザー |
ysystem7
|
| 提出日時 | 2021-03-12 21:49:40 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 3,488 bytes |
| コンパイル時間 | 2,761 ms |
| コンパイル使用メモリ | 224,428 KB |
| 最終ジャッジ日時 | 2025-01-19 14:33:12 |
|
ジャッジサーバーID (参考情報) |
judge5 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 19 WA * 10 |
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:119:11: warning: ‘root’ may be used uninitialized [-Wmaybe-uninitialized]
119 | d[root]=0;
| ^
main.cpp:109:9: note: ‘root’ was declared here
109 | int root;
| ^~~~
ソースコード
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<n;i++)
#define cinf(n,x) for(int i=0;i<(n);i++)cin>>x[i];
#define ft first
#define sc second
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define all(v) (v).begin(),(v).end()
#define LB(a,x) lb(all(a),x)-a.begin()
#define UB(a,x) ub(all(a),x)-a.begin()
#define mod 1000000007
//#define mod 998244353
#define FS fixed<<setprecision(15)
using namespace std;
typedef long long ll;
const double pi=3.141592653589793;
template<class T> using V=vector<T>;
using P=pair<ll,ll>;
typedef unsigned long long ull;
typedef long double ldouble;
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline void out(T a){ cout << a << '\n'; }
void YN(bool ok){if(ok) cout << "Yes" << endl; else cout << "No" << endl;}
//void YN(bool ok){if(ok) cout << "YES" << endl; else cout << "NO" << endl;}
const ll INF=1e18;
const int mx=200005;
class LCA{
public:
V<V<int>> G;
int n;
int root;
V<V<int>> par;
V<int> depth;
int MX_LOG=24;
explicit LCA(V<V<int>> g,int sz,int root_){
G=g;
n=sz;
root=root_;
par.resize(MX_LOG,V<int>(n,0));
depth.resize(n,0);
init();
}
void dfs(int v,int p,int d){
par[0][v]=p;
depth[v]=d;
for(int i=0;i<G[v].size();i++){
if(G[v][i]!=p) dfs(G[v][i],v,d+1);
}
}
void init(){
//par[0]とdepthを初期化する
dfs(root,-1,0);
//parを初期化
for(int k=0;k+1<MX_LOG;k++){
for(int v=0;v<n;v++){
if(par[k][v]<0) par[k+1][v]=-1;
else par[k+1][v]=par[k][par[k][v]];
}
}
}
int get(int u,int v){
//uとvの深さが同じになるまで親をたどる
if(depth[u]>depth[v]) swap(u,v);
for(int k=0;k<MX_LOG;k++){
if((depth[v]-depth[u])>>k&1){
v=par[k][v];
}
}
if(u==v) return u;
//二分探索でLCAを求める
for(int k=MX_LOG-1;k>=0;k--){
if(par[k][u]!=par[k][v]){
u=par[k][u];
v=par[k][v];
}
}
return par[0][u];
}
ll dist(int u){
return (ll)depth[u];
}
};
int main(){
//オーバーフローは大丈夫ですか??
cin.tie(0);ios::sync_with_stdio(false);
int n;
cin>>n;
V<V<int>> G(n);
V<int> deg(n,0);
rep(i,n-1){
int a,b;
cin>>a>>b;
a--;b--;
G[a].pb(b);
G[b].pb(a);
deg[a]++;
deg[b]++;
}
int root;
rep(i,n){
if(deg[i]>1){
root=i;
break;
}
}
V<int> d(n,-1);
queue<int> que;
que.push(root);
d[root]=0;
while(que.size()){
int v=que.front();
que.pop();
for(int nv:G[v]){
if(d[nv]!=-1) continue;
d[nv]=d[v]+1;
que.push(nv);
}
}
set<int> st;
V<int> cnt(n,0);
rep(i,n){
if(deg[i]==1){
st.insert(d[i]);
cnt[d[i]]++;
}
}
bool ok=1;
if(st.size()>2) ok=0;
if(st.size()==2){
int u=*begin(st);
int v=*rbegin(st);
if(cnt[u]>1||cnt[v]>1) ok=0;
}
YN(ok);
}
ysystem7