zoukankan      html  css  js  c++  java
  • 6481: Integers on a Tree

    6481: Integers on a Tree

    时间限制: 1 Sec  内存限制: 128 MB
    提交: 69  解决: 12
    [提交][状态][讨论版][命题人:admin]

    题目描述

    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.

    Constraints
    1≤N≤105
    1≤K≤N
    1≤Ai,Bi≤N (1≤i≤N−1)
    1≤Vj≤N (1≤j≤K) (21:18, a mistake in this constraint was corrected)
    0≤Pj≤105 (1≤j≤K)
    The given graph is a tree.
    All vj are distinct.

    输入

    The input is given from Standard Input in the following format:
    N
    A1 B1
    A2 B2
    :
    AN−1 BN−1
    K
    V1 P1
    V2 P2
    :
    VK PK

    输出

    If it is possible to write integers into all empty vertices so that the condition is satisfied, print Yes. Otherwise, print No.

    样例输入

    5
    1 2
    3 1
    4 3
    3 5
    2
    2 6
    5 7
    

    样例输出

    Yes
    

    提示

    The figure below shows the tree when Takahashi fell asleep. For each vertex, the integer written beside it represents the index of the vertex, and the integer written into the vertex is the integer written by Takahashi.

    Aoki can, for example, satisfy the condition by writing integers into the remaining vertices as follows:

    来源

    ABC047&ARC063 

     

    题意:

    建了一棵树,在m个位置上标记一个数,判断是否能形成一个有边连接的两个节点之间的数值只差1 。

    思路:

    对每个节点而言,其上下限的值一定由其父想上下分别扩大 1 , 并且奇偶性与其父节点想反 ,那么我们维护每个节点的上下限值,父节点求其所有子节点的上下限的交集。

     

    #include <bits/stdc++.h>
    using namespace std;
    const int maxn=1e5+10,inf=1e9+7;
    int l[maxn],r[maxn],n,m,k,t;
    bool flag=true;
    vector<int> e[maxn];
    void dfs(int x,int fa=0){
        for (auto u:e[x]){
            if(u==fa) continue;
            dfs(u,x);
    //        printf("~%d %d
    ",x,u);
            if(r[u]==inf) continue;
            l[u]--,r[u]++;
            if(r[x]==inf){
                l[x]=l[u],r[x]=r[u];
                continue;
            }
            if((r[x]^r[u])&1){
                flag=false;
                continue;
            }
            l[x]=max(l[x],l[u]);
            r[x]=min(r[x],r[u]);
        }
        if(l[x]>r[x]) flag=false;
    }
    
    int main(){
        scanf("%d",&n);
        for (int i=1,u,v; i<n; i++){
            scanf("%d%d",&u,&v);
            e[u].push_back(v);
            e[v].push_back(u);
        }
        for (int i=1;i<=n; i++) l[i]=-inf,r[i]=inf;
        scanf("%d",&m);
        int x,y;
        while(m--){
            scanf("%d%d",&x,&y);
            l[x]=r[x]=y;
        }
        dfs(1);
        puts(flag?"Yes":"No");
        return 0;
    }
    
  • 相关阅读:
    高阶函数
    js严格模式
    改变函数内this指向方法——call、apply、bind
    vue动态组件
    微信支付接口IP获取与调用之统一下单
    node.js实现微信公众号支付
    微信支付(公众号支付JSAPI)--转载
    公众号微信支付流程-(转)
    python 3 代码一模一样,出现运行结果不同的情况(只是不以为一样而已)
    pycharm设置自动换行
  • 原文地址:https://www.cnblogs.com/acerkoo/p/9490319.html
Copyright © 2011-2022 走看看