首页 > 其他分享 >Atcode Beginner Constest 309 E

Atcode Beginner Constest 309 E

时间:2023-07-09 19:55:34浏览次数:39  
标签:309 Beginner int cin dfs Atcode adj 节点 dp

e题的题意又理解错了(

E. Family and Insurance

题意

给定一棵或者若干棵树,以及\(m\)次操作。每次操作将一个节点后面几层的儿子节点的权值加1,求最后有多少节点的权值至少为1。

思路

设\(dp[i]\)为节点\(i\)后面有几个节点被覆盖,若没有覆盖为-1。DFS一遍维护每个\(dp[i]\)的最大值,最后再统计符合要求的节点数。

代码

#include<bits/stdc++.h>

using namespace std;
using ll = long long;
const int mod = 1e9 + 7;
const int N = 3e5 + 10;

vector<int> adj[N];
array<int, N> dp, p;

void dfs(int u) {
    for(int i : adj[u]) {
        dp[i] = max(dp[i], dp[p[i]] - 1);
        dfs(i);
    }
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int n, m;
    cin >> n >> m;

    for(int i = 2; i <= n; i++) {
        cin >> p[i];
        adj[p[i]].push_back(i);
    }

    fill(g.begin(), g.end(), -1);

    for(int i = 0; i < m; i++) {
        int x, y;
        cin >> x >> y;
        dp[x] = max(dp[x], y);
    }

    dfs(1);

    int ans = 0;
    for(int i = 1; i <= n; i++) {
        if(g[i] >= 0) {
            ans++;
        }
    }

    cout << ans << "\n";

    return 0;
}

标签:309,Beginner,int,cin,dfs,Atcode,adj,节点,dp
From: https://www.cnblogs.com/cenqi/p/17539265.html

相关文章

  • Atcoder ARC159C Permutation Addition
    设\(s=\sum\limits_{i=1}^na_i\),考虑加上排列\(p\),那么\(s\leftarrows+\frac{n\times(n+1)}{2}\)。当有解时,因为\(a_i\)相等,所以有\(s\bmodn=0\)。考虑\(n\bmod2=1\)时,\(\frac{n\times(n+1)}{2}\bmodn=0\),所以\(s\bmodn=0\)时才会有解。......
  • AtCoder Beginner Contest 309
    A:1#include<cstdio>2#include<cstring>3#include<algorithm>4#include<iostream>5#include<string>6#include<vector>7#include<stack>8#include<bitset>9#include<cstdlib>10#include......
  • AtCoder Beginner Contest 273 ABCD
    AtCoderBeginnerContest273A-ARecursiveFunctionProblemStatement题意:给你一个函数\(f(x)\)\(f(0)=1\)对于所有正整数\(k\),有\(f(k)=k*f(k-1)\)找到\(f(N)\)Solution思路:数据范围只有\(10\),直接递归。#include<bits/stdc++.h>usingnamespacestd;typede......
  • abc309e <dfs>
    E-FamilyandInsurance//https://atcoder.jp/contests/abc309/tasks/abc309_e//<dfs>//关键在于意识到,每个结点保留最大后代数即可#include<iostream>#include<algorithm>#include<vector>usingnamespacestd;typedeflonglongLL;constintN=3......
  • abc309f <线段树 + 离散化 + 双指针>
    F-BoxinBox//https://atcoder.jp/contests/abc309/tasks/abc309_f//<线段树+离散化+双指针>[unique+lower_bound+erase+lambda+vector]//总体思路:将每个三元组记录为如a[3]的3维向量,依次考虑每个向量,检查是否存在一个向量完全比它'小'//将向量按......
  • AtCoder Beginner Contest 309
    感觉F写了个乱搞做法A-Nine(abc309A)题目大意给定一个\(3\times3\)的网格,以及两个数字。问这两个数字是否水平相邻。解题思路求出两个数字的横纵坐标,看是否横坐标相同,纵坐标差一即可。读题不仔细,开题就WA了。神奇的代码#include<bits/stdc++.h>usingnamespa......
  • AtCoder Grand Contest 058 D Yet Another ABC String
    洛谷传送门AtCoder传送门OrzH6_6Q,感谢他给我们带来了这道容斥好题。这个东西看起来很不好搞。可以尝试容斥。但是我们要容斥啥?钦定ABC不出现,其他任意?感觉还是很难算。观察不合法子串,发现它们很有特点。如果我们钦定\(\texttt{A}\)为\(0\),\(\texttt{B}\)为\(1\),\(\te......
  • AtCoder Beginner Contest 308 题解
    https://atcoder.jp/contests/abc308/tasks_printA-NewScheme过水已隐藏。#include<bits/stdc++.h>#include<ext/pb_ds/assoc_container.hpp>#include<ext/pb_ds/tree_policy.hpp>#include<ext/pb_ds/hash_policy.hpp>usingnamespacestd;u......
  • python: PyQt5 beginner
     fromPyQt5.QtWidgetsimportQWidget,QApplication,QMainWindow,QLabel,QPushButtonfromPyQt5importQtCore,QtGuiimportsysimportosdefclick():print("HyButtonisclicked!")#Pressthegreenbuttonintheguttertorunthescri......
  • AtCoder Beginner Contest 264 ABCDE
    AtCoderBeginnerContest264A-"atcoder".substr()ProblemStatement题意:截取字符串atcoder的[L,R]一段并输出。Solution题解:用string.substr直接写#include<bits/stdc++.h>usingnamespacestd;intmain(){ strings="?atcoder"; intl,r; cin&......