【CF】196 Div.2 D. Book of Evil
显然这个图是一课树,看着题目首先联想到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的更多相关文章
- 【CF】121 Div.1 C. Fools and Roads
题意是给定一棵树.同时,给定如下k个查询: 给出任意两点u,v,对u到v的路径所经过的边进行加计数. k个查询后,分别输出各边的计数之和. 思路利用LCA,对cnt[u]++, cnt[v]++,并对 ...
- 【CF】310 Div.1 C. Case of Chocolate
线段树的简单题目,做一个离散化,O(lgn)可以找到id.RE了一晚上,额,后来找到了原因. /* 555C */ #include <iostream> #include <str ...
- 【CF】110 Div.1 B. Suspects
这题目乍眼一看还以为是2-sat.其实很水的,O(n)就解了.枚举每个人,假设其作为凶手.观察是否满足条件.然后再对满足的数目分类讨论,进行求解. /* 156B */ #include <io ...
- 【CF】222 Div.1 B Preparing for the Contest
这样类似的题目不少,很多都是一堆优化条件求最优解,这个题的策略就是二分+贪心.对时间二分, 对费用采用贪心. /* 377B */ #include <iostream> #include ...
- 【CF】207 Div.1 B.Xenia and Hamming
这题目一看很牛逼,其实非常easy.求求最小公倍数,最大公约数,均摊复杂度其实就是O(n). /* 356B */ #include <iostream> #include <str ...
- 【CF】142 Div.1 B. Planes
SPFA.注意状态转移条件,ans的求解需要在bfs中间求解.因为只要到了地点n,则无需等待其他tourist.还是蛮简单的,注意细节. /* 229B */ #include <iostrea ...
- 【CF】223 Div.1 C Sereja and Brackets
水线段树. /* 380C */ #include <iostream> #include <string> #include <map> #include < ...
- 【CF】259 Div.1 B Little Pony and Harmony Chest
还蛮有趣的一道状态DP的题目. /* 435B */ #include <iostream> #include <string> #include <map> #i ...
- 【CF】174 Div.1 B Cow Program
思路是树形DP+状态压缩.其实仅有2个状态,奇数次来到x或者偶数次来到x.(因为对x的更新不同).同时开辟visit数组,解决环.注意,一旦遇到环结果就是-1.DP数组存放第奇数/偶数次来到x时,对y ...
随机推荐
- ElastciSearch常用APi
列出所有的索引: GET /_cat/indices?v 删除索引 DELETE /customer?pretty
- SignalR: The new old thing
As you can see, this is my first blog posted in cnblog. If you find any mistake, don’t hesitate to t ...
- C++Primer笔记一
作为一名半路出家的JAVA程序员,又要开始学半路中放弃的C++了,因为真的很重要. 先来看一段代码, #include <iostream> using namespace std; i ...
- MVC小系列(六)【无刷新的验证码】
做个无刷新的验证码功能: 第一步:首先,在公用项目中建立一个生成图片验证码的类型ValidateCode /// <summary> /// 生成验证码对象 /// </summar ...
- IrisSkin4控件使用方法
参考如下: 1. 将IrisSkin4.dll动态文件导入当前项目引用中.具体操作为:解决方案资源管理器->当前项目->引用->右键->添加引用,找到IrisSkin4.dll ...
- 有理数类 Java BigInteger实现
import java.math.BigInteger; public class Rational extends Number implements Comparable { private Bi ...
- TCP调试助手
网络开发经常要用到一些TCP&UDP的调试工具,搜集一些备用. 目前总结工具有(不分先后): chrome等自带调试器调试HTTP Fiddler(.NET)和Charles debugger ...
- entityframework多条件查询类
entityframework多条件查询类 var dataaccess = new BaseAccess(); int totalCount = 0; var paramS = new OrderM ...
- Xcode中实现ARC和MRC混编
1.在Xcode中打开项目文件 2.选中项目名称 3.在右侧选择build phass 选项卡 4.选择 complite source 选项 5.选择要支持MRC编译的.m文件,双击 6.在弹出的框 ...
- DBHelper 数据库帮助类
/// <summary> /// 数据库帮助类 /// <author>vito</author> /// </summary> public cla ...