QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#378032 | #8518. Roars III | Days_of_Future_Past# | WA | 2ms | 26040kb | C++17 | 2.8kb | 2024-04-05 23:02:24 | 2024-04-05 23:02:26 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
const int inf = 1e9 + 7;
#define pb push_back
typedef long long LL;
#define fi first
#define se second
const int mod = 1e9 + 7;
const int N = 200022;
char st[N];
int dp[N], sd[N], ans[N], fa[N], lc[N], flc[N], slc[N][2], slcid[N][2];
int len[N];
map<int, int> sl[N];
vector<int> e[N];
void f(int v) {
int son_size = sl[v].size() == 0 ? 0 : sl[v].size() == 1 ? (sl[v].begin())->se : 2;
len[v] = (st[v] == '1' ? 1 : son_size >= 2 ? 1 : 0) + (sl[v].empty() ? 0 : (sl[v].rbegin())->fi);
dp[v] = sd[v];
if(st[v] == '0') {
dp[v] += (sl[v].empty() ? 0 : (sl[v].rbegin())->fi);
}
}
void upd(int v, int val, int y) {
if(slc[v][0] < val) {
slc[v][1] = slc[v][0];
slcid[v][1] = slcid[v][0];
slc[v][0] = val;
slcid[v][0] = y;
} else if(slc[v][1] < val) {
slc[v][1] = val;
slcid[v][1] = y;
}
}
void inc(int v, int y, int delta) {
if(len[y] == 0) {
return;
}
if(0 == (sl[v][len[y]] += delta)) {
sl[v].erase(len[y]);
}
}
void dfs(int v, int fa = -1) {
len[v] = st[v] == '1';
sd[v] = 0;
for(int y : e[v]) {
if(y == fa) {
continue;
}
dfs(y, v);
::fa[y] = v;
sd[v] += dp[y];
inc(v, y, 1);
}
f(v);
//printf("dp[%d] = %d\n", v, dp[v]);
}
void dfa(int v, int fa = -1) {
if(fa != -1) {
flc[v] = flc[fa];
if(v == slcid[fa][0]) {
flc[v] = max(flc[v], (st[fa] == '1') + slc[fa][1]);
} else {
flc[v] = max(flc[v], (st[fa] == '1') + slc[fa][0]);
}
} else {
flc[v] = 0;
}
for(int y : e[v]) {
if(y == fa) {
continue;
}
dfa(y, v);
}
}
void sfd(int v, int fa = -1) {
if(fa != -1) {
sd[fa] -= dp[v];
inc(fa, v, -1);
f(fa);
sd[v] += dp[fa];
inc(v, fa, 1);
f(v);
}
ans[v] = dp[v];
//printf("!%d\n", dp[v]);
for(int y : e[v]) {
if(y == fa) {
continue;
}
sfd(y, v);
}
if(fa != -1) {
sd[v] -= dp[fa];
inc(v, fa, -1);
f(v);
sd[fa] += dp[v];
inc(fa, v, 1);
f(fa);
}
}
int main() {
int n;
scanf("%d", &n);
scanf("%s", st);
::fa[0] = -1;
for(int i = 0; i < n - 1; i++) {
int x, y;
scanf("%d%d", &x, &y);
x--;
y--;
e[x].pb(y);
e[y].pb(x);
}
dfs(4);
dfa(4);
sfd(4);
//for(int i = 0; i < n; i++) {
// printf("i = %d, f%ds%ds%d\n", i, flc[i], slc[i][0], slc[i][1]);
//}
for(int i = 0; i < n; i++) {
printf("%d%c", ans[i], i == n - 1 ? '\n' : ' ');
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 23332kb
input:
5 10101 1 2 2 3 2 4 4 5
output:
2 2 2 3 3
result:
ok 5 number(s): "2 2 2 3 3"
Test #2:
score: 0
Accepted
time: 0ms
memory: 26040kb
input:
1 0
output:
0
result:
ok 1 number(s): "0"
Test #3:
score: 0
Accepted
time: 0ms
memory: 23092kb
input:
1 1
output:
0
result:
ok 1 number(s): "0"
Test #4:
score: -100
Wrong Answer
time: 2ms
memory: 23556kb
input:
2 10 1 2
output:
0 0
result:
wrong answer 2nd numbers differ - expected: '1', found: '0'