Hdu 4274 Spy's Work

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

2012 ACM/ICPC Asia Regional Changchun Online

题意:给定一个职员的关系,也就是一个树(根为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's Work

时间: 2024-08-05 14:50:15

Hdu 4274 Spy's Work的相关文章

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

Spy's Work Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1266    Accepted Submission(s): 388 Problem Description I'm a manager of a large trading company, called ACM, and responsible for the

HDU 4274 Spy&#39;s Work (树形DP,模拟)

题意: 给定一棵树,每个节点代表一个员工,节点编号小的级别就小,那么点1就是boss了.接下来给出对m个点的限制,有3种符号分别是op=“大于/小于/等于”,表示以第i个点为根的子树所有人的工资之和 大于/小于/等于 x,要求判断m个限制是否冲突了.注意每个员工的工资下限是1,而无上限.ps:可能出现对同个点多个限制,注意取交集. 思路: 很水的题嘛,想复杂了.注意限制是针对整棵子树的!所以我们只需要算出这棵子树的范围,再判断是否和所给的限制有冲突,如果没有冲突的话还得取“所给限制”与“计算出的

Hdu 4274 Spy&amp;#39;s Work

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

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

Spy's Work Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1266    Accepted Submission(s): 388 Problem Description I'm a manager of a large trading company, called ACM, and responsible for the

hdu 4274 2012长春赛区网络赛 树形dp ***

设定每个节点的上限和下限,之后向上更新,判断是否出现矛盾 1 #include<cstdio> 2 #include<iostream> 3 #include<algorithm> 4 #include<cstring> 5 #include<cmath> 6 #include<queue> 7 #include<map> 8 using namespace std; 9 #define MOD 1000000007 10

hdu 差分约束题集

[HDU]1384 Intervals 基础差分约束★1529 Cashier Employment 神级差分约束★★★★ 1531 King 差分约束★1534 Schedule Problem 差分约束输出一组解★3440 House Man 比较好的差分约束★★3592 World Exhibition 简单★3666 THE MATRIX PROBLEM 中等★★4274 Spy's Work [先处理出欧拉序列,然后就是差分约束了...] [POJ]1201 Intervals1275

HDU 3487:Play with Chain(Splay)

http://acm.hdu.edu.cn/showproblem.php?pid=3487 题意:有两种操作:1.Flip l r ,把 l 到 r 这段区间 reverse.2.Cut a b c ,把 a 到 b 这段区间切掉,再把这段区间接到切掉后的第 c 个数的后面. 思路:做完了上一道变态题目,做这道题目如鱼得水.Cut的时候就是把a 到 b 放到keytree的位置,记录一下当前keytree的值,然后切掉,再把切掉后的第 c 个数转到 root 的位置,再把这个记录的值重新连接回

HDU 1890:Robotic Sort(Splay)

http://acm.hdu.edu.cn/showproblem.php?pid=1890 题意:有一个无序序列,经过不断地翻转,使得最后的序列是一个升序的序列,而且如果相同数字要使在原本序列靠前的在前面.对于每一个位置,输出于哪个位置翻转. 思路:想通了之后其实挺简单的,不过挺巧妙.一开始先记录每一个位的pos,并对数组升序排序,然后我们从小到大枚举数组里的元素,对于每个元素,我们可以知道:i 就是翻转后应当在序列中的位置,a[i].pos就是它在原序列的位置.这个时候,我们只要在建树的时候

Bombing HDU, 4022(QQ糖的消法)

Bombing From:HDU, 4022 Submit Time Limit: 4000/2000 MS (Java/Others)      Memory Limit: 65768/65768 K (Java/Others) Problem Description It's a cruel war which killed millions of people and ruined series of cities. In order to stop it, let's bomb the