QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#686998 | #7736. Red Black Tree | KafuuChinocpp# | WA | 165ms | 35240kb | C++14 | 3.7kb | 2024-10-29 16:40:54 | 2024-10-29 16:40:55 |
Judging History
answer
#include <cstdio>
#include <algorithm>
#include <vector>
#include <queue>
#include <deque>
using namespace std;
const int max1 = 1e5;
const int inf = 0x3f3f3f3f;
int T, n;
char s[max1 + 5];
vector <int> edge[max1 + 5];
int mindeep[max1 + 5], id[max1 + 5], cnt[max1 + 5];
vector <int> f[max1 + 5];
int ans[max1 + 5];
int tmp[max1 + 5], total;
deque <int> que;
void Dfs ( int now )
{
f[now].clear();
if ( edge[now].empty() )
{
mindeep[now] = 1;
id[now] = now;
f[now].push_back(s[now] == '1');
f[now].push_back(s[now] == '0');
cnt[now] = 0;
ans[now] = 0;
}
else
{
mindeep[now] = inf;
for ( auto v : edge[now] )
Dfs(v), mindeep[now] = min(mindeep[now], mindeep[v]);
++mindeep[now];
if ( edge[now].size() == 1 )
{
id[now] = id[edge[now][0]];
cnt[now] = cnt[edge[now][0]] + (s[now] == '1');
ans[now] = ans[id[now]];
}
else
{
id[now] = now;
cnt[now] = 0;
for ( auto v : edge[now] )
{
total = mindeep[id[v]];
for ( int i = 0; i <= total; i ++ )
tmp[i] = f[id[v]][i];
f[id[v]].clear();
int Min = inf; que.clear();
for ( int i = 0; i <= mindeep[now] - 1; i ++ )
{
if ( i - cnt[v] >= 0 && i - cnt[v] <= total )
Min = min(Min, tmp[i - cnt[v]] - (i - cnt[v]));
if ( i <= total )
{
while ( !que.empty() && tmp[que.back()] + que.back() >= tmp[i] + i )
que.pop_back();
que.push_back(i);
}
while ( !que.empty() && que.front() <= i - cnt[v] )
que.pop_front();
f[id[v]].push_back(inf);
f[id[v]].back() = min(f[id[v]].back(), Min + i - cnt[v]);
if ( !que.empty() )
f[id[v]].back() = min(f[id[v]].back(), tmp[que.front()] + que.front() + cnt[v] - i);
// printf("now = %d i = %d v = %d f = %d\n", now, i, v, f[id[v]].back());
}
}
ans[now] = inf;
for ( int i = 0; i <= mindeep[now]; i ++ )
{
f[now].push_back(inf);
int sum;
if ( i != 0 )
{
sum = s[now] == '0';
for ( auto v : edge[now] )
sum += f[id[v]][i - 1];
f[now].back() = min(f[now].back(), sum);
}
if ( i != mindeep[now] )
{
sum = s[now] == '1';
for ( auto v : edge[now] )
sum += f[id[v]][i];
f[now].back() = min(f[now].back(), sum);
}
ans[now] = min(ans[now], f[now].back());
}
}
}
return;
}
void Work ()
{
int fa;
scanf("%d%s", &n, s + 1);
for ( int i = 1; i <= n; i ++ )
edge[i].clear();
for ( int i = 2; i <= n; i ++ )
scanf("%d", &fa), edge[fa].push_back(i);
Dfs(1);
for ( int i = 1; i <= n; i ++ )
printf("%d ", ans[i]);
printf("\n");
return;
}
int main ()
{
scanf("%d", &T);
while ( T -- )
Work();
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 2ms
memory: 9968kb
input:
2 9 101011110 1 1 3 3 3 6 2 2 4 1011 1 1 3
output:
4 1 2 0 0 0 0 0 0 2 0 0 0
result:
ok 2 lines
Test #2:
score: -100
Wrong Answer
time: 165ms
memory: 35240kb
input:
6107 12 000000001000 1 2 3 2 5 4 4 7 3 8 11 19 1100111101111011110 1 2 1 1 4 5 2 4 3 2 2 7 10 2 11 3 15 5 7 0111110 1 1 2 2 1 5 3 000 1 1 7 1000011 1 2 3 3 5 4 7 0001001 1 1 1 3 5 3 8 00111000 1 1 3 2 5 2 7 11 11111110111 1 1 1 4 5 4 5 2 5 1 15 110101101000010 1 2 3 2 1 5 2 5 6 5 8 7 9 14 10 0101000...
output:
1 1 1 1 0 0 0 0 0 0 0 0 6 2 0 1 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 0 1 0 0 0 0 2 1 0 0 0 0 0 0 4 0 0 2 1 0 0 0 0 0 0 4 3 0 0 2 0 0 0 0 0 0 0 0 0 0 2 0 1 0 0 0 0 0 0 0 6 5 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 1 1 0 0 0 5 1 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0...
result:
wrong answer 122nd lines differ - expected: '4 1 1 0 0 0 0 0 0 0 0 0', found: '3 1 1 0 0 0 0 0 0 0 0 0 '