arc205d - Non-Ancestor Matching

Problem

Problem Statement

You are given a rooted tree with N N N vertices numbered from 1 1 1 to N N N. Vertex 1 1 1 is the root, and the parent of vertex i i i ( 2 ≤ i ≤ N 2 \le i \le N 2≤i≤N) is vertex p i p_i pi ( 1 ≤ p i < i 1 \le p_i < i 1≤pi<i). Initially, all vertices are colored white.

You can perform the following sequence of operations zero or more times.

  • Choose a pair of integers ( u , v ) (u, v) (u,v) that satisfies all of the following conditions.

    • 1 ≤ u < v ≤ N 1 \le u < v \le N 1≤u<v≤N
    • Both vertices u u u and v v v are colored white.
    • u u u is not an ancestor of v v v.
  • Color vertices u u u and v v v black.

Here, " u u u is not an ancestor of v v v" means that you cannot reach vertex u u u from vertex v v v no matter how many times you perform the operation of moving to the parent of the current vertex.

Find the maximum number of operations you can perform when you perform operations in an appropriate order.

You are given T T T test cases, so find the answer for each of them.

Constraints

  • 1 ≤ T ≤ 5 × 10 4 1 \le T \le 5 \times 10^4 1≤T≤5×104
  • 2 ≤ N ≤ 5 × 10 5 2 \le N \le 5 \times 10^5 2≤N≤5×105
  • 1 ≤ p i < i 1 \le p_i < i 1≤pi<i
  • The sum of N N N over all test cases is at most 5 × 10 5 5 \times 10^5 5×105.
  • All input values are integers.

Translation

题目描述

给你一棵拥有 N N N 个节点的有根树,节点编号为 1 1 1 到 N N N。节点 1 1 1 是根节点,节点 i i i ( 2 ≤ i ≤ N 2 \le i \le N 2≤i≤N) 的父节点是 p i p_i pi ( 1 ≤ p i < i 1 \le p_i < i 1≤pi<i)。最初,所有节点都被染成白色。

你可以执行以下序列的操作零次或多次:

  • 选择一对整数 ( u , v ) (u, v) (u,v),并满足以下所有条件:

    • 1 ≤ u < v ≤ N 1 \le u < v \le N 1≤u<v≤N
    • 节点 u u u 和 v v v 均为白色。
    • u u u 不是 v v v 的祖先。
  • 将节点 u u u 和 v v v 染成黑色。

这里," u u u 不是 v v v 的祖先"意味着无论你执行多少次移动到当前节点父节点的操作,都无法从节点 v v v 到达节点 u u u。

请找到在以适当顺序执行操作时,你最多可以执行的操作次数。

给定 T T T 组测试用例,请为每组用例找到答案。

限制条件

  • 1 ≤ T ≤ 5 × 10 4 1 \le T \le 5 \times 10^4 1≤T≤5×104
  • 2 ≤ N ≤ 5 × 10 5 2 \le N \le 5 \times 10^5 2≤N≤5×105
  • 1 ≤ p i < i 1 \le p_i < i 1≤pi<i
  • 所有测试用例中 N N N 的总和不超过 5 × 10 5 5 \times 10^5 5×105。
  • 所有输入值均为整数。

Solutions

Approach 1

贪心。

先从最简单的情况考虑 ------ 考虑根节点和其子树,什么时候能够完全选干净子树 (当然,根节点本身选不了,如果子节点数量是奇数会有一个选不了)。

显然,当最大的子树 (即重儿子) 不大于其余所有子树和的时候。否则,假设大于,来继续递归考虑。

对于重儿子和其子树,其能够选干净的限制就是其重子树不大于其余所有子树和额外加上上一层传递来的其他所有子树和

于是如此递归。

Code
cpp 复制代码
#include<bits/stdc++.h>
using namespace std;

std::mt19937 rng(std::random_device{}());
typedef long double ld;
typedef long long ll;
typedef unsigned long long ull;

const int mod = 998244353;
const double ept = 1e-9;

void err() { cerr << endl; } template<class T, class... Ts> void err(const T& arg, const Ts&... args) { cerr << arg << " "; err(args...); }
#define db(x...) { do { cerr << #x << " -> "; err(x); } while (0); }
#define dbv(vec) { cout << #vec << ": "; for(auto &v: vec) { cout << v << ' '; } cout << endl; }


int n;
int p[500500];
int s_cnt[500500];
int hv[500500];
int ans;

void dfs(int u, int f, int s) {
  db(u, hv[u])
  if(s > 0) --s, ++ans;

  int r = s_cnt[u] - s_cnt[hv[u]] - 1;
  if(s_cnt[hv[u]] <= r + s) {
    ans += (s_cnt[u] - 1 + s) / 2;
    return;
  }
  else if(hv[u] != 0) dfs(hv[u], u, s+r);
}

void solve(int T) {
  cin >> n;

  ans = 0;
  for(int i=1; i<=n; i++) s_cnt[i] = 1, hv[i] = 0;

  for(int i=2; i<=n; i++) cin >> p[i];

  for(int i=n; i>=2; i--) {
    s_cnt[p[i]] += s_cnt[i];
    if(s_cnt[hv[p[i]]] < s_cnt[i]) hv[p[i]] = i;
  }

  dfs(1, 1, 0);

  cout << ans << endl;
}

void init() {}

signed main() {
    //freopen("1.in", "r", stdin);
    //freopen("1.out", "w", stdout);
    //cout.flags(ios::fixed); cout.precision(8);
    ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    init();
    int T_=1;
    std::cin >> T_;
    for(int _T=1; _T<=T_; _T++) { solve(_T); }
    return 0;
}
相关推荐
dazzle18 小时前
机器学习算法原理与实践-入门(三):使用数学方法实现KNN
人工智能·算法·机器学习
那个村的李富贵18 小时前
智能炼金术:CANN加速的新材料AI设计系统
人工智能·算法·aigc·cann
张张努力变强19 小时前
C++ STL string 类:常用接口 + auto + 范围 for全攻略,字符串操作效率拉满
开发语言·数据结构·c++·算法·stl
万岳科技系统开发19 小时前
食堂采购系统源码库存扣减算法与并发控制实现详解
java·前端·数据库·算法
张登杰踩19 小时前
MCR ALS 多元曲线分辨算法详解
算法
YuTaoShao19 小时前
【LeetCode 每日一题】3634. 使数组平衡的最少移除数目——(解法一)排序+滑动窗口
算法·leetcode·排序算法
波波00719 小时前
每日一题:.NET 的 GC是如何分代工作的?
算法·.net·gc
风暴之零19 小时前
变点检测算法PELT
算法
深鱼~19 小时前
视觉算法性能翻倍:ops-cv经典算子的昇腾适配指南
算法·cann
李斯啦果19 小时前
【PTA】L1-019 谁先倒
数据结构·算法