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
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;
}