HDU 4274 Spy's Work (12 years Changchun tree DP)

Source: Internet
Author: User

Question: Give a tree, give the weight relationship of some sub-trees, and ask if there is a conflict

 


Initially, the lower limit of all nodes and subtree is 1, and the upper limit may be unknown.

Then, update the upper and lower bounds through the given inequality, then perform tree-like DP, and update the lower bounds of the parent node through the lower bounds of the child.

Determine if there are any conflicts

Note that the minimum weight of the current node is 1, and the lower limit of the node is the sum of + 1


[Cpp]
# Include <iostream>
# Include <cstdio>
# Include <map>
# Include <cstring>
# Include <cmath>
# Include <vector>
# Include <algorithm>
# Include <set>
# Define inf 110000000
# Define M 10005
# Define N 10005
# Define Min (a, B) (a) <(B )? (A) (B ))
# Define Max (a, B) (a)> (B )? (A) (B ))
# Define pb (a) push_back ()
# Define mem (a, B) memset (a, B, sizeof ())
# Define LL long
# Define MOD 1000000007.
Using namespace std;
Struct Node {
Int v, next;
} Edge [N * 2];
Int n, start [N], tot;
Int up [N], low [N];
Void addedge (int u, int v ){
Edge [tot]. v = v;
Edge [tot]. next = start [u];
Start [u] = tot ++;
}
Void _ addedge (int u, int v ){
Addedge (u, v );
Addedge (v, u );
}
Bool ans;
Void dfs (int u, int pre ){
If (ans) return;
If (up [u]! =-1 & low [u]> up [u]) {ans = true; return ;}
Int tmp = 0;
Int leaf = 1;
For (int I = start [u]; I! =-1; I = edge [I]. next ){
Int v = edge [I]. v;
If (v = pre) continue;
Dfs (v, u );
Leaf = 0;
Tmp + = low [v];
}
If (leaf) return;
Low [u] = max (tmp + 1, low [u]);
If (up [u]! =-1 & low [u]> up [u]) ans = true;
}
Int main (){
While (scanf ("% d", & n )! = EOF ){
Tot = 0; mem (start,-1 );
For (int I = 2; I <= n; I ++ ){
Int u;
Scanf ("% d", & u );
_ Addedge (u, I );
}
Int m;
For (int I = 1; I <= n; I ++) {low [I] = 1; up [I] =-1 ;}
Scanf ("% d", & m );
For (int I = 0; I <m; I ++ ){
Int u, w;
Char str [5];
Scanf ("% d % s % d", & u, str, & w );
If (str [0] = '<') up [u] = W-1;
Else if (str [0] = '>') low [u] = w + 1;
Else low [u] = up [u] = w;
}
Ans = false;
Dfs (1, 0 );
Puts (ans? "Lie": "True ");
}
Return 0;
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.