結果

問題 No.1424 Ultrapalindrome
ユーザー 沙耶花沙耶花
提出日時 2021-03-12 21:32:35
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
(最新)
AC  
(最初)
実行時間 -
コード長 2,589 bytes
コンパイル時間 4,813 ms
コンパイル使用メモリ 268,544 KB
実行使用メモリ 21,660 KB
最終ジャッジ日時 2023-08-04 18:56:07
合計ジャッジ時間 6,599 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 40 ms
10,152 KB
testcase_10 AC 38 ms
9,996 KB
testcase_11 AC 26 ms
8,016 KB
testcase_12 AC 36 ms
9,676 KB
testcase_13 AC 10 ms
5,088 KB
testcase_14 AC 31 ms
8,480 KB
testcase_15 AC 2 ms
4,380 KB
testcase_16 AC 24 ms
7,460 KB
testcase_17 AC 30 ms
8,192 KB
testcase_18 AC 24 ms
6,844 KB
testcase_19 AC 31 ms
8,396 KB
testcase_20 AC 8 ms
4,440 KB
testcase_21 AC 26 ms
7,424 KB
testcase_22 AC 17 ms
5,764 KB
testcase_23 AC 6 ms
4,380 KB
testcase_24 AC 7 ms
4,540 KB
testcase_25 AC 6 ms
4,428 KB
testcase_26 AC 38 ms
9,140 KB
testcase_27 WA -
testcase_28 AC 44 ms
21,596 KB
testcase_29 AC 42 ms
21,660 KB
testcase_30 AC 60 ms
12,384 KB
testcase_31 AC 34 ms
12,552 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>
#include <atcoder/all>
#include <bits/stdc++.h>
using namespace std;
using namespace atcoder;
using mint = modint998244353;
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define Inf 1000000000

struct HLD{
	vector<int> sz,parent,depth,root,pos;
	vector<int> arr;
	HLD(vector<vector<int>> &E){
		sz.resize(E.size(),1);
		parent.resize(E.size(),0);
		depth.resize(E.size(),0);
		root.resize(E.size(),0);
		pos.resize(E.size(),0);
		
		dfs(0,-1,E);
		dfs2(0,-1,E,0);
	}
	
	void dfs(int now,int p,vector<vector<int>> &E){
		parent[now] = p;
		if(p==-1){
			depth[now] = 0;
		}
		else{
			depth[now] = depth[p]+1;
		}
		for(int i=0;i<E[now].size();i++){
			int to = E[now][i];
			if(to==p)continue;
			dfs(to,now,E);
			sz[now] += sz[to];
		}
	}
	
	void dfs2(int now,int p,vector<vector<int>> &E,int r){
		pos[now] = arr.size();
		arr.push_back(now);
		root[now] = r;
		int maxi = 0;
		int ind = -1;
		for(int i=0;i<E[now].size();i++){
			int to = E[now][i];
			if(to==p)continue;
			if(maxi<sz[to]){
				maxi = sz[to];
				ind = to;
			}
		}
		if(ind==-1)return;
		dfs2(ind,now,E,r);
		for(int i=0;i<E[now].size();i++){
			int to = E[now][i];
			if(to==p||to==ind)continue;
			dfs2(to,now,E,to);
		}
	}
	
	vector<pair<int,int>> query(int u,int v){
		vector<pair<int,int>> ret;
		int t = 0;
		while(root[u]!=root[v]){
			if(depth[root[u]] <= depth[root[v]]){
				ret.insert(ret.begin()+t,{pos[root[v]], pos[v]});
				v = parent[root[v]];
			}
			else{
				ret.insert(ret.begin()+t,{pos[u],pos[root[u]]});
				u = parent[root[u]];
				t++;
			}
		}
		ret.insert(ret.begin()+t,{pos[u],pos[v]});
		return ret;
	}
	
	int lca(int u,int v){
		for(;;v=parent[root[v]]){
			if(pos[u]>pos[v])swap(u,v);
			if(root[u]==root[v])return u;
		}
	}
	
	int get_distance(int u,int v){
		return depth[u] + depth[v] - 2 * depth[lca(u,v)];
	}
	
};

unsigned long xor128() {
	static unsigned long x=123456789, y=362436069, z=521288629, w=88675123;
	unsigned long t=(x^(x<<11));
	x=y; y=z; z=w;
	return (w=(w^(w>>19))^(t^(t>>8)));
}

int main(){
	
	int N;
	cin>>N;
	
	vector<vector<int>> E(N);
	vector<int> d(N,0);
	rep(i,N-1){
		int u,v;
		scanf("%d %d",&u,&v);
		u--;v--;
		d[u]++;
		d[v]++;
		E[u].push_back(v);
		E[v].push_back(u);
	}
	
	HLD H(E);
	
	vector<int> x;
	rep(i,N){
		if(d[i]==1)x.push_back(i);
	}
	
	rep(_,50){
		int a = xor128()%x.size();
		int aa = -1;
		rep(j,x.size()){
			if(j==a)continue;
			int dis = H.get_distance(x[j],x[a]);
			if(aa==-1)aa=  dis;
			else{
				if(aa!=dis){
					cout<<"No"<<endl;
					return 0;
				}
			}
		}
	}
	
	cout<<"Yes"<<endl;
	
    return 0;
}
0