首页 > 其他分享 >E. Wooden Game

E. Wooden Game

时间:2024-07-20 18:07:46浏览次数:4  
标签:int Wooden vertex tree trees leq Game test

E. Wooden Game

You are given a forest of $k$ rooted trees$^{\text{∗}}$. Lumberjack Timofey wants to cut down the entire forest by applying the following operation:

  • Select a subtree$^{\text{†}}$ of any vertex of one of the trees and remove it from the tree.

Timofey loves bitwise operations, so he wants the bitwise OR of the sizes of the subtrees he removed to be maximum. Help him and find the maximum result he can obtain.

$^{\text{∗}}$ A tree is a connected graph without cycles, loops, or multiple edges. In a rooted tree, a selected vertex is called a root. A forest is a collection of one or more trees.

$^{\text{†}}$ The subtree of a vertex $v$ is the set of vertices for which $v$ lies on the shortest path from this vertex to the root, including $v$ itself.

Input

Each test consists of multiple test cases. The first line contains an integer $t$ ($1 \leq t \leq 10^4$) — the number of test cases. Then follows the description of the test cases.

The first line of each test case contains a single integer $k$ ($1 \leq k \leq 10^6$) — the number of trees in the forest.

This is followed by a description of each of the $k$ trees:

The first line contains a single integer $n$ ($1 \leq n \leq 10^6$) — the size of the tree. The vertices of the tree are numbered with integers from $1$ to $n$. The root of the tree is vertex number $1$.

The second line contains $n - 1$ integers $p_2, p_3, \ldots p_n$ ($1 \leq p_i < i$), where $p_i$ — the parent of vertex $i$.

It is guaranteed that the sum of $k$ and $n$ for all sets of input data does not exceed $10^6$.

Output

For each test case, output a single integer — the maximum result that can be obtained.

Example

input

3
1
1

2
4
1 2 2
6
1 1 3 1 3
1
10
1 2 2 1 1 5 7 6 4

output

1
7
10

Note

In the second test case, the trees look like this:

The first operation removes the entire second tree.

The second operation removes vertex $4$ from the first tree.

The third operation removes the first tree. The result is $6|1|3 = 7$ ($|$ denotes bitwise OR).

In the third test case, the entire tree needs to be removed.

 

解题思路

  假设只有一棵树,那么 OR 的最大值就是这棵树本身的大小 $n$。假设把一棵树拆分成了 $m$ 个部分,每个部分的大小为 $a_i$,有 $a_1 \mid a_2 \mid \cdots \mid a_m \leq a_1 + a_2 + \cdots + a_n = n$,这是因为 OR 运算没有进位。另外,对于一棵大小为 $n$ 的树,我们可以拆分得到大小在 $1 \sim n$ 的任意一棵树,每次只需删除叶子即可。因此如果我们想让某棵树贡献某个值时,只需通过删除叶子来得到该大小的树即可,而无需考虑如何拆分。

  因此现在的问题变成了,有 $k$ 个数,每个数可取的范围是 $[1, n_i]$,求这 $k$ 个数 OR 的最大值。

  从高位往低位贪心考虑,对于某个位 $i$,如果至少有两个数的第 $i$ 位是 $1$,则将其中一个数变小,使其第 $0$ 到第 $i-1$ 位变成 $1$,这样通过 OR 就可以对答案贡献 $2^{i+1} - 1$。如果只有一个数的第 $i$ 位是 $1$,则保留这一位,对答案贡献 $2^i$。

  AC 代码如下,时间复杂度为 $O(n \log{n})$:

#include <bits/stdc++.h>
using namespace std;

typedef long long LL;

const int N = 1e6 + 5;

int a[N];

void solve() {
    int n;
    scanf("%d", &n);
    for (int i = 0; i < n; i++) {
        scanf("%d", a + i);
        for (int j = 0; j < a[i] - 1; j++) {
            scanf("%*d");
        }
    }
    sort(a, a + n, greater<int>());
    int ret = 0;
    for (int i = 0; i < n; i++) {
        for (int j = 19; j >= 0; j--) {
            if (a[i] >> j & 1) {
                if (ret >> j & 1) ret |= (1 << j) - 1;
                else ret |= 1 << j;
            }
        }
    }
    printf("%d\n", ret);
}

int main() {
    int t;
    scanf("%d", &t);
    while (t--) {
        solve();
    }
    
    return 0;
}

 

参考资料

  Codeforces Round 959 sponsored by NEAR (Div. 1 + Div. 2) A~E:https://zhuanlan.zhihu.com/p/709662276

标签:int,Wooden,vertex,tree,trees,leq,Game,test
From: https://www.cnblogs.com/onlyblues/p/18313527

相关文章

  • D. Funny Game
    鸽巢原理/抽屉原理:假如有n+1个元素放到n个集合中去,其中必定有一个集合里至少有两个元素首先,将\(x\)整除\(|a_u-a_v|\)转化为它们模x同余有n个点,x=n-1时,根据鸽巢原理,一定可以找到这样的两个同余的点,将它们连边以此类推,解毕模拟样例以感受题意点击查看代码#include<bits/s......
  • 题解:SP10502 VIDEO - Video game combos
    大意构造一个长度为\(k\)(\(k\)是给定的)的串\(x\),使得对于\(∀1\leqi\leqn,s_i\)在\(x\)中的出现次数之和最大。输出这个最大值。思考考虑对\(s_i\)建AC自动机,然后dp。记\(dp[i][u]\)表示为长度为\(i\)的字符串,且当前已计算的节点是Trie上的编号为\(u......
  • 3D 模型在 Game 视图中呈现为 2D效果
    废话不多说,上教程。......
  • [HGAME 2023 week3]kunmusic wp
    今天写了一道Hgame的题,挺有意思的,写个blog记录一下下载附件得到三个文件,先用dnspy打开dll文件,找到main函数,发现为对资源中data的加密。因此将data直接dump下来,对其进行解密,并将解密后的文件保存为111,脚本如下:file=open(r'C:\Users\usr\Desktop\ctf题库\reverse\data','wb')f......
  • pygame.display功能的使用方法
    pygame.display是Pygame库中的一个模块,它主要负责与游戏窗口的显示相关的功能。以下是对pygame.display功能的详细使用方法,按照清晰和有条理的格式进行归纳:1.初始化在使用pygame.display之前,需要先初始化Pygame。这通过pygame.init()完成,它会初始化所有Pygame模块,包括dis......
  • Python学习笔记36:进阶篇(二十五)pygame的使用之事件监听控制切歌和暂停,继续播放
    前言基础模块的知识通过这么长时间的学习已经有所了解,更加深入的话需要通过完成各种项目,在这个过程中逐渐学习,成长。我们的下一步目标是完成pythoncrashcourse中的外星人入侵项目,这是一个2D游戏项目。在这之前,我们先简单学习一下pygame模块。私信我发送消息python资料,......
  • Koxia and Game
    这道题目就看官方解答吧本来这道题目是构造题,但是题目要求计数,计数肯定就很多了,所以我们不能像传统构造题一样,去想如何特殊地构造出一个序列来,这里就要去想满足条件的序列有什么共性,所以我们就假设已经找到了序列\(c\),然后去想想Koxia怎么必胜于是不难发现引理一(这个可以感性理......
  • 【Python实战项目】用Python制作游戏—pygame超级玛丽!游戏开发
    1、需求分析具备功能播放与停止背景音乐随机生成管道与导弹障碍显示积分跳跃躲避障碍碰撞障碍2、游戏功能结构玛丽冒险的功能结构主要分为三类,分别为音效、主窗体以及随机出现的障碍物。如下图3、游戏业务流程根据该游戏的需求分析以及功能结构##-、游戏预览......
  • pygame写物体移动
    importpygameimportsysimporttimepygame.init()size=width,height=800,600screen=pygame.display.set_mode(size)color=255,255,255background=pygame.image.load(r'/Users/bytedance/Desktop/my/back.jpeg')#背景图片,加rbackground=pygame.transf......
  • CSE 13S LRC Rules of the Game
    Assignment 1LRCCSE 13S, Winter 20241 IntroductionWe are going to simulate a simplified version of the dice game Left, Right, and Center. This game isentirely a game of chance, with no skill or player decisions (except f......