結果

問題 No.1295 木と駒
ユーザー 👑 kmjpkmjp
提出日時 2020-11-27 02:33:17
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 237 ms / 2,000 ms
コード長 3,193 bytes
コンパイル時間 2,238 ms
コンパイル使用メモリ 207,084 KB
実行使用メモリ 37,084 KB
最終ジャッジ日時 2023-10-01 03:51:33
合計ジャッジ時間 12,316 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 7 ms
16,768 KB
testcase_01 AC 8 ms
16,776 KB
testcase_02 AC 7 ms
16,784 KB
testcase_03 AC 7 ms
16,776 KB
testcase_04 AC 7 ms
16,844 KB
testcase_05 AC 7 ms
16,804 KB
testcase_06 AC 8 ms
16,792 KB
testcase_07 AC 7 ms
16,772 KB
testcase_08 AC 7 ms
16,788 KB
testcase_09 AC 7 ms
16,816 KB
testcase_10 AC 7 ms
16,856 KB
testcase_11 AC 7 ms
16,808 KB
testcase_12 AC 8 ms
16,828 KB
testcase_13 AC 7 ms
16,828 KB
testcase_14 AC 199 ms
23,060 KB
testcase_15 AC 199 ms
23,056 KB
testcase_16 AC 201 ms
23,056 KB
testcase_17 AC 203 ms
23,032 KB
testcase_18 AC 204 ms
23,076 KB
testcase_19 AC 194 ms
23,140 KB
testcase_20 AC 213 ms
37,084 KB
testcase_21 AC 206 ms
27,296 KB
testcase_22 AC 201 ms
23,548 KB
testcase_23 AC 187 ms
28,576 KB
testcase_24 AC 189 ms
28,748 KB
testcase_25 AC 185 ms
23,848 KB
testcase_26 AC 187 ms
33,964 KB
testcase_27 AC 187 ms
33,980 KB
testcase_28 AC 237 ms
32,712 KB
testcase_29 AC 185 ms
33,988 KB
testcase_30 AC 193 ms
33,960 KB
testcase_31 AC 188 ms
34,148 KB
testcase_32 AC 204 ms
30,640 KB
testcase_33 AC 208 ms
23,224 KB
testcase_34 AC 209 ms
24,040 KB
testcase_35 AC 197 ms
23,416 KB
testcase_36 AC 196 ms
23,152 KB
testcase_37 AC 193 ms
23,256 KB
testcase_38 AC 201 ms
23,464 KB
testcase_39 AC 200 ms
23,196 KB
testcase_40 AC 197 ms
23,144 KB
testcase_41 AC 186 ms
23,068 KB
testcase_42 AC 189 ms
23,204 KB
testcase_43 AC 189 ms
23,208 KB
testcase_44 AC 190 ms
23,060 KB
testcase_45 AC 187 ms
23,304 KB
testcase_46 AC 187 ms
23,048 KB
testcase_47 AC 189 ms
23,292 KB
testcase_48 AC 189 ms
23,204 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef signed long long ll;

#define _P(...) (void)printf(__VA_ARGS__)
#define FOR(x,to) for(x=0;x<(to);x++)
#define FORR(x,arr) for(auto& x:arr)
#define FORR2(x,y,arr) for(auto& [x,y]:arr)
#define ALL(a) (a.begin()),(a.end())
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
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(a>b){a=b;return 1;}return 0;}
//-------------------------------------------------------

int N;
vector<int> E[101010];
int vcant[202020],vng[202020]; //自身が戻れない頂点か、子方向に戻れない頂点があるか

vector<int> cant[202020]; //子頂点方向で戻れない頂点
vector<int> ng[202020]; //子頂点方向で不可頂点

//戻れない辺を持つ子をカウント
pair<int,int> dfs(int cur,int pre) { //戻れない辺の有無・不可頂点の有無
	sort(ALL(E[cur]));
	int mc=0;
	FORR(e,E[cur]) if(e<pre) vcant[cur]=1;
	FORR(e,E[cur]) if(e!=pre) {
		auto p=dfs(e,cur);
		mc=max(mc,e);
		if(p.first) cant[cur].push_back(e), vcant[cur]=1;
		if(p.second) ng[cur].push_back(e);
	}
	
	vng[cur]=ng[cur].size()>0;
	if(vng[cur]==0&&cant[cur].size()>=2) {
		vng[cur]=1;
	}
	if(vng[cur]==0&&cant[cur].size()==1) {
		//隣接点のうち最小でないし子のうち最大でもない
		if(cant[cur][0]!=E[cur][0] && cant[cur][0]!=mc) {
			vng[cur]=1;
		}
	}
	
	return {vcant[cur], vng[cur]};
}

void dfs2(int cur,int pre,int pcant,int png) {
	
	if(pcant) cant[cur].push_back(pre);
	if(png) ng[cur].push_back(pre);
	
	/*
	cout<<"--------------"<<cur<<" "<<pre<<" "<<pcant<<" "<<png<<endl;
	FORR(c,cant[cur]) cout<<"cant"<<c<<" ";
	FORR(c,ng[cur]) cout<<"ng"<<c<<" ";
	cout<<endl;
	*/
	vng[cur]=ng[cur].size()>0;
	if(vng[cur]==0&&cant[cur].size()>=2) {
		vng[cur]=1;
	}
	if(vng[cur]==0&&cant[cur].size()==1) {
		//隣接点のうち最小でないし子のうち最大でもない
		if(cant[cur][0]!=E[cur][0] && cant[cur][0]!=E[cur].back()) {
			vng[cur]=1;
		}
	}
	
	FORR(e,E[cur]) if(e!=pre) {
		int ncant=0, nng=0;
		
		if(ng[cur].size()>=2||(ng[cur].size()==1&&vng[e]==0)) {
			nng=1;
		}
		else if(cant[cur].size()>=3||(cant[cur].size()==2&&vcant[e]==0)) {
			nng=1;
		}
		else if((cant[cur].size()==1&&vcant[e]==0)||(cant[cur].size()==2&&vcant[e]==1)) {
			int tar=cant[cur][0];
			if(tar==e) tar=cant[cur][1];
			if(tar!=E[cur][0]&&tar!=E[cur].back()&&!(e==E[cur].back()&&tar==E[cur][E[cur].size()-2])) {
				nng=1;
			}
		}
		
		if(nng==0) {
			if(cant[cur].size()>=2) ncant=1;
			if(cant[cur].size()>=1&&cant[cur][0]!=e) ncant=1;
			if(e!=E[cur][0]) ncant=1;
		}
		
		
		
		
		dfs2(e,cur,ncant,nng);
	}
}



void solve() {
	int i,j,k,l,r,x,y; string s;
	
	cin>>N;
	FOR(i,N-1) {
		cin>>x>>y;
		E[x-1].push_back(y-1);
		E[y-1].push_back(x-1);
	}
	dfs(0,0);
	dfs2(0,0,0,0);
	FOR(i,N) {
		if(vng[i]) cout<<"No"<<endl;
		else cout<<"Yes"<<endl;
	}
}


int main(int argc,char** argv){
	string s;int i;
	if(argc==1) ios::sync_with_stdio(false), cin.tie(0);
	FOR(i,argc-1) s+=argv[i+1],s+='\n'; FOR(i,s.size()) ungetc(s[s.size()-1-i],stdin);
	cout.tie(0); solve(); return 0;
}
0