QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#449156#8518. Roars IIIGraphcityWA 2ms10012kbC++203.1kb2024-06-20 19:12:022024-06-20 19:12:03

Judging History

你现在查看的是最新测评结果

  • [2024-06-20 19:12:03]
  • 评测
  • 测评结果:WA
  • 用时:2ms
  • 内存:10012kb
  • [2024-06-20 19:12:02]
  • 提交

answer

#include<bits/stdc++.h>
#define ll long long
#define For(i,a,b) for(int i=(a);i<=(b);++i)
#define Rof(i,a,b) for(int i=(a);i>=(b);--i)
using namespace std;
const int Maxn=2e5;

int n,h[Maxn+5],anc[Maxn+5]; ll sf[Maxn+5],sg[Maxn+5];
struct Node{int mx,se;} f[Maxn+5],g[Maxn+5];
inline Node operator+(Node a,Node b)
{
    if(a.mx>=b.mx) return Node{a.mx,max(a.se,b.mx)};
    else return Node{b.mx,max(b.se,a.mx)};
}
vector<int> v[Maxn+5];

inline void dfs1(int x,int fa)
{
    anc[x]=fa;
    for(auto y:v[x]) if(y!=fa) dfs1(y,x),sf[x]=sf[x]+sf[y];
    if(h[x])
    {
        for(auto y:v[x]) if(y!=fa) f[x]=f[x]+f[y];
        f[x].mx++; if(f[x].se) f[x].se++;
    }
    else
    {
        vector<int> w; for(auto y:v[x]) if(y!=fa)
        {
            if(f[y].mx) w.push_back(f[y].mx);
            if(f[y].se) w.push_back(f[y].se);
        } sort(w.begin(),w.end());
        if(!w.empty()) sf[x]+=w.back(),w.back()--;
        sort(w.begin(),w.end());
        if(!w.empty()) f[x].mx=w.back()+1,w.pop_back();
        if(!w.empty()) f[x].se=w.back()+1;
    }
}
inline void dfs2(int x,int fa)
{
    static Node pre[Maxn+5],suf[Maxn+5]; Node cur=g[x]; ll sum=sg[x];
    for(auto y:v[x]) if(y!=fa) pre[y]=cur,cur=cur+f[y],sum+=sf[y];
    reverse(v[x].begin(),v[x].end()),cur=Node{0,0};
    for(auto y:v[x]) if(y!=fa) sg[y]+=(sum-sf[y]),suf[y]=cur,cur=cur+f[y];
    reverse(v[x].begin(),v[x].end());
    if(h[x])
    {
        for(auto y:v[x]) if(y!=fa)
            {g[y]=pre[y]+suf[y],g[y].mx++; if(g[y].se) g[y].se++;}
    }
    else
    {
        multiset<int> st;
        if(g[x].mx) st.insert(g[x].mx); if(g[x].se) st.insert(g[x].se);
        for(auto y:v[x]) if(y!=fa) {if(f[y].mx) st.insert(f[y].mx); if(f[y].se) st.insert(f[y].se);}
        for(auto y:v[x]) if(y!=fa)
        {
            if(f[y].mx) st.erase(st.find(f[y].mx)); if(f[y].se) st.erase(st.find(f[y].se));
            int res=-1; if(!st.empty()) res=*prev(st.end()),st.erase(prev(st.end())),st.insert(res-1),sg[y]+=res;
            int s1=-1; if(!st.empty()) s1=(*prev(st.end()))+1,st.erase(prev(st.end())),g[y].mx=s1;
            if(!st.empty()) g[y].se=*prev(st.end())+1;
            if(s1>=0) st.insert(s1-1); if(res>=0) st.erase(st.find(res-1)),st.insert(res);
            if(f[y].mx) st.insert(f[y].mx); if(f[y].se) st.insert(f[y].se);
        }
    }
    for(auto y:v[x]) if(y!=fa) dfs2(y,x);
}

int main()
{
    // freopen("1.in","r",stdin);

    cin>>n; For(i,1,n) scanf("%1d",&h[i]);
    For(i,1,n-1)
    {
        int a,b; cin>>a>>b;
        v[a].push_back(b),v[b].push_back(a);
    }
    For(rt,1,n)
    {
        For(i,1,n) f[i].mx=f[i].se=sf[i]=0;
        dfs1(rt,0); printf("%d ",sf[rt]);
    } return 0;
    
    dfs1(1,0),dfs2(1,0);
    // cerr<<g[5].mx<<' '<<g[5].se<<' '<<sg[5]<<endl;
    For(i,1,n)
    {
        if(i==1) {printf("%lld ",sf[1]); continue;}
        if(h[i]) {printf("%lld ",sf[i]+sg[i]); continue;}
        ll ans=sg[i]; int mx=g[i].mx;
        for(auto j:v[i]) if(j!=anc[i]) ans+=sf[j],mx=max(mx,f[j].mx);
        if(mx) ans+=mx;
        printf("%lld ",ans);
    }
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 2ms
memory: 10012kb

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: 1ms
memory: 9972kb

input:

1
0

output:

0 

result:

ok 1 number(s): "0"

Test #3:

score: 0
Accepted
time: 1ms
memory: 7916kb

input:

1
1

output:

0 

result:

ok 1 number(s): "0"

Test #4:

score: 0
Accepted
time: 1ms
memory: 7916kb

input:

2
10
1 2

output:

0 1 

result:

ok 2 number(s): "0 1"

Test #5:

score: 0
Accepted
time: 0ms
memory: 9964kb

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: 1ms
memory: 9992kb

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: 1ms
memory: 7912kb

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: 1ms
memory: 7992kb

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: 1ms
memory: 9948kb

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 47 31 39 33 27 35 33 29 29 27 30 35 30 27 34 35 38 44 29 27 38 35 48 27 33 35 30 33 30 27 35 39 34 34 31 33 38 39 36 30 36 30 37 36 30 27 29 33 34 35 32 33 39 27 

result:

wrong answer 11th numbers differ - expected: '49', found: '47'