Spy ' s workTime
limit:2000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 1324 Accepted Submission (s): 415
Problem Descriptioni ' m a manager of a large trading company, called ACM, and responsible for the market. Recently, another trading company, called ICPC, was set up suddenly. It ' s obvious that we're the competitor to each other now!
To get some information about ICPC, I had learned a lot of it. ICPC have N staffs now (numbered from 1 to N, and Boss is 1), and anybody have at the most one superior. To increase the efficiency of the whole company, the company contains N departments and the ith department are led by the I TH staff. All subordinates of the ith staff is 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 and the spy only gets some information about some departments:the sum of the salaries of staff s working for the ith department are 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 was telling a lie to us, that's to say, there would be some conflicts in the INF Ormation. So I invite your, the talented programmer, to help me check the correction of the information. Pay attention, my dear friend, each staff of ICPC would always get a salary even if it just 1 dollar!
Inputthere is multiple test cases.
The first line was an integer n. (1 <= N <= 10,000)
Each line I from 2 to N lines contains a integer x indicating the xth staff are the ith staff ' s superior (x<i).
The next line contains a integer M indicating the number of information from the spy. (1 <= M <= 10,000)
The next M lines has the form like (x < (> or =) w), indicating the sum of the Xth department are less than (more tha n or equal To) W (1 <= w <=100,000,000)
Outputfor each test case, output "True" If the information have no confliction; Otherwise output "Lie".
Sample Input
5113331 < = 42 = 2 = 25113331 > 53 =
Sample Output
Lietrue
Source2012 ACM/ICPC Asia Regional Changchun Online
Test Instructions: Given an employee's relationship, that is, a tree (root 1), and a subtree of a node as the root of the wages and conditions of employees, to determine whether these conditions are set up!
Puzzle : First build a tree, and then initialize each node can reach the interval of [1,inf], and then update according to the relationship, ' = ' in the case of the left and right intervals are updated to X, ' < ' caseThe right zone is updated to x-1, ' > ' When the left interval is updated to x+1, and finally determine whether the tree meets the conditions.
#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;///the minimum value of the left interval, initialized to 1, the right interval does not update 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);///Update 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== ' = ') {///equal to the case, the left and right intervals are X if (x<range[ith].l| | X>RANGE[ITH].R)///not in accordance with res=false; Range[ith].l=x; Range[ith].r=x; } else if (c== ' < ') {////is less than the case updated with the interval if (range[ith].l>=x) Res=false; Range[ith].r=x-1; } else {///-greater than the condition update left interval 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;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Hdu 4274 Spy ' s work