设定每个节点的上限和下限,之后向上更新,判断是否出现矛盾

 #include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#include<map>
using namespace std;
#define MOD 1000000007
const int INF=;
const double eps=1e-;
typedef long long ll;
#define cl(a) memset(a,0,sizeof(a))
#define ts printf("*****\n");
const int N=;
int n,m,tt,tot=,head[N],dp[N];
int maxw;
bool flag;
struct edge
{
int to,next;
}edge[N*];
int up[N],low[N];
void addedge(int a,int b)
{
edge[tot].to=a;
edge[tot].next=head[b];
head[b]=tot++;
}
void init()
{
memset(head,-,sizeof(head));
tot=;
}
void dfs(int u,int pre)
{
if(flag) return;
if(up[u]!=-&&low[u]>up[u])
{
flag=;
return;
}
int le=;
int sum=;
for(int i=head[u];i!=-;i=edge[i].next)
{
int v=edge[i].to;
if(v==pre) continue;
le=;
dfs(v,u);
sum+=low[v];
}
if(!le) return;
low[u]=max(sum+,low[u]);
if(up[u]!=-&&low[u]>up[u])
{
flag=;
return;
}
}
int main()
{
int i,j,k;
#ifndef ONLINE_JUDGE
freopen("1.in","r",stdin);
#endif
while(scanf("%d",&n)!=EOF)
{
init();
for(i=;i<=n;i++)
{
int u;
scanf("%d",&u);
addedge(i,u);
addedge(u,i);
}
for(i=;i<=n;i++) up[i]=-,low[i]=;
scanf("%d",&m);
for(i=;i<m;i++)
{
int u,v,w;
char c[];
scanf("%d%s%d",&u,c,&w);
if(c[]=='<') up[u]=w-;
else if(c[]=='>') low[u]=w+;
else low[u]=up[u]=w;
}
flag=;
dfs(,-);
if(flag)
{
printf("Lie\n");
}
else printf("True\n");
}
}

hdu 4274 2012长春赛区网络赛 树形dp ***的更多相关文章

  1. hdu 4273 2012长春赛区网络赛 三维凸包中心到最近面距离 ***

    新模板 /* HDU 4273 Rescue 给一个三维凸包,求重心到表面的最短距离 模板题:三维凸包+多边形重心+点面距离 */ #include<stdio.h> #include&l ...

  2. hdu 4277 2012长春赛区网络赛 dfs+hashmap ***

    hashmap判重大法好 #include<cstdio> #include<iostream> #include<algorithm> #include<c ...

  3. hdu 4272 2012长春赛区网络赛 dfs暴力 ***

    总是T,以为要剪枝,后来发现加个map就行了 #include<cstdio> #include<iostream> #include<algorithm> #in ...

  4. hdu 4412 2012杭州赛区网络赛 期望

    虽然dp方程很好写,就是这个期望不知道怎么求,昨晚的BC也是 题目问题抽象之后为:在一个x坐标轴上有N个点,每个点上有一个概率值,可以修M个工作站, 求怎样安排这M个工作站的位置,使得这N个点都走到工 ...

  5. hdu 4411 2012杭州赛区网络赛 最小费用最大流 ***

    题意: 有 n+1 个城市编号 0..n,有 m 条无向边,在 0 城市有个警察总部,最多可以派出 k 个逮捕队伍,在1..n 每个城市有一个犯罪团伙,          每个逮捕队伍在每个城市可以选 ...

  6. hdu 4293 2012成都赛区网络赛 dp ****

    题意:有n个人,可任意分成若干组,然后每个人个各提供一个信息,表示他们组前面有多少人,后面有多少人.问最多有多少个信息是不冲突的. 将n个人看成一组区间,然后每个人的信息可以表示为该人所在组的区间,然 ...

  7. hdu 4291 2012成都赛区网络赛 矩阵快速幂 ***

    分析:假设g(g(g(n)))=g(x),x可能非常大,但是由于mod 10^9+7,所以可以求出x的循环节 求出x的循环节后,假设g(g(g(n)))=g(x)=g(g(y)),即x=g(y),y也 ...

  8. hdu 4278 2012天津赛区网络赛 数学 *

    8进制转为10进制 #include<cstdio> #include<iostream> #include<algorithm> #include<cstr ...

  9. hdu 4028 2011上海赛区网络赛H dp+map离散

    一开始用搜索直接超时,看题解会的 #include<iostream> #include<cstdio> #include<map> #include<cst ...

随机推荐

  1. Junit 测试 Spring

    在测试类上加上@RunWith,和@ContextConfiguration @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration ...

  2. JavaScript Coding 模式荟萃

    1.自运行的匿名函数 <script type="text/javascript" src="./js/jquery-1.7.2.js"></ ...

  3. HTTP Servlet 重要的几个方法

    HTTP Servlet继承了GencenServlet类    GencenServlet实现了两个接口··一个用于ServletConfig设置接口,一个为Servlet接口只要是(1) init ...

  4. poj 1011

    http://poj.org/problem?id=1011 这是一道POJ的搜索的题目,最开始确实难以理解,但做过一些搜索的题目后,也没那么难了. 大概题意就是,现在有N根木头,要拼成若干根木头,并 ...

  5. Java for LeetCode 234 Palindrome Linked List

    解题思路: O(1)的空间复杂度,意味着不能通过开一个List来解决问题.我们可以把List分成前后两个部分,后半部分通过指针的相互赋值进行翻转即可. JAVA实现如下: public static ...

  6. Django~Models2

    Generally, each model maps to a single database table. Each attribute of the model represents a data ...

  7. 设置UISegmentedControl的字体大小和颜色

    NSDictionary *dic = [NSDictionarydictionaryWithObjectsAndKeys:[UIColorblackColor],UITextAttributeTex ...

  8. 微信video标签全屏无法退出bug 本文系转载

    安卓(android)微信里面video播放视频,会被强制全屏,播放完毕后还有腾讯推荐的视频,非常讨厌..强制被全屏无法解决,但是视频播放完毕后退出播放器可以解决.方法就是视频播放完毕后,用音频aud ...

  9. 【leetcode】Merge Two Sorted Lists(easy)

    Merge two sorted linked lists and return it as a new list. The new list should be made by splicing t ...

  10. 【leetcode】Single Number II (medium) ★ 自己没做出来....

    Given an array of integers, every element appears three times except for one. Find that single one. ...