Submission #24773656


Source Code Expand

#include "bits/stdc++.h"

#define int long long

using namespace std;
using ll = long long;
using P = pair<ll, ll>;
const ll INF = (1LL << 61);
ll mod = 1000000007;
struct mint {
	ll x; // typedef long long ll;
	mint(ll x = 0) :x((x%mod + mod) % mod) {}
	mint operator-() const { return mint(-x); }
	mint& operator+=(const mint a) {
		if ((x += a.x) >= mod) x -= mod;
		return *this;
	}
	mint& operator-=(const mint a) {
		if ((x += mod - a.x) >= mod) x -= mod;
		return *this;
	}
	mint& operator*=(const mint a) {
		(x *= a.x) %= mod;
		return *this;
	}
	mint operator+(const mint a) const {
		mint res(*this);
		return res += a;
	}
	mint operator-(const mint a) const {
		mint res(*this);
		return res -= a;
	}
	mint operator*(const mint a) const {
		mint res(*this);
		return res *= a;
	}
	mint pow(ll t) const {
		if (!t) return 1;
		mint a = pow(t >> 1);
		a *= a;
		if (t & 1) a *= *this;
		return a;
	}

	// for prime mod
	mint inv() const {
		return pow(mod - 2);
	}
	mint& operator/=(const mint a) {
		return (*this) *= a.inv();
	}
	mint operator/(const mint a) const {
		mint res(*this);
		return res /= a;
	}
};
istream& operator>>(istream& is, mint& a) { return is >> a.x; }
ostream& operator<<(ostream& os, const mint& a) { return os << a.x; }

int N;
char c[100010];
vector<int>G[100010];
mint dp[100010][3];
void dfs(int v, int p = -1) {
	mint S = 1, T = 1;
	if (c[v] == 'a') {
		for (auto nv : G[v]) {
			if (nv == p)continue;
			dfs(nv, v);
			S *= (dp[nv][0] + dp[nv][2]);
			T *= (dp[nv][0] + dp[nv][1] + (mint)2 * dp[nv][2]);
		}
		dp[v][0] = S;
		dp[v][2] = T - S;
	}
	else {
		for (auto nv : G[v]) {
			if (nv == p)continue;
			dfs(nv, v);
			S *= (dp[nv][1] + dp[nv][2]);
			T *= (dp[nv][0] + dp[nv][1] + (mint)2 * dp[nv][2]);
		}
		dp[v][1] = S;
		dp[v][2] = T - S;
	}
}

signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	cin >> N;
	for (int i = 0; i < N; i++)cin >> c[i];
	for (int i = 0; i < N - 1; i++) {
		int a, b; cin >> a >> b; a--; b--;
		G[a].push_back(b);
		G[b].push_back(a);
	}
	dfs(0, -1);
	cout << dp[0][2] << endl;
	return 0;


}

Submission Info

Submission Time
Task 073 - We Need Both a and b(★5)
User Example
Language C++ (GCC 9.2.1)
Score 5
Code Size 2185 Byte
Status AC
Exec Time 62 ms
Memory 12176 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 5 / 5
Status
AC × 3
AC × 43
Set Name Test Cases
Sample 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt
All 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt, 10_max_00.txt, 10_max_01.txt, 10_max_02.txt, 10_max_03.txt, 10_max_04.txt, 10_small_00.txt, 10_small_01.txt, 10_small_02.txt, 10_small_03.txt, 10_small_04.txt, 10_small_05.txt, 10_small_06.txt, 10_small_07.txt, 10_small_08.txt, 10_small_09.txt, 10_small_10.txt, 10_small_11.txt, 10_small_12.txt, 10_small_13.txt, 10_small_14.txt, 10_small_15.txt, 10_small_16.txt, 10_small_17.txt, 10_small_18.txt, 10_small_19.txt, 11_large_00.txt, 11_large_01.txt, 11_large_02.txt, 11_large_03.txt, 11_large_04.txt, 11_large_05.txt, 11_large_06.txt, 11_large_07.txt, 11_large_08.txt, 11_large_09.txt, 20_uni_00.txt, 20_uni_01.txt, 30_hand_00.txt, 30_hand_01.txt, 30_hand_02.txt
Case Name Status Exec Time Memory
00_sample_00.txt AC 9 ms 8228 KiB
00_sample_01.txt AC 14 ms 8316 KiB
00_sample_02.txt AC 6 ms 8320 KiB
10_max_00.txt AC 57 ms 12056 KiB
10_max_01.txt AC 57 ms 12068 KiB
10_max_02.txt AC 57 ms 12116 KiB
10_max_03.txt AC 62 ms 12092 KiB
10_max_04.txt AC 56 ms 12144 KiB
10_small_00.txt AC 9 ms 8248 KiB
10_small_01.txt AC 7 ms 8228 KiB
10_small_02.txt AC 6 ms 8176 KiB
10_small_03.txt AC 9 ms 8240 KiB
10_small_04.txt AC 8 ms 8192 KiB
10_small_05.txt AC 7 ms 8192 KiB
10_small_06.txt AC 8 ms 8288 KiB
10_small_07.txt AC 7 ms 8248 KiB
10_small_08.txt AC 7 ms 8308 KiB
10_small_09.txt AC 8 ms 8244 KiB
10_small_10.txt AC 10 ms 8288 KiB
10_small_11.txt AC 9 ms 8240 KiB
10_small_12.txt AC 7 ms 8292 KiB
10_small_13.txt AC 10 ms 8244 KiB
10_small_14.txt AC 9 ms 8244 KiB
10_small_15.txt AC 8 ms 8272 KiB
10_small_16.txt AC 7 ms 8272 KiB
10_small_17.txt AC 14 ms 8184 KiB
10_small_18.txt AC 8 ms 8248 KiB
10_small_19.txt AC 6 ms 8244 KiB
11_large_00.txt AC 7 ms 8384 KiB
11_large_01.txt AC 8 ms 8236 KiB
11_large_02.txt AC 6 ms 8256 KiB
11_large_03.txt AC 8 ms 8336 KiB
11_large_04.txt AC 11 ms 8248 KiB
11_large_05.txt AC 8 ms 8316 KiB
11_large_06.txt AC 8 ms 8216 KiB
11_large_07.txt AC 7 ms 8320 KiB
11_large_08.txt AC 5 ms 8308 KiB
11_large_09.txt AC 7 ms 8316 KiB
20_uni_00.txt AC 58 ms 12076 KiB
20_uni_01.txt AC 53 ms 12176 KiB
30_hand_00.txt AC 8 ms 8288 KiB
30_hand_01.txt AC 9 ms 8228 KiB
30_hand_02.txt AC 6 ms 8316 KiB