显然这个图是一课树,看着题目首先联想到LCA(肯定是可以解的)。但是看了一下数据大小,应该会TLE。
然后,忽然想到一个前面做过的题目,大概是在一定条件下树中某结点旋转成为根后查询最长路径。
结果灵感就来了,主要思路是对于每个结点,第一次dfs得到两个变量到P结点的最大值以及次大值。
然后,第二次dfs对于当前结点u,u到它的子树中P类结点的最大距离已知(nd[u].mx),那么除u的其他结点v到P类结点的最大距离加上v到u的距离和的最大值为pmx,可以通过每次深搜计算出来,只要d大于等于两者的最大值就为有效结点。而pmx的求法也很容易,对于u来说pmx可能为其父亲的pmx+1,或者为v的兄弟结点的mx值。
刚好因为我们已知每个结点的最大值以及次大值,所有兄弟结点的最大值可求。直接用INT_MIN,实现比较容易。

 /* 337D */
#include <iostream>
#include <string>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#include <deque>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <cstring>
#include <climits>
#include <cctype>
#include <cassert>
#include <functional>
#include <iterator>
#include <iomanip>
using namespace std;
//#pragma comment(linker,"/STACK:102400000,1024000") #define sti set<int>
#define stpii set<pair<int, int> >
#define mpii map<int,int>
#define vi vector<int>
#define pii pair<int,int>
#define vpii vector<pair<int,int> >
#define rep(i, a, n) for (int i=a;i<n;++i)
#define per(i, a, n) for (int i=n-1;i>=a;--i)
#define clr clear
#define pb push_back
#define mp make_pair
#define fir first
#define sec second
#define all(x) (x).begin(),(x).end()
#define SZ(x) ((int)(x).size())
#define lson l, mid, rt<<1
#define rson mid+1, r, rt<<1|1 typedef struct {
int mx, mx2;
} node_t; const int maxn = 1e5+;
bool mark[maxn];
int n, m, d;
vi E[maxn];
node_t nd[maxn];
int ans = ; int dfs(int u, int fa) {
int i, v;
int tmp; nd[u].mx = nd[u].mx2 = INT_MIN;
if (mark[u])
nd[u].mx = ;
for (i=; i<SZ(E[u]); ++i) {
v = E[u][i];
if (v != fa) {
tmp = dfs(v, u) + ;
if (tmp >= nd[u].mx) {
// find the two fathest distance from p[*] to u
nd[u].mx2 = nd[u].mx;
nd[u].mx = tmp;
} else if (tmp > nd[u].mx2) {
nd[u].mx2 = tmp;
}
}
} // -1 means no p in the path
return nd[u].mx;
} void dfs2(int u, int fa, int pmx) {
int i, v;
int tmp = max(pmx, nd[u].mx); if (tmp <= d) {
++ans;
}
for (i=; i<SZ(E[u]); ++i) {
v = E[u][i];
if (v != fa) {
if (nd[v].mx+ == nd[u].mx)
tmp = nd[u].mx2;
else
tmp = nd[u].mx;
tmp = max(tmp, pmx)+;
dfs2(v, u, tmp);
}
}
} int main() {
ios::sync_with_stdio(false);
#ifndef ONLINE_JUDGE
freopen("data.in", "r", stdin);
freopen("data.out", "w", stdout);
#endif int u, v; scanf("%d %d %d", &n, &m, &d);
while (m--) {
scanf("%d", &u);
mark[u] = true;
} rep(i, , n) {
scanf("%d %d", &u, &v);
E[u].pb(v);
E[v].pb(u);
} // get the item in node
dfs(, -);
// calculate the number of valid position
dfs2(, -, INT_MIN); printf("%d\n", ans); #ifndef ONLINE_JUDGE
printf("time = %d.\n", (int)clock());
#endif return ;
}

【CF】196 Div.2 D. Book of Evil的更多相关文章

  1. 【CF】121 Div.1 C. Fools and Roads

    题意是给定一棵树.同时,给定如下k个查询: 给出任意两点u,v,对u到v的路径所经过的边进行加计数. k个查询后,分别输出各边的计数之和. 思路利用LCA,对cnt[u]++, cnt[v]++,并对 ...

  2. 【CF】310 Div.1 C. Case of Chocolate

    线段树的简单题目,做一个离散化,O(lgn)可以找到id.RE了一晚上,额,后来找到了原因. /* 555C */ #include <iostream> #include <str ...

  3. 【CF】110 Div.1 B. Suspects

    这题目乍眼一看还以为是2-sat.其实很水的,O(n)就解了.枚举每个人,假设其作为凶手.观察是否满足条件.然后再对满足的数目分类讨论,进行求解. /* 156B */ #include <io ...

  4. 【CF】222 Div.1 B Preparing for the Contest

    这样类似的题目不少,很多都是一堆优化条件求最优解,这个题的策略就是二分+贪心.对时间二分, 对费用采用贪心. /* 377B */ #include <iostream> #include ...

  5. 【CF】207 Div.1 B.Xenia and Hamming

    这题目一看很牛逼,其实非常easy.求求最小公倍数,最大公约数,均摊复杂度其实就是O(n). /* 356B */ #include <iostream> #include <str ...

  6. 【CF】142 Div.1 B. Planes

    SPFA.注意状态转移条件,ans的求解需要在bfs中间求解.因为只要到了地点n,则无需等待其他tourist.还是蛮简单的,注意细节. /* 229B */ #include <iostrea ...

  7. 【CF】223 Div.1 C Sereja and Brackets

    水线段树. /* 380C */ #include <iostream> #include <string> #include <map> #include < ...

  8. 【CF】259 Div.1 B Little Pony and Harmony Chest

    还蛮有趣的一道状态DP的题目. /* 435B */ #include <iostream> #include <string> #include <map> #i ...

  9. 【CF】174 Div.1 B Cow Program

    思路是树形DP+状态压缩.其实仅有2个状态,奇数次来到x或者偶数次来到x.(因为对x的更新不同).同时开辟visit数组,解决环.注意,一旦遇到环结果就是-1.DP数组存放第奇数/偶数次来到x时,对y ...

随机推荐

  1. Android中log4j的运用

    网上一查关于android上面运用Log4j的运用,各种说需要添加多样的包的,照着log4j的官网教程看了下,给了个简单的输出到console上面的代码,似乎没什么用.看网上关于Log4j更多是在ja ...

  2. Canvas实现曲线运动

    前言 Html5添加的最受欢迎的功能就是<canvas>元素,它负责在页面中设定一个区域,然后在里面可以通过javascript动态地在其内绘制图形. 主流浏览器,IE9+,手机端都是支持 ...

  3. ssh(Struts2+hibernate+spring)简单分页

    实体类+实体映射+entity(pagebean)+dao层+service层+action层

  4. (五)Hibernate 操作对象

    所有项目导入对应的hibernate的jar包.mysql的jar包和添加每次都需要用到的HibernateUtil.java 第一节:Hibernate 中四种对象状态 临时状态(transient ...

  5. 01_根据Id查询User的数据

    [工程目录] [数据库表中内容 user表] [sqlMapConfig.xml配置文件主要内容] 简述:sqlMapConfig.xml配置文件主要有两个作用: 1.配置和数据连接的相关信息,例如事 ...

  6. HDU 2502 月之数(简单递推)

    月之数 Problem Description 当寒月还在读大一的时候,他在一本武林秘籍中(据后来考证,估计是计算机基础,狂汗-ing),发现了神奇的二进制数.如果一个正整数m表示成二进制,它的位数为 ...

  7. zabbix 配置

    终于把zabbix配置好了.可能还有待优化   我主要参考了几个链接 http://lnmp.org/install.html   一键安装lnmp http://blog.unix178.com/2 ...

  8. MVC中Razor视图基本语法(1)

    Razor前面,必须要跟前面的有空隙,即空格(多谢一楼提醒,url里面确实不用空格,如果要在url里面只需要@(ViewBag.),加上括号就好了),之后的必须要连贯,否则加小括号 1,在页面中输出单 ...

  9. html5生成柱状图(条形图)

    <html> <canvas id="a_canvas" width="1000" height="700">< ...

  10. 使用远程链接数据库工具无法链接到 linxu 系统上的数据库配置 1045

    1.远程连接上Linux系统,确保Linux系统已经安装上了MySQL数据库.登陆数据库.mysql -uroot -p(密码). 2. 创建用户用来远程连接 GRANT ALL PRIVILEGES ...