Spy's Work

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 1324    Accepted Submission(s): 415

Problem Description
I'm a manager of a large trading company, called ACM, and responsible for the market research. Recently, another trading company, called ICPC, is set up suddenly. It's obvious that we are the competitor to each other now!

To get some information about ICPC, I have learned a lot about it. ICPC has N staffs now (numbered from 1 to N, and boss is 1), and anybody has at most one superior. To increase the efficiency of the whole company, the company contains N departments and the
ith department is led by the ith staff. All subordinates of the ith staff are also belong to the ith department.

Last week, we hire a spy stealing into ICPC to get some information about salaries of staffs. Not getting the detail about each one, the spy only gets some information about some departments: the sum of the salaries of staff s working for the ith department
is less than (more than or equal to) w. Although the some inaccurate information, we can also get some important intelligence from it.

Now I only concerned about whether the spy is telling a lie to us, that is to say, there will be some conflicts in the information. So I invite you, the talented programmer, to help me check the correction of the information. Pay attention, my dear friend,
each staff of ICPC will always get a salary even if it just 1 dollar!
 
Input
There are multiple test cases.

The first line is an integer N. (1 <= N <= 10,000)

Each line i from 2 to N lines contains an integer x indicating the xth staff is the ith staff's superior(x<i).

The next line contains an integer M indicating the number of information from spy. (1 <= M <= 10,000)

The next M lines have the form like (x < (> or =) w), indicating the sum of the xth department is less than(more than or equal to) w (1 <= w <=100,000,000)
 
Output
For each test case, output "True" if the information has no confliction; otherwise output "Lie".
 
Sample Input
5
1
1
3
3
3
1 < 6
3 = 4
2 = 2 5
1
1
3
3
3
1 > 5
3 = 4
2 = 2
 
Sample Output
Lie
True
 
Source
 

题意:给定一个职员的关系,也就是一个树(根为1),和以某一个节点为根的子树员工的工资和的条件,推断这些条件是否都成立!

题解:先建一棵树,然后初始化每一个节点的所能达到的区间为[1,INF],再依据所给的关系更新。‘=’的情况就把左右区间都更新为x,'<'的情况
就把右区间更新为x-1。‘>'时吧左区间更新为x+1。最后推断这棵树是否符合条件。

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <string>
#include <map>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <set>
#include <queue> using namespace std; typedef long long ll;
const ll INF=1e16;
const int maxn=1e4+5; struct T {
ll l;
ll r;
};
T range[maxn];
vector<int> g[maxn]; int n; void inti() {
for(int i=1; i<=n; i++) {
g[i].clear();
range[i].l=1;
range[i].r=INF;
}
} bool dfs(int u) {
if(g[u].size()==0)
return true;
ll l=1;///左区间的最小值,初始化为1,右区间不用更新
for(int i=0; i<g[u].size(); i++) {
int v=g[u][i];
bool res=dfs(v);
if(res==false)
return false;
l+=range[v].l;
if(l>range[u].r)
return false;
}
range[u].l=max(range[u].l,l);///更新
return true;
} int main() {
while(~scanf("%d",&n)) {
inti();
for(int i=2; i<=n; i++) {
int x;
scanf("%d",&x);
g[x].push_back(i);
}
int m;
scanf("%d",&m);
bool res=true;
for(int i=1; i<=m; i++) {
int ith,x;
char c;
scanf("%d %c %d",&ith,&c,&x);
if(c=='=') { ///等于的情况。左右区间都为x
if(x<range[ith].l||x>range[ith].r)///不符合
res=false;
range[ith].l=x;
range[ith].r=x;
} else if(c=='<') { ///小于的情况更新有区间
if(range[ith].l>=x)
res=false;
range[ith].r=x-1;
} else { ///大于的情况更新左区间
if(range[ith].r<=x)
res=false;
range[ith].l=x+1;
}
}
if(res) {
res=dfs(1);
}
if(res==true)
puts("True");
else
puts("Lie");
}
return 0;
}

Hdu 4274 Spy&#39;s Work的更多相关文章

  1. hdu 4274 Spy&#39;s Work(水题)

    Spy's Work Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  2. HDU 4274 Spy's Work (树 DFS)

    给定N个点,每个点都有一个唯一的前驱结点(点1为大boss),每个点的实际权值是子节点的求和值.现在给出某些点的权值的估算(> , = , < ),问这些估算是否会有冲突,现在保证每个点的 ...

  3. HDU 4274 Spy's Work (树形DP)

    题意 给定一棵树,给出一些子树的权值关系,问是否矛盾(初始所有结点的下限为1) 思路 设lmin和lmax表示题目给定的限制范围,默认为[1..oo]:amin和amax表示实际符合要求的范围.从根节 ...

  4. HDU 4274 Spy's Work (树形DP,模拟)

    题意: 给定一棵树,每个节点代表一个员工,节点编号小的级别就小,那么点1就是boss了.接下来给出对m个点的限制,有3种符号分别是op=“大于/小于/等于”,表示以第i个点为根的子树所有人的工资之和  ...

  5. hdu 3966 Aragorn&#39;s Story(树链剖分+树状数组)

    pid=3966" target="_blank" style="">题目链接:hdu 3966 Aragorn's Story 题目大意:给定 ...

  6. HDU 3966 Aragorn&#39;s Story(树链剖分)

    HDU Aragorn's Story 题目链接 树抛入门裸题,这题是区间改动单点查询,于是套树状数组就OK了 代码: #include <cstdio> #include <cst ...

  7. hdu 5282 Senior&#39;s String 两次dp

    题链:http://acm.hdu.edu.cn/showproblem.php?pid=5282 Senior's String Time Limit: 2000/1000 MS (Java/Oth ...

  8. HDU 3177 Crixalis&#39;s Equipment(贪婪)

    主题链接:http://acm.hdu.edu.cn/showproblem.php? pid=3177 Problem Description Crixalis - Sand King used t ...

  9. HDU - 5186 - zhx&#39;s submissions (精密塔尔苏斯)

    zhx's submissions Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others ...

随机推荐

  1. enq: TX - row lock contention“等待事件的处理

      enq: TX - row lock contention“等待事件的处理   session1: SQL> conn scott/triger Connected. SQL> CRE ...

  2. python 字典 key 和value 互换

    这里是一个可能有用的通过字典解析实现的小技巧: 交换字典的键和值. >>> a_dict = {'a': 1, 'b': 2, 'c': 3} >>> {value ...

  3. [luoguP2324] [SCOI2005]骑士精神(A*?)

    传送门 蒟蒻并不懂A*是什么,但是题解里有个Astar 可以看出,当前棋盘和最终的棋盘如果有k个不同的,那么至少需要k-1步来移动 所以如果 当前步数 + k - 1 > limit 就直接退出 ...

  4. P1140 相似基因 (动态规划)

    题目背景 大家都知道,基因可以看作一个碱基对序列.它包含了4种核苷酸,简记作A,C,G,T.生物学家正致力于寻找人类基因的功能,以利用于诊断疾病和发明药物. 在一个人类基因工作组的任务中,生物学家研究 ...

  5. 【2018.10.2】Note of CXM

    1.有一张无向图,现在要给每个点染上黑色或白色,最后每个点的染色代价是它与离这个点最近的不同色节点的距离.求最小代价.所有边权$\geq 0$且互不相同. 分三种情况: 两点都染了色:两点都跟其它点算 ...

  6. C++ 中的 C_str() 函数用法

    转中转 ~\(≧▽≦)/~ :http://blog.csdn.net/nancy_m/article/details/7583550 语法: const char *c_str(); c_str() ...

  7. uva 10710 快速幂取模

    //题目大意:输入一个n值问洗牌n-1次后是不是会变成初始状态(Jimmy-number),从案例可看出牌1的位置变化为2^i%n,所以最终判断2^(n-1)=1(mod n)是否成立#include ...

  8. spring-Ioc容器与bean

    1. spring 的beanFactory容器 bean.xml  HelloWorld实体类与spring教程学习笔记1相同 public static void main(String[] ar ...

  9. hdu 4951

    Multiplication table Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Oth ...

  10. android控件-images

    1.imageButton 图片按钮 <ImageButton android:id="@+id/imageButton" android:layout_width=&quo ...