【CF】196 Div.2 D. Book of Evil

本文介绍了一种针对树结构的LCA查询算法,通过两次深度优先搜索(DFS)来解决给定节点到特定类型节点P的最大距离问题,并判断是否满足特定条件。首次DFS用于获取每个节点到P节点的最大及次大距离;第二次DFS则用于计算每个节点的有效性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

显然这个图是一课树,看着题目首先联想到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,实现比较容易。

  1 /* 337D */
  2 #include <iostream>
  3 #include <string>
  4 #include <map>
  5 #include <queue>
  6 #include <set>
  7 #include <stack>
  8 #include <vector>
  9 #include <deque>
 10 #include <algorithm>
 11 #include <cstdio>
 12 #include <cmath>
 13 #include <ctime>
 14 #include <cstring>
 15 #include <climits>
 16 #include <cctype>
 17 #include <cassert>
 18 #include <functional>
 19 #include <iterator>
 20 #include <iomanip>
 21 using namespace std;
 22 //#pragma comment(linker,"/STACK:102400000,1024000")
 23 
 24 #define sti                set<int>
 25 #define stpii            set<pair<int, int> >
 26 #define mpii            map<int,int>
 27 #define vi                vector<int>
 28 #define pii                pair<int,int>
 29 #define vpii            vector<pair<int,int> >
 30 #define rep(i, a, n)     for (int i=a;i<n;++i)
 31 #define per(i, a, n)     for (int i=n-1;i>=a;--i)
 32 #define clr                clear
 33 #define pb                 push_back
 34 #define mp                 make_pair
 35 #define fir                first
 36 #define sec                second
 37 #define all(x)             (x).begin(),(x).end()
 38 #define SZ(x)             ((int)(x).size())
 39 #define lson            l, mid, rt<<1
 40 #define rson            mid+1, r, rt<<1|1
 41 
 42 typedef struct {
 43     int mx, mx2;
 44 } node_t;
 45 
 46 const int maxn = 1e5+5;
 47 bool mark[maxn];
 48 int n, m, d;
 49 vi E[maxn];
 50 node_t nd[maxn];
 51 int ans = 0;
 52 
 53 int dfs(int u, int fa) {
 54     int i, v;
 55     int tmp;
 56     
 57     nd[u].mx = nd[u].mx2 = INT_MIN;
 58     if (mark[u])
 59         nd[u].mx = 0;
 60     for (i=0; i<SZ(E[u]); ++i) {
 61         v = E[u][i];
 62         if (v != fa) {
 63             tmp = dfs(v, u) + 1;
 64             if (tmp >= nd[u].mx) {
 65                 // find the two fathest distance from p[*] to u
 66                 nd[u].mx2 = nd[u].mx;
 67                 nd[u].mx = tmp;
 68             } else if (tmp > nd[u].mx2) {
 69                 nd[u].mx2 = tmp;
 70             }
 71         }
 72     }
 73     
 74     // -1 means no p in the path
 75     return nd[u].mx;
 76 }
 77 
 78 void dfs2(int u, int fa, int pmx) {
 79     int i, v;
 80     int tmp = max(pmx, nd[u].mx);
 81     
 82     if (tmp <= d) {
 83         ++ans;
 84     }
 85     for (i=0; i<SZ(E[u]); ++i) {
 86         v = E[u][i];
 87         if (v != fa) {
 88             if (nd[v].mx+1 == nd[u].mx)
 89                 tmp = nd[u].mx2;
 90             else
 91                 tmp = nd[u].mx;
 92             tmp = max(tmp, pmx)+1;
 93             dfs2(v, u, tmp);
 94         }
 95     }
 96 }
 97 
 98 int main() {
 99     ios::sync_with_stdio(false);
100     #ifndef ONLINE_JUDGE
101         freopen("data.in", "r", stdin);
102         freopen("data.out", "w", stdout);
103     #endif
104     
105     int u, v;
106     
107     scanf("%d %d %d", &n, &m, &d);
108     while (m--) {
109         scanf("%d", &u);
110         mark[u] = true;
111     }
112     
113     rep(i, 1, n) {
114         scanf("%d %d", &u, &v);
115         E[u].pb(v);
116         E[v].pb(u);
117     }
118     
119     // get the item in node
120     dfs(1, -1);
121     // calculate the number of valid position
122     dfs2(1, -1, INT_MIN);
123     
124     printf("%d\n", ans);
125     
126     #ifndef ONLINE_JUDGE
127         printf("time = %d.\n", (int)clock());
128     #endif
129     
130     return 0;
131 }

 

转载于:https://www.cnblogs.com/bombe1013/p/4639322.html

### 关于 Codeforces Round 1010 Div 2 的未评级题目与解答 Codeforces 平台上的比赛通常会提供详细的题目描述以及官方解法。然而,针对 Codeforces Round 1010 Div 2 的具体信息并未在当前引用中提及[^1]。值得注意的是,某些比赛可能会被标记为 unrated(未评级),这意味着该场比赛的结果不会影响参赛者的评分等级。 对于未评级的比赛问题及其解决方案,可以参考以下几点: #### 题目解析 尽管无法直接获取到 Codeforces Round 1010 Div 2 的具体内容,但可以通过类似的 CF 比赛来推测其可能涉及的主题。CF 圆桌会议中的常见主题包括但不限于字符串处理、数组操作、动态规划和图论等问题[^2]。 以下是基于一般竞赛模式下的假设性分析: 1. **字符串匹配问题** 字符串问题是许多编程比赛中常见的类型之一。例如,在某次比赛中曾出现过一个非常相似的问题:“Given a string S, find the minimum number of operations required to convert all characters into uppercase.” 这一类型的解决方法通常是通过遍历整个字符串并逐一比较字符实现。 ```python def min_operations_to_uppercase(s): count = 0 for char in s: if 'a' <= char <= 'z': count += 1 return count ``` 2. **数组排序与统计** 数组类问题也频繁出现在各类算法挑战之中。比如给定一组整数列表,要求重新排列使得偶数位于奇数之前的同时保持相对顺序不变。这类问题可通过双指针技术或者额外空间辅助完成。 3. **动态规划应用** 动态规划适用于求解最优化路径或最大子序列等相关场景。如果存在一道 DP 类型的题目,则需定义状态转移方程并通过迭代计算得出最终结果。 #### 解决方案总结 由于缺乏确切的目标赛事数据集,上述仅为通用策略展示而非特定实例解答。建议访问 Codeforces 官网查询历史存档资料以获得权威版本说明文档。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值