dfs and similar dp graphs trees *1300

Please click on ads to support us..

Python Code:

from sys import *
setrecursionlimit(10 ** 8)
for i in range(int(input())):
	n = int(input())
	a = list(map(int,input().split()))
	graph =  [[] for i in range(n+1)]
	for i in range(2 , n+1):
		graph[i].append(a[i-2])
		graph[a[i-2]].append(i)
	b = input()
	b = b.replace('W' , '1')
	b = b.replace('B' , '0')
	b = [-1] + list(map(int,b))
	dp = [[0 , 0] for i in range(n+1)]
	vis = [0 for i in range(n+1)]
	def dfs(u):
		vis[u] = 1
		dp[u][b[u]] = 1
		for v in graph[u]:
			if vis[v] == 0:
				dfs(v)
				dp[u][0] += dp[v][0]
				dp[u][1] += dp[v][1]
	def main():
		dfs(1)
		count = 0
		for i in dp[1:]:
			if i[0] == i[1]:
				count += 1
		print(count)
	main()

C++ Code:

/* Author: Abhishek Singh */
#include <bits/stdc++.h>
typedef long long ll;
#define mod 1e9+7;
#define int long long int 
using namespace std;
#define nl cout<<"\n";
#define setBits(x) __builtin_popcountll(x)
const int INF=1e9;
vector <int> storePrime;bool primeSieve[10000001];
void sieve(){primeSieve[0]=true;primeSieve[1]=true;int limit=10000000;for(int i=2;i*i<=limit;i++)
{if(!primeSieve[i]){for(int j=i*i;j<=limit;j+=i)primeSieve[j]=true;}}for(int i=2;i<=limit;i++)
{if(!primeSieve[i]) storePrime.push_back(i);}}
void swap(ll &x,ll &y){ll temp=x;x=y;y=temp;}
ll factors(ll n){ll c=0;for(int j=1;j*j<=n;j++){if(n%j==0&&n!=j*j)c+=2;else if(n%j==0&&n==j*j)c++;}return c;}
/***********************************************************************************************************/
int dfs(int root,vector<int> adj[],string s,int &count){
	int ans=0;
	if(s[root-1]=='B')
		ans++;
	else
		ans--;
	for(auto i:adj[root]){
		ans=ans+dfs(i,adj,s,count);
	}
	count+=ans==0;
	return ans;
}
void solve(){
	int n;
	cin>>n;
	vector<int> a(n);int root=0;
	for(int i=0;i<n-1;i++)cin>>a[i];
	string s;cin>>s;
	vector<int> adj[n+1];
	for(int i=0;i<n-1;i++){
		adj[a[i]].push_back(i+2);
	}
	int count=0;
	dfs(1,adj,s,count);
	cout<<count;nl;
}
int32_t main(){
	ios::sync_with_stdio(0);
	cin.tie(NULL);cout.tie(NULL);
	#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
	#endif
 
	int t = 1;
	cin>>t;
	//sieve();
	while(t--)solve();
	return 0;
}


Comments

Submit
0 Comments
More Questions

344. Reverse String
1047. Remove All Adjacent Duplicates In String
977. Squares of a Sorted Array
852. Peak Index in a Mountain Array
461. Hamming Distance
1748. Sum of Unique Elements
897. Increasing Order Search Tree
905. Sort Array By Parity
1351. Count Negative Numbers in a Sorted Matrix
617. Merge Two Binary Trees
1450. Number of Students Doing Homework at a Given Time
700. Search in a Binary Search Tree
590. N-ary Tree Postorder Traversal
589. N-ary Tree Preorder Traversal
1299. Replace Elements with Greatest Element on Right Side
1768. Merge Strings Alternately
561. Array Partition I
1374. Generate a String With Characters That Have Odd Counts
1822. Sign of the Product of an Array
1464. Maximum Product of Two Elements in an Array
1323. Maximum 69 Number
832. Flipping an Image
1295. Find Numbers with Even Number of Digits
1704. Determine if String Halves Are Alike
1732. Find the Highest Altitude
709. To Lower Case
1688. Count of Matches in Tournament
1684. Count the Number of Consistent Strings
1588. Sum of All Odd Length Subarrays
1662. Check If Two String Arrays are Equivalent