580C - Kefa and Park - CodeForces Solution


dfs and similar graphs trees *1500

Please click on ads to support us..

Python Code:

'''
Don't Copy This Code, CopyRight . [email protected] © 2022-2023 :)
'''

import sys
import threading
sys.setrecursionlimit(2147483647)
input = sys.stdin.readline
def print(*args, end='\n', sep=' ') -> None:
    sys.stdout.write(sep.join(map(str, args)) + end)

def dfs(node , visited , c):
    global graph, x
    if(x[node - 1]):c += 1
    else:c = 0
    
    if(c > m):return 0
    
    ans = 0
    for i in graph[node]:
 
        if(i != visited):
            ans += dfs(i , node , c)
 
    if(node != 1 and len(graph[node]) == 1):return 1
    return ans

def Solve():
    global x, m, ans, graph
    n, m = map(int, input().split())
    x = list(map(int, input().split()))
    ans = 0
    graph = dict()
    for i in range(n-1):
        a, b = map(int, input().split())
        if a not in graph.keys():
            graph[a] = set()
        if b not in graph.keys():
            graph[b] = set()
        graph[a].add(b)
        graph[b].add(a)

    ans = dfs(1, -1, 0)
    print(ans)


if __name__ == "__main__":
    threading.stack_size(10**8)
    threading.Thread(target=Solve).start()

C++ Code:

#include <bits/stdc++.h>
#define pb push_back
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
#define ll long long
using namespace std;
const int MOD = 1e9 + 7;
const int N = 2e5+6;

int n,m;
vector<int>g[N];
int a[N];

int dfs(int u,int c,int p){
    if(c > m)return 0;
    if(sz(g[u]) == 1 && u != 1)return 1;
    int ret = 0;
    for(auto v : g[u]){
        if(v == p)continue;
        if(a[v] == 1){
            ret += dfs(v,c+1,u);
        }else{
            ret += dfs(v,0,u);
        }
    }
    return ret;
}
void solve(int tc){
    scanf("%d %d",&n,&m);
    for(int i = 1;i<=n;i++)scanf("%d",&a[i]);
    for(int i = 1,u,v;i<n;i++)scanf("%d %d",&u,&v),g[u].pb(v),g[v].pb(u);
    printf("%d\n",dfs(1,a[1],0));
}

int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int tc = 1;
    //cin>>tc;
    //scanf("%d",&tc);
    for(int i = 1;i<=tc;i++)
        solve(i);
}


Comments

Submit
0 Comments
More Questions

1832. Check if the Sentence Is Pangram
1678. Goal Parser Interpretation
1389. Create Target Array in the Given Order
1313. Decompress Run-Length Encoded List
1281. Subtract the Product and Sum of Digits of an Integer
1342. Number of Steps to Reduce a Number to Zero
1528. Shuffle String
1365. How Many Numbers Are Smaller Than the Current Number
771. Jewels and Stones
1512. Number of Good Pairs
672. Richest Customer Wealth
1470. Shuffle the Array
1431. Kids With the Greatest Number of Candies
1480. Running Sum of 1d Array
682. Baseball Game
496. Next Greater Element I
232. Implement Queue using Stacks
844. Backspace String Compare
20. Valid Parentheses
746. Min Cost Climbing Stairs
392. Is Subsequence
70. Climbing Stairs
53. Maximum Subarray
1527A. And Then There Were K
1689. Partitioning Into Minimum Number Of Deci-Binary Numbers
318. Maximum Product of Word Lengths
448. Find All Numbers Disappeared in an Array
1155. Number of Dice Rolls With Target Sum
415. Add Strings
22. Generate Parentheses