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;
    }
    
    
  • 相关阅读:
    剑指 Offer 67. 把字符串转换成整数 && Leetcode 8 字符串转换整数 (atoi)
    剑指 Offer 49. 丑数 && Leetcode 264. 丑数 II
    [LeetCode] Implement strStr()
    [LeetCode] Rotate Image
    [LeetCode] Remove Duplicates from Sorted List II
    [LeetCode] Gas Station
    OpenCV-paper detection & perspective transformation 相关资料
    Install PIL on mac osX10.9
    为什么是 n(n+1)/2 ?
    数组排序
  • 原文地址:https://www.cnblogs.com/Hxymmm/p/7794813.html
Copyright © 2011-2022 走看看