답안 #386845

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
386845 2021-04-07T13:29:00 Z BartolM Meetings 2 (JOI21_meetings2) C++17
100 / 100
3594 ms 39740 KB
#define DEBUG 0
#include <bits/stdc++.h>

using namespace std;

#define X first
#define Y second
#define mp make_pair
#define pb push_back
typedef long long ll;
typedef pair <int, int> pii;
typedef pair <int, pii> pip;
typedef pair <pii, int> ppi;
typedef pair <ll, ll> pll;

const int INF=0x3f3f3f3f;
const int N=2e5+5;
const int OFF=(1<<18);

int n;
vector <int> ls[N], cnt[N], svi;
int dp[N], sol[N], rip[N];
int T[2*OFF];
vector <pii> v;//(vel, dub)

void update(int pos, int val) {
    for (pos+=OFF; pos; pos/=2) T[pos]=max(T[pos], val);
}

void reset(int pos) {
    for (pos+=OFF; pos; pos/=2) T[pos]=-INF;
}

int query(int a, int b,  int pos=1, int lo=0, int hi=OFF) {
    if (lo>=b || hi<=a) return -INF;
    if (lo>=a && hi<=b) return T[pos];
    int mid=(lo+hi)/2;
    return max(query(a, b, pos*2, lo, mid), query(a, b, pos*2+1, mid, hi));
}

void calc(int node, int par) {
    dp[node]=1;
    for (int sus:ls[node]) {
        if (sus==par || rip[sus]) continue;
        calc(sus, node);
        dp[node]+=dp[sus];
    }
}

int nadji(int node, int par, int uk) {
    for (int sus:ls[node]) {
        if (sus==par || rip[sus]) continue;
        if (dp[sus]>uk/2) return nadji(sus, node, uk);
    }
    return node;
}

void ubaci(int node, int par, int d) {
    int ind_par=-1;
    for (int i=0; i<ls[node].size(); ++i) {
        int sus=ls[node][i];
        if (rip[sus]) continue;
        if (sus==par) ind_par=i;
        else ubaci(sus, node, d+1);
    }
    v.pb(mp(n-cnt[node][ind_par], d));
    svi.pb(n-cnt[node][ind_par]);
}

void rek(int node) {
    calc(node, 0);
    int cent=nadji(node, 0, dp[node]);

    #if DEBUG
        printf("\ncent =========== %d\n", cent);
    #endif // DEBUG

    svi.clear();
    for (int i=0; i<ls[cent].size(); ++i) {
        v.clear();
        int sus=ls[cent][i], siz=n-cnt[cent][i];
        if (rip[sus]) continue;
        ubaci(sus, cent, 1);

        #if DEBUG
            printf("sus: %d\n", sus);
        #endif // DEBUG

        for (pii pp:v) {
            #if DEBUG
                printf("(%d, %d) ", pp.X, pp.Y);
            #endif

            if (pp.X>=siz) sol[siz]=max(sol[siz], pp.Y+1);
            else sol[pp.X]=max(sol[pp.X], pp.Y+1);
            sol[pp.X]=max(sol[pp.X], query(pp.X, n+1)+pp.Y+1);
        }
        for (pii pp:v) update(pp.X, pp.Y);
        #if DEBUG
            printf("\n");
        #endif // DEBUG

    }
    for (int x:svi) reset(x);

    svi.clear();
    for (int i=(int)ls[cent].size()-1; i>=0; --i) {
        int sus=ls[cent][i];
        if (rip[sus]) continue;
        v.clear();
        ubaci(sus, cent, 1);
        for (pii pp:v) sol[pp.X]=max(sol[pp.X], query(pp.X, n)+pp.Y+1);
        for (pii pp:v) update(pp.X, pp.Y);
    }
    for (int x:svi) reset(x);

    rip[cent]=1;
    for (int sus:ls[cent]) if (!rip[sus]) rek(sus);
}

void prec(int node, int par) {
    dp[node]=1;
    int ind_par=-1;
    for (int i=0; i<ls[node].size(); ++i) {
        int sus=ls[node][i];
        if (sus==par) ind_par=i, cnt[node].pb(0);
        else {
            prec(sus, node);
            dp[node]+=dp[sus];
            cnt[node].pb(dp[sus]);
        }
    }
    if (ind_par>-1) cnt[node][ind_par]=n-dp[node];
}

void load() {
    scanf("%d", &n);
    for (int i=0; i<n-1; ++i) {
        int a, b;
        scanf("%d %d", &a, &b);
        ls[a].pb(b); ls[b].pb(a);
    }
}

int main() {
    load();
    prec(1, 0);

    #if DEBUG
        for (int i=1; i<=n; ++i) {
            printf("node: %d\nls: ", i);
            for (int sus:ls[i]) printf("%d ", sus);
            printf("\ncnt: ");
            for (int x:cnt[i]) printf("%d ", x);
            printf("\n\n");
        }
    #endif // DEBUG


    fill(T, T+2*OFF, -INF);
    rek(1);

    sol[n]=max(sol[n], 1);
    for (int i=n-1; i>=1; --i) sol[i]=max(sol[i], sol[i+1]);
    for (int i=1; i<=n; ++i) printf("%d\n", i%2 ? 1 : sol[i/2]);

    return 0;
}

Compilation message

meetings2.cpp: In function 'void ubaci(int, int, int)':
meetings2.cpp:60:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   60 |     for (int i=0; i<ls[node].size(); ++i) {
      |                   ~^~~~~~~~~~~~~~~~
meetings2.cpp: In function 'void rek(int)':
meetings2.cpp:79:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   79 |     for (int i=0; i<ls[cent].size(); ++i) {
      |                   ~^~~~~~~~~~~~~~~~
meetings2.cpp: In function 'void prec(int, int)':
meetings2.cpp:124:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  124 |     for (int i=0; i<ls[node].size(); ++i) {
      |                   ~^~~~~~~~~~~~~~~~
meetings2.cpp: In function 'void load()':
meetings2.cpp:137:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  137 |     scanf("%d", &n);
      |     ~~~~~^~~~~~~~~~
meetings2.cpp:140:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  140 |         scanf("%d %d", &a, &b);
      |         ~~~~~^~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 11756 KB Output is correct
2 Correct 9 ms 11756 KB Output is correct
3 Correct 10 ms 11756 KB Output is correct
4 Correct 9 ms 11756 KB Output is correct
5 Correct 9 ms 11756 KB Output is correct
6 Correct 9 ms 11756 KB Output is correct
7 Correct 10 ms 11756 KB Output is correct
8 Correct 9 ms 11756 KB Output is correct
9 Correct 9 ms 11756 KB Output is correct
10 Correct 9 ms 11756 KB Output is correct
11 Correct 9 ms 11756 KB Output is correct
12 Correct 9 ms 11756 KB Output is correct
13 Correct 9 ms 11756 KB Output is correct
14 Correct 9 ms 11756 KB Output is correct
15 Correct 9 ms 11756 KB Output is correct
16 Correct 9 ms 11756 KB Output is correct
17 Correct 9 ms 11756 KB Output is correct
18 Correct 9 ms 11756 KB Output is correct
19 Correct 9 ms 11756 KB Output is correct
20 Correct 9 ms 11756 KB Output is correct
21 Correct 9 ms 11756 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 11756 KB Output is correct
2 Correct 9 ms 11756 KB Output is correct
3 Correct 10 ms 11756 KB Output is correct
4 Correct 9 ms 11756 KB Output is correct
5 Correct 9 ms 11756 KB Output is correct
6 Correct 9 ms 11756 KB Output is correct
7 Correct 10 ms 11756 KB Output is correct
8 Correct 9 ms 11756 KB Output is correct
9 Correct 9 ms 11756 KB Output is correct
10 Correct 9 ms 11756 KB Output is correct
11 Correct 9 ms 11756 KB Output is correct
12 Correct 9 ms 11756 KB Output is correct
13 Correct 9 ms 11756 KB Output is correct
14 Correct 9 ms 11756 KB Output is correct
15 Correct 9 ms 11756 KB Output is correct
16 Correct 9 ms 11756 KB Output is correct
17 Correct 9 ms 11756 KB Output is correct
18 Correct 9 ms 11756 KB Output is correct
19 Correct 9 ms 11756 KB Output is correct
20 Correct 9 ms 11756 KB Output is correct
21 Correct 9 ms 11756 KB Output is correct
22 Correct 29 ms 12160 KB Output is correct
23 Correct 29 ms 12140 KB Output is correct
24 Correct 29 ms 12180 KB Output is correct
25 Correct 29 ms 12140 KB Output is correct
26 Correct 28 ms 12140 KB Output is correct
27 Correct 29 ms 12268 KB Output is correct
28 Correct 28 ms 12140 KB Output is correct
29 Correct 28 ms 12156 KB Output is correct
30 Correct 28 ms 12180 KB Output is correct
31 Correct 28 ms 12140 KB Output is correct
32 Correct 43 ms 12396 KB Output is correct
33 Correct 46 ms 12396 KB Output is correct
34 Correct 15 ms 12140 KB Output is correct
35 Correct 14 ms 12140 KB Output is correct
36 Correct 30 ms 12140 KB Output is correct
37 Correct 18 ms 12140 KB Output is correct
38 Correct 26 ms 12268 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 11756 KB Output is correct
2 Correct 9 ms 11756 KB Output is correct
3 Correct 10 ms 11756 KB Output is correct
4 Correct 9 ms 11756 KB Output is correct
5 Correct 9 ms 11756 KB Output is correct
6 Correct 9 ms 11756 KB Output is correct
7 Correct 10 ms 11756 KB Output is correct
8 Correct 9 ms 11756 KB Output is correct
9 Correct 9 ms 11756 KB Output is correct
10 Correct 9 ms 11756 KB Output is correct
11 Correct 9 ms 11756 KB Output is correct
12 Correct 9 ms 11756 KB Output is correct
13 Correct 9 ms 11756 KB Output is correct
14 Correct 9 ms 11756 KB Output is correct
15 Correct 9 ms 11756 KB Output is correct
16 Correct 9 ms 11756 KB Output is correct
17 Correct 9 ms 11756 KB Output is correct
18 Correct 9 ms 11756 KB Output is correct
19 Correct 9 ms 11756 KB Output is correct
20 Correct 9 ms 11756 KB Output is correct
21 Correct 9 ms 11756 KB Output is correct
22 Correct 29 ms 12160 KB Output is correct
23 Correct 29 ms 12140 KB Output is correct
24 Correct 29 ms 12180 KB Output is correct
25 Correct 29 ms 12140 KB Output is correct
26 Correct 28 ms 12140 KB Output is correct
27 Correct 29 ms 12268 KB Output is correct
28 Correct 28 ms 12140 KB Output is correct
29 Correct 28 ms 12156 KB Output is correct
30 Correct 28 ms 12180 KB Output is correct
31 Correct 28 ms 12140 KB Output is correct
32 Correct 43 ms 12396 KB Output is correct
33 Correct 46 ms 12396 KB Output is correct
34 Correct 15 ms 12140 KB Output is correct
35 Correct 14 ms 12140 KB Output is correct
36 Correct 30 ms 12140 KB Output is correct
37 Correct 18 ms 12140 KB Output is correct
38 Correct 26 ms 12268 KB Output is correct
39 Correct 2172 ms 28888 KB Output is correct
40 Correct 2159 ms 28756 KB Output is correct
41 Correct 2143 ms 28944 KB Output is correct
42 Correct 2025 ms 29152 KB Output is correct
43 Correct 2060 ms 29280 KB Output is correct
44 Correct 2114 ms 29312 KB Output is correct
45 Correct 3594 ms 34964 KB Output is correct
46 Correct 3327 ms 39696 KB Output is correct
47 Correct 553 ms 29024 KB Output is correct
48 Correct 404 ms 30452 KB Output is correct
49 Correct 2369 ms 31208 KB Output is correct
50 Correct 720 ms 31456 KB Output is correct
51 Correct 1924 ms 39740 KB Output is correct