Submission #1830078


Source Code Expand

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cassert>
#include <cctype>
using namespace std;
typedef long long lint;
#define cout cerr
#define ni (next_num<int>())
template<class T>inline T next_num(){
	T i=0;char c;
	while(!isdigit(c=getchar())&&c!='-');
	bool flag=c=='-';
	flag?c=getchar():0;
	while(i=i*10-'0'+c,isdigit(c=getchar()));
	return flag?-i:i;
}
template<class T1,class T2>inline void apmax(T1 &a,const T2 &b){if(a<b)a=b;}
template<class T1,class T2>inline void apmin(T1 &a,const T2 &b){if(b<a)a=b;}
const int N=100010,INF=0x7f7f7f7f;
int pval[N];
namespace T{
	const int E=N<<1;
	int to[E],bro[E],head[N],e=0;
	int liml[N],limr[N];
	inline void init(){
		memset(head,-1,sizeof(head));
		memset(liml,128,sizeof(liml));
		memset(limr,127,sizeof(limr));
	}
	inline void ae(int u,int v){
		to[e]=v,bro[e]=head[u],head[u]=e++;
	}
	inline void add(int u,int v){
		ae(u,v),ae(v,u);
	}
	bool dfs1(int x,int fa){
		for(int i=head[x],v;~i;i=bro[i]){
			if((v=to[i])!=fa){
				if(!dfs1(v,x))return false;
				if(limr[v]<INF){
					if(limr[x]==INF){
						liml[x]=liml[v]-1,limr[x]=limr[v]+1;
					}else if((limr[x]&1)!=(limr[v]&1)){
						apmax(liml[x],liml[v]-1);
						apmin(limr[x],limr[v]+1);
						if(liml[x]>limr[x])return false;
					}else return false;
				}
			}
		}
		return true;
	}
	void dfs2(int x,int fa){
		if(fa==0){
			pval[x]=limr[x];
		}else{
			pval[x]=min(limr[x],pval[fa]+1);
			assert(pval[x]>=liml[x]);
		}
		for(int i=head[x],v;~i;i=bro[i]){
			if((v=to[i])!=fa){
				dfs2(v,x);
			}
		}
	}
}
int main(){
	int n=ni;
	T::init();
	for(int i=1;i<n;T::add(ni,ni),i++);
	for(int tot=ni;tot--;){
		int x=ni;
		T::liml[x]=T::limr[x]=ni;
	}
	if(!T::dfs1(1,0)){
		puts("No");
		return 0;
	}
	puts("Yes");
	T::dfs2(1,0);
	for(int i=1;i<=n;i++){
		printf("%d\n",pval[i]);
	}
	putchar('\n');
	return 0;
}

Submission Info

Submission Time
Task E - Integers on a Tree
User sshockwave
Language C++14 (GCC 5.4.1)
Score 800
Code Size 1929 Byte
Status AC
Exec Time 50 ms
Memory 8832 KB

Judge Result

Set Name sample All
Score / Max Score 0 / 0 800 / 800
Status
AC × 3
AC × 39
Set Name Test Cases
sample sample_01.txt, sample_02.txt, sample_03.txt
All binary_01.txt, binary_02.txt, hand_01.txt, hand_02.txt, hand_03.txt, kary_01.txt, kary_02.txt, kary_03.txt, line_01.txt, line_02.txt, line_03.txt, line_04.txt, line_05.txt, line_06.txt, random0_01.txt, random1_01.txt, random1_02.txt, random1_03.txt, random1_04.txt, random1_05.txt, random1_06.txt, random1_07.txt, random1_08.txt, random2_01.txt, random2_02.txt, random2_03.txt, random2_04.txt, random2_05.txt, random2_06.txt, random3_01.txt, random3_02.txt, random4_01.txt, random4_02.txt, random4_03.txt, sample_01.txt, sample_02.txt, sample_03.txt, star_01.txt, star_02.txt
Case Name Status Exec Time Memory
binary_01.txt AC 27 ms 6400 KB
binary_02.txt AC 13 ms 5376 KB
hand_01.txt AC 2 ms 1408 KB
hand_02.txt AC 2 ms 1408 KB
hand_03.txt AC 2 ms 1408 KB
kary_01.txt AC 12 ms 2944 KB
kary_02.txt AC 21 ms 4096 KB
kary_03.txt AC 12 ms 2944 KB
line_01.txt AC 16 ms 7680 KB
line_02.txt AC 24 ms 8704 KB
line_03.txt AC 24 ms 8704 KB
line_04.txt AC 25 ms 8832 KB
line_05.txt AC 15 ms 7680 KB
line_06.txt AC 16 ms 7680 KB
random0_01.txt AC 16 ms 2944 KB
random1_01.txt AC 27 ms 4096 KB
random1_02.txt AC 28 ms 4096 KB
random1_03.txt AC 29 ms 4096 KB
random1_04.txt AC 33 ms 4096 KB
random1_05.txt AC 28 ms 4096 KB
random1_06.txt AC 27 ms 4096 KB
random1_07.txt AC 28 ms 4096 KB
random1_08.txt AC 50 ms 4096 KB
random2_01.txt AC 14 ms 2944 KB
random2_02.txt AC 13 ms 2944 KB
random2_03.txt AC 15 ms 2944 KB
random2_04.txt AC 14 ms 2944 KB
random2_05.txt AC 18 ms 2944 KB
random2_06.txt AC 24 ms 2944 KB
random3_01.txt AC 12 ms 2944 KB
random3_02.txt AC 20 ms 2944 KB
random4_01.txt AC 27 ms 4096 KB
random4_02.txt AC 27 ms 4096 KB
random4_03.txt AC 27 ms 4096 KB
sample_01.txt AC 2 ms 1408 KB
sample_02.txt AC 2 ms 1408 KB
sample_03.txt AC 2 ms 1408 KB
star_01.txt AC 23 ms 4096 KB
star_02.txt AC 13 ms 2944 KB