HDU 4274 Spy's Work (树形DP,模拟)

题意: 

  给定一棵树,每个节点代表一个员工,节点编号小的级别就小,那么点1就是boss了。接下来给出对m个点的限制,有3种符号分别是op=“大于/小于/等于”,表示以第i个点为根的子树所有人的工资之和 大于/小于/等于 x,要求判断m个限制是否冲突了。注意每个员工的工资下限是1,而无上限。ps:可能出现对同个点多个限制,注意取交集。

思路:

  很水的题嘛,想复杂了。注意限制是针对整棵子树的!所以我们只需要算出这棵子树的范围,再判断是否和所给的限制有冲突,如果没有冲突的话还得取“所给限制”与“计算出的范围”的交集。在输入m个限制的时候注意可能已经冲突了,需先提前判断一下。注意可能需要用longlong,初始化时就将每个人的范围限制在[1,INF),这样就完事了~

  1 //#include <bits/stdc++.h>
  2 #include <iostream>
  3 #include <cstdio>
  4 #include <cstring>
  5 #include <cmath>
  6 #include <map>
  7 #include <deque>
  8 #include <algorithm>
  9 #include <vector>
 10 #include <iostream>
 11 #define pii pair<int,int>
 12 #define max(x,y) ((x)>(y)?(x):(y))
 13 #define min(x,y) ((x)<(y)?(x):(y))
 14 #define INF 0x7f7f7f7f
 15 #define LL  long long
 16 using namespace std;
 17 const double PI  = acos(-1.0);
 18 const int N=10100;
 19 bool isWA;
 20 int n, edge_cnt, head[N];
 21 LL down[N], up[N];
 22 struct node
 23 {
 24     int from, to, next;
 25     node(){};
 26     node(int from,int to,int next):from(from),to(to),next(next){};
 27 }edge[N];
 28
 29 void add_node(int from,int to)
 30 {
 31     edge[edge_cnt]=node(from,to,head[from]);
 32     head[from]=edge_cnt++;
 33 }
 34
 35 bool DFS(int t)
 36 {
 37     node e;
 38     LL L=1, R=INF;        //计算此子树的范围
 39     for(int i=head[t]; i!=-1; i=e.next)
 40     {
 41         e=edge[i];
 42         if(DFS(e.to)==false)    return false;
 43         L+=down[e.to];
 44     }
 45
 46     if(head[t]==-1)    //叶子,只要在[1,INF)都是合法的
 47     {
 48         if(up[t]>0)  return true;
 49         else         return false;
 50     }
 51     else
 52     {
 53         if( R<down[t] || L>up[t]  )    return false;    //无交集
 54         down[t]=max(down[t], L);        //更新本子树的范围
 55         up[t]=min(up[t], R);
 56         return true;
 57     }
 58 }
 59
 60
 61 void init(int n)
 62 {
 63     for(int i=0; i<=n; i++) //注意初始化问题
 64         down[i]=1,
 65         up[i]=INF,
 66         head[i]=-1;
 67     isWA=false;
 68     edge_cnt=0;
 69 }
 70
 71 int main()
 72 {
 73     //freopen("input.txt", "r", stdin);
 74     int n, m, a, d;char b,c;
 75     while(~scanf("%d",&n))
 76     {
 77         init(n);
 78         for(int i=2; i<=n; i++)
 79         {
 80             scanf("%d",&a);
 81             add_node(a,i);
 82         }
 83         scanf("%d",&m);
 84
 85         for(int i=0,L,R; i<m; i++)
 86         {
 87             scanf("%d%c%c%d",&a,&b,&c,&d);
 88             L=1, R=INF;
 89             if(c==‘<‘)         R=d-1;
 90             else if(c==‘>‘)    L=d+1;
 91             else               L=R=d;
 92             if(L>up[a] || R<down[a])  isWA=true;//所给条件可能已经冲突
 93             down[a]=L, up[a]=R;
 94         }
 95         if(!isWA && DFS(1))  puts("True");
 96         else                 puts("Lie");
 97     }
 98
 99     return 0;
100 }

AC代码

HDU 4274 Spy's Work (树形DP,模拟)

时间: 2024-10-25 07:40:56

HDU 4274 Spy's Work (树形DP,模拟)的相关文章

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

Bestcoder round #65 &amp;&amp; hdu 5593 ZYB&#39;s Tree 树形dp

Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 354    Accepted Submission(s): 100 Problem Description ZYB has a tree with N nodes,now he wants you to solve the numbers of nodes distanced no m

hdu 4514 并查集+树形dp

湫湫系列故事——设计风景线 Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total Submission(s): 4539    Accepted Submission(s): 816 Problem Description 随着杭州西湖的知名度的进一步提升,园林规划专家湫湫希望设计出一条新的经典观光线路,根据老板马小腾的指示,新的风景线最好能建成环形,如果没有条件建成环形,

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): 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&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 4118 Holiday&#39;s Accommodation 树形dp

Holiday's Accommodation Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=4118 Description Nowadays, people have many ways to save money on accommodation when they are on vacation.One of these ways is exchanging

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 5379 Mahjong tree(树形dp)

题目链接:hdu 5379 Mahjong tree 树形dp,每个节点最多有2个子节点为一棵节点数大于1的子树的根节点,而且要么后代的节点值都大于,要么都小于本身(所以tson不为0是,要乘2).对于K个单一节点的子节点,种类数即为全排K!.当一个节点没有兄弟节点时,以这个节点为根结点的子树,根可以选择最大或者最小. #pragma comment(linker, "/STACK:102400000,102400000") #include <cstdio> #inclu

HDU 5758 Explorer Bo(树形DP)

[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5758 [题目大意] 给出一棵树,每条路长度为1,允许从一个节点传送到任意一个节点,现在要求在传送次数尽量少的情况下至少经过每条路一遍啊,同时最小化走过的路程总长度.输出路程总长度. [题解] 首先,对于传送次数尽量少这个条件,我们很容易发现,当且仅当每次出发点和终止点都是叶节点的时候,是最少的,当然在叶节点无法两两匹配的时候,再多走一条链. 然后就是叶节点的匹配问题,使得匹配后的叶节点连线覆盖全