QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#446685 | #8518. Roars III | marher | TL | 855ms | 9772kb | C++14 | 1.1kb | 2024-06-17 15:07:35 | 2024-06-17 15:07:35 |
Judging History
answer
#pragma GCC optimize("Ofast","inline")
#include<bits/stdc++.h>
using namespace std;
const int N=4e5+50;
struct edge
{
int u,v,nxt;
}e[N];
int n,head[N],cnt,ans,fa[N],siz[N],dep[N],tot;
char s[N];
void dfs(int x,int f)
{
fa[x]=f;dep[x]=dep[f]+1;siz[x]=0;
if(s[x])tot+=dep[x],siz[x]++;
for(int i=head[x];i;i=e[i].nxt)
{
int v=e[i].v;
if(v!=f)dfs(v,x),siz[x]+=siz[v];
}
}
int chk(int x)
{
int t=x;
while(x){if(siz[x]<=0)return 0;x=fa[x];}
while(t)siz[t]--,t=fa[t];
return 1;
}
void solve(int rt)
{
tot=0;dfs(rt,0);
queue<int>q;q.push(rt);
while(!q.empty())
{
int x=q.front();q.pop();
if(chk(x))tot-=dep[x];
for(int i=head[x];i;i=e[i].nxt)if(e[i].v!=fa[x])q.push(e[i].v);
}
cout<<tot<<' ';
}
main()
{
// freopen("in.txt","r",stdin);
cin>>n>>(s+1);
for(int i=1;i<=n;i++)s[i]-='0';
for(int i=1,u,v;i<n;i++)
{
cin>>u>>v;
e[++cnt]=(edge){u,v,head[u]};head[u]=cnt;
e[++cnt]=(edge){v,u,head[v]};head[v]=cnt;
}
for(int i=1;i<=n;i++)solve(i);
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 7696kb
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: 9672kb
input:
1 0
output:
0
result:
ok 1 number(s): "0"
Test #3:
score: 0
Accepted
time: 2ms
memory: 9732kb
input:
1 1
output:
0
result:
ok 1 number(s): "0"
Test #4:
score: 0
Accepted
time: 1ms
memory: 7624kb
input:
2 10 1 2
output:
0 1
result:
ok 2 number(s): "0 1"
Test #5:
score: 0
Accepted
time: 1ms
memory: 7884kb
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: 7696kb
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: 2ms
memory: 9772kb
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: 9744kb
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: 0
Accepted
time: 2ms
memory: 9744kb
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:
34 29 29 33 34 37 34 29 31 30 49 31 41 33 27 36 34 29 29 27 30 36 30 27 34 36 38 46 29 27 38 36 50 27 33 36 30 33 30 27 36 40 34 34 31 33 38 40 36 30 36 30 37 36 30 27 29 33 34 36 32 34 40 27
result:
ok 64 numbers
Test #10:
score: 0
Accepted
time: 9ms
memory: 7952kb
input:
512 11100000100000011111111011010110001001101100011110001001111001011011000001010000100011011101001001101100010000011100000110101010100001001110000011010000111001000110010011000100000010010000100100000000100001001011100010100100101101010000111100110010001010010011101010001000111001001010111111111101...
output:
229 209 213 228 257 224 211 243 208 238 231 211 246 231 235 206 211 208 220 225 226 206 220 251 215 220 238 234 230 208 215 235 230 220 220 235 233 217 243 250 206 220 221 253 230 206 240 209 208 220 215 251 228 221 228 219 208 208 219 256 221 206 222 208 228 230 208 230 235 241 228 223 231 221 229 ...
result:
ok 512 numbers
Test #11:
score: 0
Accepted
time: 855ms
memory: 9768kb
input:
4096 0011111001111111001000100000001010100111001000010100110000110111011111111001001101101010011010001010111100111110001111000000000111001110100110100011011101110011101111001010011011110101111110000101111111100101101001001000000110100111100010110110011001111111011111100111001101111110110111000010101...
output:
1579 1639 1528 1542 1551 1523 1522 1553 1553 1532 1533 1551 1562 1534 1564 1568 1583 1559 1553 1537 1533 1568 1541 1566 1563 1557 1546 1537 1551 1586 1570 1548 1576 1587 1523 1541 1539 1522 1538 1565 1554 1589 1548 1614 1570 1570 1549 1527 1578 1520 1569 1581 1540 1540 1551 1579 1594 1550 1551 1550 ...
result:
ok 4096 numbers
Test #12:
score: -100
Time Limit Exceeded
input:
32768 010000100111010011101111111011001000100100101001100000110010100111111101110011000110001011001001100110111010010111000001110101000011101010010101111101001100100011101101010000001011001010100011011011001001111101011000001101101110101010001001011011101001010101010000011111100011110100011000001110...
output:
12964 12950 12999 13070 12980 12974 12927 12949 12952 12953 12999 12962 12972 12972 12951 12989 12948 12971 12930 12954 13012 12972 12946 12985 12946 12926 12996 13037 12915 12915 13011 13017 12939 13055 13044 12986 12986 12984 12968 12958 12960 12973 12981 12976 12992 13011 12991 12930 13001 13018 ...