QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#378036 | #8518. Roars III | Days_of_Future_Past# | WA | 5ms | 24172kb | C++17 | 2.7kb | 2024-04-05 23:06:37 | 2024-04-05 23:06:37 |
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) {
len[v] = (st[v] == '1' ? 1 : (!sl[v].empty() && (sl[v].rbegin())->second >= 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) {
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(0);
dfa(0);
sfd(0);
//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: 5ms
memory: 23860kb
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: 22740kb
input:
1 0
output:
0
result:
ok 1 number(s): "0"
Test #3:
score: 0
Accepted
time: 2ms
memory: 23216kb
input:
1 1
output:
0
result:
ok 1 number(s): "0"
Test #4:
score: 0
Accepted
time: 2ms
memory: 23704kb
input:
2 10 1 2
output:
0 1
result:
ok 2 number(s): "0 1"
Test #5:
score: 0
Accepted
time: 0ms
memory: 23344kb
input:
3 100 2 3 2 1
output:
0 1 2
result:
ok 3 number(s): "0 1 2"
Test #6:
score: 0
Accepted
time: 0ms
memory: 23080kb
input:
4 1100 4 1 4 3 4 2
output:
1 1 3 1
result:
ok 4 number(s): "1 1 3 1"
Test #7:
score: 0
Accepted
time: 0ms
memory: 23164kb
input:
5 10100 4 5 1 3 2 1 3 5
output:
0 2 0 4 2
result:
ok 5 number(s): "0 2 0 4 2"
Test #8:
score: 0
Accepted
time: 0ms
memory: 24172kb
input:
8 11001000 4 2 7 2 5 7 6 2 3 1 6 3 7 8
output:
5 3 5 5 4 4 4 6
result:
ok 8 numbers
Test #9:
score: -100
Wrong Answer
time: 0ms
memory: 22700kb
input:
64 1110101001010110011100110000010100011011010001000111010110100101 57 60 58 63 7 43 64 9 19 8 62 17 4 40 41 18 34 56 46 14 41 36 57 26 46 58 16 32 59 1 8 17 17 13 34 29 55 10 43 5 34 8 28 36 6 10 37 21 11 48 2 8 56 55 3 8 56 61 53 52 49 51 20 30 52 39 57 22 9 49 18 16 9 27 50 52 38 40 59 43 2 18 31...
output:
29 27 27 31 29 32 29 27 28 27 39 29 34 30 25 32 30 27 27 25 27 32 27 25 31 32 33 39 28 25 33 32 40 26 31 32 27 31 27 26 32 34 29 31 29 30 33 34 31 27 31 27 32 31 27 25 27 30 29 32 29 30 34 25
result:
wrong answer 1st numbers differ - expected: '34', found: '29'