训练赛时题目都读错了 以为符号两侧是两个不同部门,以为要用并查集之类的方法解决矛盾判断,结果右边是数值,其实初始化完上下界之后,dfs一遍。一边更新每一个部门下界在判断矛盾就可以了。
Spy's Work
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 802 Accepted Submission(s): 252
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!
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)
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
#include<iostream>
#include<cstring>
#include<string>
#include<cstdio>
using namespace std;
#define MAXN 11000
#define INF 0x3fff3f3f
struct edges
{
int v,next;
}edge[22000];
int n;
int vis[MAXN],head[MAXN],en;
int l[MAXN],u[MAXN];
void add(int u,int v)
{
edge[en].v=v;
edge[en].next=head[u];
head[u]=en++;
edge[en].v=u;
edge[en].next=head[v];
head[v]=en++;
}
bool dfs(int x)
{
vis[x]=1;
if(l[x]>u[x]) return false;
int cnt=0,low=0;
for(int i=head[x];i!=-1;i=edge[i].next)
{
int to=edge[i].v;
if(vis[to]) continue;
cnt++;
if(!dfs(to)) return false;
low+=l[to];
}
if(cnt==0) return true;
l[x]=max(l[x],low+1);
if(l[x]>u[x]) return false;
return true;
}
int main()
{
int x,y,m;
char op[8];
while(~scanf("%d",&n))
{
memset(head,-1,sizeof(head));en=0;
for(int i=2;i<=n;i++)
{
scanf("%d",&x);
add(x,i);
}
for(int i=1;i<=n;i++)
{
l[i]=1;
u[i]=INF;
}
scanf("%d",&m);
for(int i=0;i<m;i++)
{
scanf("%d%s%d",&x,op,&y);
if(op[0]=='<') u[x]=y-1;
else if(op[0]=='>') l[x]=y+1;
else l[x]=u[x]=y;
}
memset(vis,0,sizeof(vis));
if(dfs(1)) printf("True\n");
else printf("Lie\n");
}
return 0;
}