HDU 4274 Spy's Work (dfs)

每一个节点维护一个下限和一个上限,初始化为1和inf。根据输入的大小关系,可以相应更改该点的上下限。比如输入s<e,则点s的上限修改为e-1,若s>e,则点s的下限修改为e+1,若s=e,则是上下限都为e。然后判断是否下限大于上限,若是,则说明是lie了。考虑到父亲节点的上限是不受孩子的影响的,所以dfs的时候只需根据孩子的下限来修改父亲的下限,每次返回到当前点的时候,判断是否出现下限大于上限的情况,若有,则标记。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define SIZE 10001

using namespace std;

typedef __int64 Int;
const Int inf = (Int)1<<62;

struct node
{
    int to,next;
}edge[SIZE<<2];

int head[SIZE],idx;
int n,m;
Int up[SIZE],down[SIZE];
bool judge;

void addnode(int from,int to)
{
    edge[idx].to = to;
    edge[idx].next = head[from];
    head[from] = idx++;
}

void check(int rt,Int u,Int d)
{
    Int uu = min(u,up[rt]);
    Int dd = max(d,down[rt]);
    if(dd > uu)
        judge = true;
    up[rt] = uu;
    down[rt] = dd;
}

void dfs(int wh)
{
    if(judge)
        return;
    Int d = 1;
    for(int i=head[wh]; i!=-1; i=edge[i].next)
    {
        int to = edge[i].to;
        dfs(to);
        d += down[to];
    }
    check(wh,inf,d);
}

int main()
{
    while(~scanf("%d",&n))
    {
        idx = 0;
        memset(head,-1,sizeof(head));
        int wh;
        for(int i=2; i<=n; i++)
        {
            scanf("%d",&wh);
            addnode(wh,i);
        }
        scanf("%d",&m);
        for(int i=1; i<=n; i++)
            up[i] = inf,down[i] = 1;
        Int s,e;
        char op[5];
        judge = false;
        Int u,d;
        for(int i=1; i<=m; i++)
        {
            scanf("%I64d %s %I64d",&s,op,&e);
            u = inf, d = 1;
            if(op[0] == '<')u = e-1;
            else if(op[0] == '>')d = e+1;
            else u = d = e;
            check(s,u,d);
        }
        dfs(1);
        if(judge)puts("Lie");
        else puts("True");
    }
    return 0;
}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值