QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#433028#3100. Meetings 2snpmrnhlol0 3ms10960kbC++143.5kb2024-06-07 22:39:192024-06-07 22:39:21

Judging History

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

  • [2024-06-07 22:39:21]
  • 评测
  • 测评结果:0
  • 用时:3ms
  • 内存:10960kb
  • [2024-06-07 22:39:19]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
const int N = 2e5;
const int inf = 2e9;
vector <int> e[N];
int bad[N];
int sub[N];
int gbsub[N];
int pr[N];
int ans[N + 1];
int n;
void dfs(int node, int p){
    gbsub[node] = 1;
    pr[node] = p;
    for(auto i:e[node]){
        if(i == p)continue;
        dfs(i,node);
        gbsub[node]+=gbsub[i];
    }
}
int get(int node, int p){
    if(pr[node] == p){
        return gbsub[node];
    }else{
        return n - gbsub[p];
    }
}
void cendecomp(int node){
    vector <array<int,3>> nodes;
    int sz = 0;
    int cen = -1,cennr = inf;
    auto dfs = [&](auto self, int node, int p) -> void{
        sz++;
        for(auto i:e[node]){
            if(i == p || bad[i])continue;
            self(self, i, node);
        }
    };
    auto dfs2 = [&](auto self, int node, int p) -> void{
        sub[node] = 1;
        int mx = -1;
        for(auto i:e[node]){
            if(i == p || bad[i])continue;
            self(self, i, node);
            sub[node]+=sub[i];
            mx = max(mx,sub[i]);
        }
        mx = max(mx,sz - sub[node]);
        if(mx < cennr){
            mx = cennr;
            cen = node;
        }
    };
    auto dfs3 = [&](auto self, int node, int p, int dpth, int orig) -> void{
        int nr = min(get(node,p),get(cen,orig));
        //cout<<node<<' '<<cen<<' '<<dpth<<'\n';
        ans[2*nr] = max(dpth,ans[2*nr]);
        for(auto i:e[node]){
            if(i == p || bad[i])continue;
            self(self, i, node, dpth + 1, orig);
        }
    };
    auto dfs4 = [&](auto self, int node, int p, int dpth, int orig) -> void{
        nodes.push_back({orig,dpth,get(node,p)});
        for(auto i:e[node]){
            if(i == p || bad[i])continue;
            self(self, i, node, dpth + 1, orig);
        }
    };
    dfs(dfs, node, -1);
    dfs2(dfs2, node, -1);
    for(auto i:e[cen]){
        if(bad[i])continue;
        dfs3(dfs3, i, cen, 2, i);
        dfs4(dfs4, i, cen, 1, i);
    }
    sort(nodes.begin(),nodes.end(),[&](auto a,auto b){
         return a[1] > b[1];
    });
    array <int,3> best[2] = {{-1,-1,-1},{-1,-1,-1}};
    for(int i = 0;i < nodes.size();i++){

        bool ok = 0;
        for(int j = 0;j < 2;j++){
            if(nodes[i][2] != -1 && nodes[i][0] != best[j][0]){
                ans[2*min(nodes[i][2],best[j][2])] = max(ans[2*min(nodes[i][2],best[j][2])],nodes[i][1] + best[j][1] + 1);
            }
            if(best[j][0] == nodes[i][0]){
                best[j][1] = max(best[j][1],nodes[i][1]);
                ok = 1;
            }
        }
        if(!ok){
            if(best[0][1] < nodes[i][1]){
                best[1] = best[0];
                best[0] = nodes[i];
            }else if(best[1][1] < nodes[i][1]){
                best[1] = nodes[i];
            }
        }
    }
    bad[cen] = 1;
    for(auto i:e[cen]){
        if(bad[i])continue;
        cendecomp(i);
    }
}
void solve(){
    cin>>n;
    for(int i = 0;i < n - 1;i++){
        int u,w;
        cin>>u>>w;
        u--;w--;
        e[w].push_back(u);
        e[u].push_back(w);
    }
    dfs(0, -1);
    cendecomp(0);
    for(int i = n;i >= 2;i--){
        if(i%2 == 0)ans[i] = max(ans[i],ans[i + 2]);
    }
    for(int i = 1;i <= n;i++){
        if(i%2 == 1)cout<<1<<'\n';
        else{
            cout<<max(ans[i],1)<<'\n';
        }
    }
}
int main(){
    int t;
    t = 1;
    while(t--)solve();
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Subtask #1:

score: 0
Wrong Answer

Test #1:

score: 4
Accepted
time: 0ms
memory: 10636kb

input:

1

output:

1

result:

ok single line: '1'

Test #2:

score: 4
Accepted
time: 0ms
memory: 8240kb

input:

2
2 1

output:

1
2

result:

ok 2 lines

Test #3:

score: 4
Accepted
time: 3ms
memory: 8248kb

input:

3
1 3
3 2

output:

1
3
1

result:

ok 3 lines

Test #4:

score: 4
Accepted
time: 0ms
memory: 8304kb

input:

14
12 14
12 9
14 2
12 6
12 7
6 4
3 4
1 4
12 8
13 1
10 12
11 6
6 5

output:

1
7
1
5
1
3
1
3
1
2
1
2
1
2

result:

ok 14 lines

Test #5:

score: 0
Wrong Answer
time: 2ms
memory: 10960kb

input:

14
10 14
3 10
14 13
1 3
3 5
3 11
12 14
14 6
11 8
2 3
7 8
9 7
1 4

output:

1
8
1
5
1
3
1
3
1
2
1
1
1
1

result:

wrong answer 4th lines differ - expected: '6', found: '5'

Subtask #2:

score: 0
Skipped

Dependency #1:

0%

Subtask #3:

score: 0
Skipped

Dependency #1:

0%