思维题--code forces round# 551 div.2 D

本文详细解析了Codeforces Round #551 (Div. 2) D题——Serval and Rooted Tree的解题思路。通过分析树的结构和节点操作,给出了一种高效的算法实现,旨在最大化根节点上的数值。

思维题--code forces round# 551 div.2-D

题目

D. Serval and Rooted Tree

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Now Serval is a junior high school student in Japari Middle School, and he is still thrilled on math as before.

As a talented boy in mathematics, he likes to play with numbers. This time, he wants to play with numbers on a rooted tree.

A tree is a connected graph without cycles. A rooted tree has a special vertex called the root. A parent of a node vv is the last different from vv vertex on the path from the root to the vertex vv. Children of vertex vv are all nodes for which vv is the parent. A vertex is a leaf if it has no children.

The rooted tree Serval owns has nn nodes, node 11 is the root. Serval will write some numbers into all nodes of the tree. However, there are some restrictions. Each of the nodes except leaves has an operation maxmax or minmin written in it, indicating that the number in this node should be equal to the maximum or minimum of all the numbers in its sons, respectively.

Assume that there are kk leaves in the tree. Serval wants to put integers 1,2,…,k1,2,…,k to the kk leaves (each number should be used exactly once). He loves large numbers, so he wants to maximize the number in the root. As his best friend, can you help him?

Input

The first line contains an integer nn (2≤n≤3⋅1052≤n≤3⋅105), the size of the tree.

The second line contains nn integers, the ii-th of them represents the operation in the node ii. 00 represents minmin and 11represents maxmax. If the node is a leaf, there is still a number of 00 or 11, but you can ignore it.

The third line contains n−1n−1 integers f2,f3,…,fnf2,f3,…,fn (1≤fi≤i−11≤fi≤i−1), where fifi represents the parent of the node ii.

Output

Output one integer — the maximum possible number in the root of the tree.

Examples

input

Copy

6
1 0 1 1 0 1
1 2 2 2 2

output

Copy

1

input

Copy

5
1 0 1 0 1
1 1 1 1

output

Copy

4

input

Copy

8
1 0 0 1 0 1 1 0
1 1 2 2 3 3 3

output

Copy

4

input

Copy

9
1 1 0 0 1 0 1 0 1
1 1 2 2 3 3 4 4

output

Copy

5

Note

Pictures below explain the examples. The numbers written in the middle of the nodes are their indices, and the numbers written on the top are the numbers written in the nodes.

In the first example, no matter how you arrange the numbers, the answer is 11.

img

In the second example, no matter how you arrange the numbers, the answer is 44.

img

In the third example, one of the best solution to achieve 44 is to arrange 44 and 55 to nodes 44 and 55.

img

In the fourth example, the best solution is to arrange 55 to node 55.

img

题意易懂

思路

让所有叶子的值都为1

算的是必要的叶子个数,那么答案就是叶子个数 - 必要的叶子个数 + 1

如果是取max,那么该节点要取最大,那必要的叶子个数取决于该节点的所有子节点中,最小的必要叶子个数

如果是取min,那么该节点要取最大,必要的叶子个数为该节点的所有子节点中,所有的必要叶子个数的和

因为题目输入格式,可以知道父节点的输入一定在子节点前面,所以能遍历

#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <cmath>
#include <sstream>
#include <algorithm>
#include <set>
#include <map>
#include <vector>
#include <queue>
#include <iomanip>
#include <stack>

using namespace std;

typedef long long LL;
const int INF = 0x3f3f3f3f;
const int N = 300000+50;
const int MOD = 1e9 + 9;

#define lson l, m, rt << 1
#define rson m + 1, r, rt << 1 | 1
#define F(i, l, r) for(int i = l;i <= (r);++i)
#define RF(i, l, r) for(int i = l;i >= (r);--i)

vector<int> v[N];
int a[N], c[N];
int n;

int main()
{
    cin >> n;
    F(i, 1, n) cin >> a[i];
    F(i, 2, n)
    {
        int t;
        cin >> t;
        v[t].push_back(i);
    }

    int cnt = 0, ans = 0;
    RF(i, n, 1)
    {
        if(v[i].size() == 0)
        {
            c[i] = 1;
            cnt++;
        }
        else if(a[i])
        {
            c[i] = INF;
            F(j, 0, v[i].size() - 1)
                c[i] = min(c[i], c[v[i][j]]);
        }
        else
        {
            F(j, 0, v[i].size() - 1)
                c[i] += c[v[i][j]];
        }
    }
    cout << cnt + 1 - c[1] << endl;
    return 0;
}

dfs写法

#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <cmath>
#include <sstream>
#include <algorithm>
#include <set>
#include <map>
#include <vector>
#include <queue>
#include <iomanip>
#include <stack>

using namespace std;

typedef long long LL;
const int INF = 0x3f3f3f3f;
const int N = 300000+50;
const int MOD = 1e9 + 9;

#define lson l, m, rt << 1
#define rson m + 1, r, rt << 1 | 1
#define F(i, l, r) for(int i = l;i <= (r);++i)
#define RF(i, l, r) for(int i = l;i >= (r);--i)

vector<int> v[N];
int a[N], c[N];
int n, ant = 0, cnt;

void dfs(int now)
{
    if(v[now].size() == 0)
    {
        c[now] = 1;
        cnt++;
    }
    else if(a[now])
    {
        c[now] = INF;
        F(j, 0, v[now].size() - 1)
        {
            dfs(v[now][j]);
            c[now] = min(c[now], c[v[now][j]]);
        }
    }
    else
    {
        F(j, 0, v[now].size() - 1)
        {
            dfs(v[now][j]);
            c[now] += c[v[now][j]];
        }
    }
}

int main()
{
    cin >> n;
    F(i, 1, n) cin >> a[i];
    F(i, 2, n)
    {
        int t;
        cin >> t;
        v[t].push_back(i);
    }
    dfs(1);
    cout << cnt + 1 - c[1] << endl;
    return 0;
}

转载于:https://www.cnblogs.com/shuizhidao/p/10745692.html

C语言-光伏MPPT算法:电导增量法扰动观察法+自动全局搜索Plecs最大功率跟踪算法仿真内容概要:本文档主要介绍了一种基于C语言实现的光伏最大功率点跟踪(MPPT)算法,结合电导增量法与扰动观察法,并引入自动全局搜索策略,利用Plecs仿真工具对算法进行建模与仿真验证。文档重点阐述了两种经典MPPT算法的原理、优缺点及其在不同光照和温度条件下的动态响应特性,同时提出一种改进的复合控制策略以提升系统在复杂环境下的跟踪精度与稳定性。通过仿真结果对比分析,验证了所提方法在快速性和准确性方面的优势,适用于光伏发电系统的高效能量转换控制。; 适合人群:具备一定C语言编程基础和电力电子知识背景,从事光伏系统开发、嵌入式控制或新能源技术研发的工程师及高校研究人员;工作年限1-3年的初级至中级研发人员尤为适合。; 使用场景及目标:①掌握电导增量法与扰动观察法在实际光伏系统中的实现机制与切换逻辑;②学习如何在Plecs中搭建MPPT控制系统仿真模型;③实现自动全局搜索以避免传统算法陷入局部峰值问,提升复杂工况下的最大功率追踪效率;④为光伏逆变器或太阳能充电控制器的算法开发提供技术参考与实现范例。; 阅读建议:建议读者结合文中提供的C语言算法逻辑与Plecs仿真模型同步学习,重点关注算法判断条件、步长调节策略及仿真参数设置。在理解基本原理的基础上,可通过修改光照强度、温度变化曲线等外部扰动因素,进一步测试算法鲁棒性,并尝试将其移植到实际嵌入式平台进行实验验证。
SPAdes genome assembler v4.0.0 Usage: spades.py [options] -o <output_dir> Basic options: -o <output_dir> directory to store all the resulting files (required) --isolate this flag is highly recommended for high-coverage isolate and multi-cell data --sc this flag is required for MDA (single-cell) data --meta this flag is required for metagenomic data --bio this flag is required for biosyntheticSPAdes mode --sewage this flag is required for sewage mode --corona this flag is required for coronaSPAdes mode --rna this flag is required for RNA-Seq data --plasmid runs plasmidSPAdes pipeline for plasmid detection --metaviral runs metaviralSPAdes pipeline for virus detection --metaplasmid runs metaplasmidSPAdes pipeline for plasmid detection in metagenomic datasets (equivalent for --meta --plasmid) --rnaviral this flag enables virus assembly module from RNA-Seq data --iontorrent this flag is required for IonTorrent data --test runs SPAdes on toy dataset -h, --help prints this usage message -v, --version prints version Input data: --12 <filename> file with interlaced forward and reverse paired-end reads -1 <filename> file with forward paired-end reads -2 <filename> file with reverse paired-end reads -s <filename> file with unpaired reads --merged <filename> file with merged forward and reverse paired-end reads --pe-12 <#> <filename> file with interlaced reads for paired-end library number <#>. Older deprecated syntax is -pe<#>-12 <filename> --pe-1 <#> <filename> file with forward reads for paired-end library number <#>. Older deprecated syntax is -pe<#>-1 <filename> --pe-2 <#> <filename> file with reverse reads for paired-end library number <#>. Older deprecated syntax is -pe<#>-2 <filename> --pe-s <#> <filename> file with unpaired reads for paired-end library number <#>. Older deprecated syntax is -pe<#>-s <filename> --pe-m <#> <filename> file with merged reads for paired-end library number <#>. Older deprecated syntax is -pe<#>-m <filename> --pe-or <#> <or> orientation of reads for paired-end library number <#> (<or> = fr, rf, ff). Older deprecated syntax is -pe<#>-<or> --s <#> <filename> file with unpaired reads for single reads library number <#>. Older deprecated syntax is --s<#> <filename> --mp-12 <#> <filename> file with interlaced reads for mate-pair library number <#>. Older deprecated syntax is -mp<#>-12 <filename> --mp-1 <#> <filename> file with forward reads for mate-pair library number <#>. Older deprecated syntax is -mp<#>-1 <filename> --mp-2 <#> <filename> file with reverse reads for mate-pair library number <#>. Older deprecated syntax is -mp<#>-2 <filename> --mp-s <#> <filename> file with unpaired reads for mate-pair library number <#>. Older deprecated syntax is -mp<#>-s <filename> --mp-or <#> <or> orientation of reads for mate-pair library number <#> (<or> = fr, rf, ff). Older deprecated syntax is -mp<#>-<or> --hqmp-12 <#> <filename> file with interlaced reads for high-quality mate-pair library number <#>. Older deprecated syntax is -hqmp<#>-12 <filename> --hqmp-1 <#> <filename> file with forward reads for high-quality mate-pair library number <#>. Older deprecated syntax is -hqmp<#>-1 <filename> --hqmp-2 <#> <filename> file with reverse reads for high-quality mate-pair library number <#>. Older deprecated syntax is -hqmp<#>-2 <filename> --hqmp-s <#> <filename> file with unpaired reads for high-quality mate-pair library number <#>. Older deprecated syntax is -hqmp<#>-s <filename> --hqmp-or <#> <or> orientation of reads for high-quality mate-pair library number <#> (<or> = fr, rf, ff). Older deprecated syntax is -hqmp<#>-<or> --sanger <filename> file with Sanger reads --pacbio <filename> file with PacBio reads --nanopore <filename> file with Nanopore reads --trusted-contigs <filename> file with trusted contigs --untrusted-contigs <filename> file with untrusted contigs Pipeline options: --only-error-correction runs only read error correction (without assembling) --only-assembler runs only assembling (without read error correction) --careful tries to reduce number of mismatches and short indels --checkpoints <last or all> save intermediate check-points ('last', 'all') --continue continue run from the last available check-point (only -o should be specified) --restart-from <cp> restart run with updated options and from the specified check-point ('ec', 'as', 'k<int>', 'mc', 'last') --disable-gzip-output forces error correction not to compress the corrected reads --disable-rr disables repeat resolution stage of assembling Advanced options: --dataset <filename> file with dataset description in YAML format -t <int>, --threads <int> number of threads. [default: 16] -m <int>, --memory <int> RAM limit for SPAdes in Gb (terminates if exceeded). [default: 250] --tmp-dir <dirname> directory for temporary files. [default: <output_dir>/tmp] -k <int> [<int> ...] list of k-mer sizes (must be odd and less than 128) [default: 'auto'] --cov-cutoff <float> coverage cutoff value (a positive float number, or 'auto', or 'off') [default: 'off'] --phred-offset <33 or 64> PHRED quality offset in the input reads (33 or 64), [default: auto-detect] --custom-hmms <dirname> directory with custom hmms that replace default ones, [default: None] --gfa11 use GFA v1.1 format for assembly graph 填写答案
11-13
SPAdes(St. Petersburg genome assembler)是一款专为测序数据组装和分析而设计的多功能工具包,主要针对Illumina测序数据开发,也支持IonTorrent数据,其多个组装流程支持混合模式,可使用PacBio和Oxford Nanopore的长读长数据作为补充数据[^1]。 以下为SPAdes genome assembler v4.0.0通用使用命令示例: ```bash spades.py -1 forward_reads.fastq -2 reverse_reads.fastq -o output_directory ``` ### 常用参数解释 - `-1`:指定正向测序读段文件(通常为FASTQ格式)。例如`-1 reads_1.fastq` 。 - `-2`:指定反向测序读段文件(通常为FASTQ格式)。例如`-2 reads_2.fastq` 。 - `-s`:指定单端测序读段文件(通常为FASTQ格式)。例如`-s single_reads.fastq` 。 - `-o`:指定输出目录,SPAdes会将组装结果存于此目录。例如`-o assembly_results` 。 - `--pacbio`:指定PacBio长读长数据文件(通常为FASTQ或FASTA格式),用于混合组装。例如`--pacbio pacbio_reads.fastq` 。 - `--nanopore`:指定Oxford Nanopore长读长数据文件(通常为FASTQ或FASTA格式),用于混合组装。例如`--nanopore nanopore_reads.fastq` 。 - `--careful`:启用该选项后,SPAdes会尝试减少错配和短插入缺失,不过会增加运行时间。 - `--isolate`:适用于分离株测序数据的组装,优化组装流程以提高组装质量。 - `--meta`:用于宏基因组数据的组装,针对复杂的宏基因组样本优化组装策略。 ### 高级参数解释 - `--cov-cutoff`:指定覆盖度阈值,低于该阈值的读段会被过滤掉。例如`--cov-cutoff 5` 。 - `--k-mers`:指定使用的k - mer值,可提供多个值,用逗号分隔。例如`--k-mers 21,33,55` 。 - `--threads`:指定运行时使用的线程数,可加快组装速度。例如`--threads 8` 。 - `--memory`:指定运行时允许使用的最大内存(单位为GB)。例如`--memory 32` 。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值