답안 #914871

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
914871 2024-01-22T19:39:08 Z alexander707070 Meetings 2 (JOI21_meetings2) C++14
20 / 100
4000 ms 22052 KB
#include<bits/stdc++.h>
#define MAXN 300007
using namespace std;

struct dude{
    int h,l;
};

int n,a,b,sz[MAXN],root,parent[MAXN],pt[MAXN],ans[MAXN];
int dist[MAXN],maxs;
vector<int> v[MAXN];
bool used[MAXN];

void dfs(int x,int p){
    sz[x]=1; parent[x]=p;

    for(int i=0;i<v[x].size();i++){
        if(v[x][i]==p)continue;
        dfs(v[x][i],x);
        sz[x]+=sz[v[x][i]];
    }
}

int centroid(int x,int p){
    for(int i=0;i<v[x].size();i++){
        if(v[x][i]==p)continue;
        if(sz[v[x][i]]>n/2){
            return centroid(v[x][i],x);
        }
    }

    return x;
}

void dfs1(int x,int p,int d){
    dist[x]=d;
    for(int i=0;i<v[x].size();i++){
        if(v[x][i]==p or used[v[x][i]])continue;
        dfs1(v[x][i],x,d+1);
    }
}

int diameter(){
    for(int i=1;i<=n;i++){
        if(used[i])continue;
        dfs1(i,0,0); break;
    }

    maxs=0;
    for(int i=1;i<=n;i++){
        if(used[i])continue;
        if(dist[i]>dist[maxs])maxs=i;
    }
    
    dfs1(maxs,0,0);

    maxs=0;
    for(int i=1;i<=n;i++){
        if(used[i])continue;
        maxs=max(maxs,dist[i]);
    }

    return maxs+1;
}

int main(){

    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    cin>>n;
    for(int i=1;i<=n-1;i++){
        cin>>a>>b;
        v[a].push_back(b);
        v[b].push_back(a);
    }

    dfs(1,0);
    root=centroid(1,0);
    dfs(root,0);

    for(int i=1;i<=n;i++)pt[i]=i;

    for(int k=1;k<=n/2;k++){
        for(int i=1;i<=n;i++){
            while(sz[pt[i]]<k){
                used[pt[i]]=true;
                pt[i]=parent[pt[i]];
            }
        }

        ans[2*k]=diameter();
    }

    for(int i=1;i<=n;i++){
        if(i%2==1)cout<<"1\n";
        else cout<<ans[i]<<"\n";
    }

    return 0;
}

Compilation message

meetings2.cpp: In function 'void dfs(int, int)':
meetings2.cpp:17:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   17 |     for(int i=0;i<v[x].size();i++){
      |                 ~^~~~~~~~~~~~
meetings2.cpp: In function 'int centroid(int, int)':
meetings2.cpp:25:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   25 |     for(int i=0;i<v[x].size();i++){
      |                 ~^~~~~~~~~~~~
meetings2.cpp: In function 'void dfs1(int, int, int)':
meetings2.cpp:37:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   37 |     for(int i=0;i<v[x].size();i++){
      |                 ~^~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 12632 KB Output is correct
2 Correct 3 ms 12636 KB Output is correct
3 Correct 3 ms 12636 KB Output is correct
4 Correct 2 ms 12636 KB Output is correct
5 Correct 3 ms 12636 KB Output is correct
6 Correct 3 ms 12636 KB Output is correct
7 Correct 2 ms 12636 KB Output is correct
8 Correct 3 ms 12636 KB Output is correct
9 Correct 3 ms 12636 KB Output is correct
10 Correct 3 ms 12636 KB Output is correct
11 Correct 3 ms 12636 KB Output is correct
12 Correct 3 ms 12636 KB Output is correct
13 Correct 3 ms 12636 KB Output is correct
14 Correct 2 ms 12636 KB Output is correct
15 Correct 3 ms 12636 KB Output is correct
16 Correct 3 ms 12732 KB Output is correct
17 Correct 3 ms 12636 KB Output is correct
18 Correct 3 ms 12764 KB Output is correct
19 Correct 3 ms 12636 KB Output is correct
20 Correct 3 ms 12636 KB Output is correct
21 Correct 2 ms 12636 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 12632 KB Output is correct
2 Correct 3 ms 12636 KB Output is correct
3 Correct 3 ms 12636 KB Output is correct
4 Correct 2 ms 12636 KB Output is correct
5 Correct 3 ms 12636 KB Output is correct
6 Correct 3 ms 12636 KB Output is correct
7 Correct 2 ms 12636 KB Output is correct
8 Correct 3 ms 12636 KB Output is correct
9 Correct 3 ms 12636 KB Output is correct
10 Correct 3 ms 12636 KB Output is correct
11 Correct 3 ms 12636 KB Output is correct
12 Correct 3 ms 12636 KB Output is correct
13 Correct 3 ms 12636 KB Output is correct
14 Correct 2 ms 12636 KB Output is correct
15 Correct 3 ms 12636 KB Output is correct
16 Correct 3 ms 12732 KB Output is correct
17 Correct 3 ms 12636 KB Output is correct
18 Correct 3 ms 12764 KB Output is correct
19 Correct 3 ms 12636 KB Output is correct
20 Correct 3 ms 12636 KB Output is correct
21 Correct 2 ms 12636 KB Output is correct
22 Correct 21 ms 12940 KB Output is correct
23 Correct 21 ms 12888 KB Output is correct
24 Correct 26 ms 12892 KB Output is correct
25 Correct 23 ms 12892 KB Output is correct
26 Correct 21 ms 12892 KB Output is correct
27 Correct 22 ms 12952 KB Output is correct
28 Correct 20 ms 12892 KB Output is correct
29 Correct 21 ms 12892 KB Output is correct
30 Correct 21 ms 12936 KB Output is correct
31 Correct 21 ms 12892 KB Output is correct
32 Correct 127 ms 13060 KB Output is correct
33 Correct 140 ms 13180 KB Output is correct
34 Correct 23 ms 12892 KB Output is correct
35 Correct 28 ms 12892 KB Output is correct
36 Correct 25 ms 12892 KB Output is correct
37 Correct 29 ms 12892 KB Output is correct
38 Correct 68 ms 13056 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 12632 KB Output is correct
2 Correct 3 ms 12636 KB Output is correct
3 Correct 3 ms 12636 KB Output is correct
4 Correct 2 ms 12636 KB Output is correct
5 Correct 3 ms 12636 KB Output is correct
6 Correct 3 ms 12636 KB Output is correct
7 Correct 2 ms 12636 KB Output is correct
8 Correct 3 ms 12636 KB Output is correct
9 Correct 3 ms 12636 KB Output is correct
10 Correct 3 ms 12636 KB Output is correct
11 Correct 3 ms 12636 KB Output is correct
12 Correct 3 ms 12636 KB Output is correct
13 Correct 3 ms 12636 KB Output is correct
14 Correct 2 ms 12636 KB Output is correct
15 Correct 3 ms 12636 KB Output is correct
16 Correct 3 ms 12732 KB Output is correct
17 Correct 3 ms 12636 KB Output is correct
18 Correct 3 ms 12764 KB Output is correct
19 Correct 3 ms 12636 KB Output is correct
20 Correct 3 ms 12636 KB Output is correct
21 Correct 2 ms 12636 KB Output is correct
22 Correct 21 ms 12940 KB Output is correct
23 Correct 21 ms 12888 KB Output is correct
24 Correct 26 ms 12892 KB Output is correct
25 Correct 23 ms 12892 KB Output is correct
26 Correct 21 ms 12892 KB Output is correct
27 Correct 22 ms 12952 KB Output is correct
28 Correct 20 ms 12892 KB Output is correct
29 Correct 21 ms 12892 KB Output is correct
30 Correct 21 ms 12936 KB Output is correct
31 Correct 21 ms 12892 KB Output is correct
32 Correct 127 ms 13060 KB Output is correct
33 Correct 140 ms 13180 KB Output is correct
34 Correct 23 ms 12892 KB Output is correct
35 Correct 28 ms 12892 KB Output is correct
36 Correct 25 ms 12892 KB Output is correct
37 Correct 29 ms 12892 KB Output is correct
38 Correct 68 ms 13056 KB Output is correct
39 Execution timed out 4037 ms 22052 KB Time limit exceeded
40 Halted 0 ms 0 KB -