zoukankan      html  css  js  c++  java
  • AtCoder Regular Contest 063 E

    Problem Statement

    We have a tree with N vertices. The vertices are numbered 1,2,…,N. The i-th (1≦i≦N−1) edge connects the two vertices Ai and Bi.
    Takahashi wrote integers into K of the vertices. Specifically, for each 1≦j≦K, he wrote the integer Pj into vertex Vj. The remaining vertices are left empty. After that, he got tired and fell asleep.
    Then, Aoki appeared. He is trying to surprise Takahashi by writing integers into all empty vertices so that the following condition is satisfied:
    Condition: For any two vertices directly connected by an edge, the integers written into these vertices differ by exactly 1.
    Determine if it is possible to write integers into all empty vertices so that the condition is satisfied. If the answer is positive, find one specific way to satisfy the condition.

    solution

    我们首先根据奇偶性判掉一些矛盾,然后我们从已知点可以得到一个点的取值范围,即 $$L[x]=Max(L[x],L[son]-1)$$ $$R[x]=Min(R[x],R[son]+1)$$,我们判一下每个点是否都有取值,判断矛盾之后就可以在取值范围内任意选取了,任意输出一组答案即可

    #include <algorithm>
    #include <iostream>
    #include <cstdlib>
    #include <cstring>
    #include <cstdio>
    #include <cmath>
    #define RG register
    #define il inline
    #define iter iterator
    #define Max(a,b) ((a)>(b)?(a):(b))
    #define Min(a,b) ((a)<(b)?(a):(b))
    using namespace std;
    typedef long long ll;
    const int N=100005;
    int n,a[N],head[N],nxt[N<<1],to[N<<1],num=0;bool v[N],flag=1,mark[N];
    il void link(int x,int y){nxt[++num]=head[x];to[num]=y;head[x]=num;}
    il void dfs(int x,int last){
    	if(mark[x] && (v[x]^(a[x]&1))){flag=0;return ;}
    	for(int i=head[x];i;i=nxt[i]){
    		int u=to[i];if(u==last)continue;
    		v[u]=v[x]^1;dfs(u,x);
    	}
    }
    
    int L[N],R[N];
    il void dfs2(int x,int last){
    	if(mark[x])L[x]=R[x]=a[x];
    	else L[x]=-2e8,R[x]=2e8;
    	for(int i=head[x];i;i=nxt[i]){
    		int u=to[i];if(u==last)continue;
    		dfs2(u,x);
    		L[x]=Max(L[x],L[u]-1);
    		R[x]=Min(R[x],R[u]+1);
    	}
    	if(L[x]>R[x])flag=0;
    }
    
    il void dfs3(int x,int last){
    	for(int i=head[x];i;i=nxt[i]){
    		int u=to[i];if(u==last)continue;
    		a[u]=a[x]+(a[x]+1<=R[u]?1:-1);
    		dfs3(u,x);
    	}
    }
    
    void work()
    {
    	int x,y;
    	scanf("%d",&n);
    	for(int i=1;i<n;i++){
    		scanf("%d%d",&x,&y);
    		link(x,y);link(y,x);
    	}
    	int Q;cin>>Q;
    	for(int i=1;i<=Q;i++){
    		scanf("%d%d",&x,&y);
    		a[x]=y;mark[x]=1;
    	}
    	for(int i=1;i<=n;i++){
    		if(!mark[i])continue;
    		v[i]=a[i]&1;dfs(i,i);
    		break;
    	}
    	if(!flag){puts("No");return ;}
    	dfs2(1,1);
    	if(!flag){puts("No");return ;}
    	L[1]+=((L[1]&1)^v[1]);
    	a[1]=L[1];
    	dfs3(1,1);
    	puts("Yes");
    	for(int i=1;i<=n;i++)printf("%d
    ",a[i]);
    }
    
    int main()
    {
    	work();
    	return 0;
    }
    
    
  • 相关阅读:
    75.Java异常处理机制-自定义异常
    75.Java异常处理机制-手动抛出异常
    75.Java异常处理机制throws
    mybatis的xml文件中如何处理大小于号
    JS 拼装代码的HTML onClick方法传递字符串
    Java 日期往后推迟n天
    MySql 去重且指定某字段在前的排序方法
    java运行内存分配图(转)
    Java中正则Matcher类的matches()、lookAt()和find()的区别<转>
    图片在父元素中上下居中(vertical-align的有效性)
  • 原文地址:https://www.cnblogs.com/Hxymmm/p/7794813.html
Copyright © 2011-2022 走看看