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. 雅图CAD

    今天培训了雅图CAD. 由辅助线确定下一步的位置,是个好思想.

  2. php.ini配置参数详解

    下面对php.ini中一些安全相关参数的配置进行说明 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 2 ...

  3. 【Luogu】P1383高级打字机

    可持久化线段树模板题之一. 权当温习主席树模板 #include<cstdio> #include<cstdlib> #include<cctype> #defin ...

  4. [BZOJ1604] [Usaco2008 Open]Cow Neighborhoods 奶牛的邻居(好题)

    传送门 良心题解 #include <set> #include <cstdio> #include <iostream> #include <algorit ...

  5. [POJ2778]DNA Sequence(AC自动机 + DP + 矩阵优化)

    传送门 AC自动机加DP就不说了 注意到 m <= 10,所以模式串很少. 而 n 很大就需要 log 的算法,很容易想到矩阵. 但是该怎么构建? 还是矩阵 A(i,j) = ∑A(i,k) * ...

  6. java面试题之什么是死锁、活锁、饿死和竞态条件?

    死锁:是指两个或两个以上的进程(或线程)在执行过程中,因争夺资源而造成的一种相互等待的现象,若无外力作用,他们将无法推进下去: 活锁:是指两个线程优先级相同,都礼让不走,就这样一直僵持下去: 饿死:在 ...

  7. mongodb的安装及环境配置

    一 下载 官网:https://fastdl.mongodb.org/win32/mongodb-win32-x86_64-2008plus-ssl-3.6.3-signed.msi 官网可能打不开, ...

  8. JSONObject与JSONArray的使用(jackson)

    1.创建一个JSONObject对象 package com.resource.controller.web; import java.util.ArrayList; import java.util ...

  9. html-Span 指定宽度

    html-Span 指定宽度 css: span{ display:-moz-inline-box; display:inline-block; width:150px; } 链接:如何设置HTML ...

  10. hanzi 全拼音 qu de

    Function pinyin(ByVal mystr As String, Optional types As Byte = 0) As StringDim temp   As String, i ...