結果

問題 No.1295 木と駒
ユーザー 👑 kmjpkmjp
提出日時 2020-11-27 02:09:18
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 3,009 bytes
コンパイル時間 2,933 ms
コンパイル使用メモリ 208,012 KB
実行使用メモリ 35,604 KB
最終ジャッジ日時 2023-10-01 03:49:56
合計ジャッジ時間 13,469 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 AC 8 ms
16,840 KB
testcase_03 AC 7 ms
16,864 KB
testcase_04 WA -
testcase_05 AC 7 ms
16,808 KB
testcase_06 AC 7 ms
16,812 KB
testcase_07 WA -
testcase_08 WA -
testcase_09 AC 7 ms
16,852 KB
testcase_10 AC 7 ms
16,768 KB
testcase_11 AC 7 ms
16,864 KB
testcase_12 AC 7 ms
16,776 KB
testcase_13 AC 7 ms
16,960 KB
testcase_14 WA -
testcase_15 WA -
testcase_16 AC 212 ms
23,068 KB
testcase_17 AC 213 ms
23,116 KB
testcase_18 AC 207 ms
23,076 KB
testcase_19 AC 180 ms
23,132 KB
testcase_20 AC 203 ms
35,604 KB
testcase_21 AC 196 ms
26,864 KB
testcase_22 AC 185 ms
23,724 KB
testcase_23 AC 190 ms
27,828 KB
testcase_24 AC 189 ms
27,916 KB
testcase_25 AC 182 ms
23,676 KB
testcase_26 AC 171 ms
32,676 KB
testcase_27 AC 171 ms
32,448 KB
testcase_28 AC 244 ms
31,348 KB
testcase_29 AC 170 ms
32,436 KB
testcase_30 AC 172 ms
32,400 KB
testcase_31 AC 168 ms
32,552 KB
testcase_32 AC 200 ms
29,544 KB
testcase_33 AC 200 ms
23,420 KB
testcase_34 AC 205 ms
23,876 KB
testcase_35 AC 192 ms
23,184 KB
testcase_36 AC 198 ms
23,452 KB
testcase_37 AC 188 ms
23,256 KB
testcase_38 AC 196 ms
23,756 KB
testcase_39 AC 186 ms
23,236 KB
testcase_40 AC 199 ms
23,232 KB
testcase_41 AC 175 ms
23,388 KB
testcase_42 AC 182 ms
23,236 KB
testcase_43 AC 186 ms
23,080 KB
testcase_44 AC 177 ms
23,064 KB
testcase_45 AC 184 ms
23,212 KB
testcase_46 AC 180 ms
23,356 KB
testcase_47 AC 180 ms
23,152 KB
testcase_48 WA -
権限があれば一括ダウンロードができます

ソースコード

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);
	
	
	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||(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&&E[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