D. Serval and Rooted Tree

本文解析了Codeforces比赛中的D题,题目要求在n个节点的树中,给叶子节点分配1~k的值,通过min和max操作求根节点的最大可能值。文章详细介绍了算法思路,包括使用DFS遍历树并记录每个节点能表示的叶子数量的最小值。

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

http://codeforces.com/contest/1153/problem/D

题意:n个节点的树,叶子可以任意分别给1~k(k为叶子个数)的值,每个节点有操作min,max分别取其子树的最小和最大的值;

问根节点1最大多少;

思路:每个节点记录它能表示的它叶子数量的最小的值(排序后最靠近前面的,第k大的数),min时就是只能是子树的叶子的数量了;max时就能是儿子最小的了(最靠前);ans就是叶子数量减去根的值;

#include<algorithm>
#include<set>
#include<vector>
#include<queue>
#include<cmath>
#include<cstring>
#include<iostream>
#include<set>
#include<vector>
#include<queue>
#include<cmath>
#include<cstdio>
#include<map>
#include<stack>
#include<string>
using namespace std;

#define sfi(i) scanf("%d",&i)
#define pri(i) printf("%d\n",i)
#define sff(i) scanf("%lf",&i)
#define ll long long
#define mem(x) memset(x,0,sizeof(x))
#define INF 0x3f3f3f3f
#define eps 1e-16
#define PI acos(-1)
#define lowbit(x) ((x)&(-x))
#define zero(x) (((x)>0?(x):-(x))<eps)
#define fl() printf("flag\n")
#define MOD(x) ((x%mod)+mod)%mod
#define FASTIO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
ll gcd(ll a,ll b){while(b^=a^=b^=a%=b);return a;}
const int maxn=3e5+9;
const int mod=1e9+7;

template <class T>
inline void sc(T &ret)
{
    char c;
    ret = 0;
    while ((c = getchar()) < '0' || c > '9');
    while (c >= '0' && c <= '9')
    {
        ret = ret * 10 + (c - '0'), c = getchar();
    }
}

ll power(ll x,ll n)
{
    ll ans=1;
    while(n)
    {
        if(n&1) ans=ans*x;
        n>>=1;
        x=x*x;
    }
    return ans;
}

vector<int>g[maxn];
bool op[maxn];
int k;
int dfs(int u)
{
    int num=0;
    if(op[u]) num=INF;
    if(g[u].size()==0) num=1,k++;
    for(int i=0;i<g[u].size();i++)
    {
        int v=g[u][i];
        int numv=dfs(v);

        if(op[u])
        {
            num=min(numv,num);
        }
        else num+=numv;
    }
    return num;
}
int main()
{
    FASTIO;
    int n;
    cin>>n;
    for(int i=1;i<=n;i++) cin>>op[i];
    for(int i=2;i<=n;i++)
    {
        int u;
        cin>>u;
        g[u].push_back(i);
    }
    int num=dfs(1);
    cout<<k-num+1<<endl;
    return 0;
}

 

 

B. Serval and Final MEX time limit per test1 second memory limit per test256 megabytes You are given an array a consisting of n≥4 non-negative integers. You need to perform the following operation on a until its length becomes 1 : Select two indices l and r (1≤l<r≤|a| ), and replace the subarray [al,al+1,…,ar] with a single integer mex([al,al+1,…,ar]) , where mex(b) denotes the minimum excluded (MEX)∗ of the integers in b . In other words, let x=mex([al,al+1,…,ar]) , the array a will become [a1,a2,…,al−1,x,ar+1,ar+2,…,a|a|] . Note that the length of a decreases by (r−l) after this operation. Serval wants the final element in a to be 0 . Help him! More formally, you have to find a sequence of operations, such that after performing these operations in order, the length of a becomes 1 , and the final element in a is 0 . It can be shown that at least one valid operation sequence exists under the constraints of the problem, and the length of any valid operation sequence does not exceed n . Note that you do not need to minimize the number of operations. ∗ The minimum excluded (MEX) of a collection of integers b1,b2,…,bk is defined as the smallest non-negative integer x which does not occur in the collection b . Input Each test contains multiple test cases. The first line contains the number of test cases t (1≤t≤1000 ). The description of the test cases follows. The first line of each test case contains a single integer n (4≤n≤5000 ) — the length of the array a . The second line contains n integers a1,a2,…,an (0≤ai≤n ) — the elements of the array a . It is guaranteed that the sum of n over all test cases does not exceed 5000 . Output For each test case, output a single integer k (0≤k≤n ) in the first line of output — the length of the operation sequence. Then, output k lines, the i -th line containing two integers li and ri (1≤li<ri≤|a| ) — the two indices you choose in the i -th operation, where |a| denotes the length of the array be
03-23
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值