AtCoder Regular Contest 063 E - 木と整数 / Integers on a Tree

树形DP解决条件填数问题
本文介绍了一个关于在树形结构中根据特定条件填充数值的问题及其解决方案。问题要求在给定一棵树和部分顶点上的初始整数值的基础上,为剩余顶点填入整数,使得相邻顶点的数值相差恰好为1。文章通过分析提出了利用深度优先搜索(DFS)进行动态规划的方法,以判断填数方案是否存在,并给出一种可行的填数方式。

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\n",a[i]);
}

int main()
{
    work();
    return 0;
}

转载于:https://www.cnblogs.com/Hxymmm/p/7794813.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值