QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#398528 | #7509. 01tree | luanmenglei | TL | 15ms | 11520kb | C++17 | 2.8kb | 2024-04-25 14:35:23 | 2024-04-25 14:35:23 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
namespace SOL {
using i64 = long long;
void debug(const char *msg, ...) {
#ifdef CLESIP
va_list arg;
static char pbString[512];
va_start(arg,msg);
vsprintf(pbString,msg,arg);
cerr << "[DEBUG] " << pbString << "\n";
va_end(arg);
#endif
}
template<typename T, typename L>
bool chkmax(T &x, L y) { if (x < y) return x = y, true; return false; }
template<typename T, typename L>
bool chkmin(T &x, L y) { if (y < x) return x = y, true; return false; }
const int N = 2e5 + 10;
const i64 P = 1e9 + 7;
int n, dep[N], asum[2], acnt[2];
i64 fac[N], inv[N], ans;
char s[N], t[N];
vector<int> G[N];
void init() {
fac[0] = inv[0] = inv[1] = 1;
for (int i = 1; i < N; i ++)
fac[i] = fac[i - 1] * i % P;
for (int i = 2; i < N; i ++)
inv[i] = (P - P / i) * inv[P % i] % P;
for (int i = 1; i < N; i ++)
inv[i] = inv[i - 1] * inv[i] % P;
}
void mod(i64 &x, i64 y) {
x += y;
if (x >= P)
x -= P;
}
i64 binom(int n, int m) {
if (n < m || m < 0)
return 0;
return fac[n] * inv[m] % P * inv[n - m] % P;
}
char flip(char ch) {
if (ch == '?')
return '?';
if (ch == '0')
return '1';
return '0';
}
int sum[2][N], cnt[2][N];
void flip_dfs(int x, int fa) {
dep[x] = dep[fa] + 1;
if (dep[x] & 1) {
s[x] = flip(s[x]);
t[x] = flip(t[x]);
}
for (int y : G[x]) if (y != fa)
flip_dfs(y, x);
}
void dfs(int x, int fa) {
for (int i = 0; i < 2; i ++)
sum[i][x] = cnt[i][x] = 0;
sum[0][x] += (s[x] == '1'), sum[1][x] += (t[x] == '1');
cnt[0][x] += (s[x] == '?'), cnt[1][x] += (t[x] == '?');
for (int y : G[x]) if (y != fa) {
dfs(y, x);
for (int i = 0; i < 2; i ++)
sum[i][x] += sum[i][y], cnt[i][x] += cnt[i][y];
}
// debug("x: %d (%d, %d) (%d, %d)", x, sum[0][x], sum[1][x], cnt[0][x], cnt[1][x]);
for (int i = 0; i <= acnt[0] + asum[0] && i <= acnt[1] + asum[1]; i ++)
for (int j = 0; j <= cnt[0][x] && j + asum[0] <= i; j ++)
for (int k = 0; k <= cnt[1][x] && k + asum[1] <= i; k ++)
mod(ans, abs(sum[0][x] - sum[1][x] + j - k) * binom(cnt[0][x], j) % P * binom(cnt[1][x], k) % P * binom(acnt[0] - cnt[0][x], i - asum[0] - j) % P * binom(acnt[1] - cnt[1][x], i - asum[1] - k) % P);
}
void solve() {
cin >> n;
ans = 0;
for (int i = 1; i <= n; i ++)
G[i].clear();
for (int i = 1, x, y; i < n; i ++) {
cin >> x >> y;
G[x].push_back(y), G[y].push_back(x);
}
cin >> (s + 1) >> (t + 1);
for (int i = 0; i < 2; i ++)
asum[i] = acnt[i] = 0;
flip_dfs(1, 0);
for (int i = 1; i <= n; i ++) {
asum[0] += (s[i] == '1'), asum[1] += (t[i] == '1');
acnt[0] += (s[i] == '?'), acnt[1] += (t[i] == '?');
}
dfs(1, 0);
cout << ans << "\n";
}
}
int main() {
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
SOL::init();
int tt; cin >> tt;
while (tt --)
SOL::solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 7ms
memory: 11520kb
input:
3 2 1 2 00 11 3 1 2 2 3 ??? ??? 3 1 2 2 3 ??1 0?0
output:
1 16 1
result:
ok 3 number(s): "1 16 1"
Test #2:
score: 0
Accepted
time: 15ms
memory: 11520kb
input:
1000 23 1 2 1 3 1 4 2 5 5 6 4 7 3 8 4 9 8 10 8 11 8 12 1 13 7 14 10 15 7 16 7 17 5 18 18 19 12 20 9 21 21 22 6 23 00?10?0000??1?00111?010 011??1?10?01?110?0??101 6 1 2 1 3 1 4 4 5 3 6 000?10 1???01 25 1 2 2 3 2 4 4 5 5 6 2 7 4 8 5 9 7 10 8 11 11 12 5 13 11 14 3 15 6 16 14 17 1 18 4 19 6 20 4 21 5 22...
output:
53545 24 11724 2228 162 14 711 28 550 1680 52 2 13 988 9480 2342 626416 0 71780 1 88 39207 19448 4 37395 9602 3253496 38057200 1066 3 303732 1608 281022 11718 170 78 15 1219376 29364 9092 7 0 2 7014 1942448 170371 75845 167217 16554 64 904 564290 14822 1127090 1758504 1227646 14833316 14954550 36055...
result:
ok 1000 numbers
Test #3:
score: -100
Time Limit Exceeded
input:
1 3000 1 2 2 3 2 4 1 5 3 6 2 7 5 8 8 9 9 10 10 11 2 12 11 13 7 14 11 15 7 16 13 17 8 18 1 19 11 20 10 21 18 22 7 23 7 24 15 25 23 26 24 27 14 28 15 29 25 30 16 31 6 32 10 33 3 34 30 35 16 36 9 37 36 38 28 39 26 40 33 41 1 42 11 43 20 44 23 45 14 46 31 47 41 48 11 49 48 50 45 51 6 52 10 53 32 54 38 5...