답안 #100928

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
100928 2019-03-15T09:27:55 Z Alexa2001 Unique Cities (JOI19_ho_t5) C++17
100 / 100
1197 ms 62584 KB
#include <bits/stdc++.h>

using namespace std;


const int Nmax = 2e5 + 5;

int down[Nmax], ans[Nmax], level[Nmax], st[Nmax], max_ex[Nmax];
bool ap[Nmax];
vector<int> v[Nmax], collect_ans[Nmax], edge[Nmax];
int n, M, nr, Distincte;
int tip[Nmax], S[Nmax], tip_now[Nmax];


void read()
{
    int i, x, y;
    cin >> n >> M;
    for(i=1; i<n; ++i)
    {
        cin >> x >> y;
        v[x].push_back(y);
        v[y].push_back(x);
    }

    for(i=1; i<=n; ++i) cin >> tip[i];
}

void print_sol()
{
    int i;
    for(i=1; i<=n; ++i) cout << ans[i] << '\n';
}

void max_to(int &x, int y) { if(x < y) x = y; }


void dfs0(int node, int dad = 0)
{
    level[node] = level[dad] + 1;
    for(auto it : v[node])
        if(it != dad)
            dfs0(it, node);
}

pair<int,int> diametru()
{
    int i, A = 1, B = 1;

    dfs0(1);
    for(i=1; i<=n; ++i)
        if(level[i] > level[A]) A = i;

    dfs0(A);
    for(i=1; i<=n; ++i)
        if(level[i] > level[B]) B = i;

    return {A, B};
}

void dfs(int node, int dad = 0)
{
    down[node] = 1;
    level[node] = level[dad] + 1;

    for(auto it : v[node])
        if(it != dad)
        {
            dfs(it, node);
            down[node] = max(down[node], down[it] + 1);
        }

    int i, mx = 0;
    for(i=0; i<v[node].size(); ++i)
        if(v[node][i] != dad)
        {
            max_ex[v[node][i]] = mx;
            max_to(mx, down[v[node][i]]);
        }

    mx = 0;
    for(i=v[node].size()-1; i>=0; --i)
        if(v[node][i] != dad)
        {
            max_to(max_ex[v[node][i]], mx);
            max_to(mx, down[v[node][i]]);
        }
}

int bs(int val)
{
    int st = 1, dr = nr, mid;

    while(st <= dr)
    {
        mid = (st + dr) / 2;
        if(level[S[mid]] <= val) st = mid + 1;
            else dr = mid - 1;
    }
    return dr;
}

void solve(int node, int dad = 0)
{
    collect_ans[S[bs(level[node] - down[node] + 1)]].push_back(node);
    tip_now[node] = tip[dad];

    for(auto it : v[node])
        if(it != dad)
        {
            int pos, val, lnr;
            pos = bs(level[node] - max_ex[it]);
            lnr = nr;

            edge[S[pos]].push_back(it);

            val = S[pos+1];
            S[pos+1] = it;
            nr = pos+1;

            solve(it, node);

            S[pos+1] = val;
            nr = lnr;
        }
}

void extract_ans(int node)
{
    bool ok = 0;

    if(!ap[tip_now[node]])
    {
        ok = 1;
        ap[tip_now[node]] = 1;
        ++Distincte;
    }

    for(auto it : collect_ans[node])
        max_to(ans[it], Distincte);

    for(auto it : edge[node])
        extract_ans(it);

    if(ok)
    {
        ap[tip_now[node]] = 0;
        --Distincte;
    }
}

void Solve(int node)
{
    dfs(node);
    int i;
    for(i=0; i<=nr; ++i) S[i] = 0;
    nr = 0;
    for(i=0; i<=n; ++i) edge[i].clear(), collect_ans[i].clear(), ap[i] = 0;
    Distincte = 0;

    solve(node);
    for(auto it : edge[0]) extract_ans(it);
}

int main()
{
//    freopen("input", "r", stdin);
    cin.sync_with_stdio(false);

    read();
    auto P = diametru();

    Solve(P.first);
    Solve(P.second);

    print_sol();

    return 0;
}

Compilation message

joi2019_ho_t5.cpp: In function 'void dfs(int, int)':
joi2019_ho_t5.cpp:74:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(i=0; i<v[node].size(); ++i)
              ~^~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 14464 KB Output is correct
2 Correct 17 ms 14592 KB Output is correct
3 Correct 16 ms 14720 KB Output is correct
4 Correct 16 ms 14720 KB Output is correct
5 Correct 20 ms 14592 KB Output is correct
6 Correct 22 ms 14848 KB Output is correct
7 Correct 17 ms 14848 KB Output is correct
8 Correct 21 ms 14720 KB Output is correct
9 Correct 18 ms 14720 KB Output is correct
10 Correct 16 ms 14720 KB Output is correct
11 Correct 17 ms 14712 KB Output is correct
12 Correct 9 ms 14592 KB Output is correct
13 Correct 20 ms 14848 KB Output is correct
14 Correct 18 ms 14720 KB Output is correct
15 Correct 18 ms 14720 KB Output is correct
16 Correct 23 ms 14704 KB Output is correct
17 Correct 18 ms 14848 KB Output is correct
18 Correct 17 ms 14720 KB Output is correct
19 Correct 18 ms 14712 KB Output is correct
20 Correct 22 ms 14848 KB Output is correct
21 Correct 17 ms 14720 KB Output is correct
22 Correct 17 ms 14720 KB Output is correct
23 Correct 17 ms 14720 KB Output is correct
24 Correct 18 ms 14720 KB Output is correct
25 Correct 21 ms 14720 KB Output is correct
26 Correct 17 ms 14592 KB Output is correct
27 Correct 18 ms 14848 KB Output is correct
28 Correct 19 ms 14848 KB Output is correct
29 Correct 21 ms 14720 KB Output is correct
30 Correct 22 ms 14720 KB Output is correct
31 Correct 18 ms 14848 KB Output is correct
32 Correct 18 ms 14848 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 348 ms 25316 KB Output is correct
2 Correct 540 ms 39884 KB Output is correct
3 Correct 66 ms 18808 KB Output is correct
4 Correct 636 ms 33764 KB Output is correct
5 Correct 1065 ms 58448 KB Output is correct
6 Correct 973 ms 44656 KB Output is correct
7 Correct 596 ms 34156 KB Output is correct
8 Correct 753 ms 35064 KB Output is correct
9 Correct 670 ms 34284 KB Output is correct
10 Correct 656 ms 34668 KB Output is correct
11 Correct 438 ms 33900 KB Output is correct
12 Correct 895 ms 46040 KB Output is correct
13 Correct 841 ms 44356 KB Output is correct
14 Correct 841 ms 43956 KB Output is correct
15 Correct 435 ms 34000 KB Output is correct
16 Correct 954 ms 48712 KB Output is correct
17 Correct 873 ms 45168 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 483 ms 29824 KB Output is correct
2 Correct 1076 ms 54512 KB Output is correct
3 Correct 94 ms 19292 KB Output is correct
4 Correct 653 ms 33072 KB Output is correct
5 Correct 1052 ms 59408 KB Output is correct
6 Correct 946 ms 44912 KB Output is correct
7 Correct 716 ms 32820 KB Output is correct
8 Correct 790 ms 37232 KB Output is correct
9 Correct 818 ms 36144 KB Output is correct
10 Correct 723 ms 34576 KB Output is correct
11 Correct 631 ms 33388 KB Output is correct
12 Correct 1194 ms 50940 KB Output is correct
13 Correct 868 ms 42096 KB Output is correct
14 Correct 1030 ms 43700 KB Output is correct
15 Correct 431 ms 33812 KB Output is correct
16 Correct 933 ms 49132 KB Output is correct
17 Correct 918 ms 45292 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 14464 KB Output is correct
2 Correct 17 ms 14592 KB Output is correct
3 Correct 16 ms 14720 KB Output is correct
4 Correct 16 ms 14720 KB Output is correct
5 Correct 20 ms 14592 KB Output is correct
6 Correct 22 ms 14848 KB Output is correct
7 Correct 17 ms 14848 KB Output is correct
8 Correct 21 ms 14720 KB Output is correct
9 Correct 18 ms 14720 KB Output is correct
10 Correct 16 ms 14720 KB Output is correct
11 Correct 17 ms 14712 KB Output is correct
12 Correct 9 ms 14592 KB Output is correct
13 Correct 20 ms 14848 KB Output is correct
14 Correct 18 ms 14720 KB Output is correct
15 Correct 18 ms 14720 KB Output is correct
16 Correct 23 ms 14704 KB Output is correct
17 Correct 18 ms 14848 KB Output is correct
18 Correct 17 ms 14720 KB Output is correct
19 Correct 18 ms 14712 KB Output is correct
20 Correct 22 ms 14848 KB Output is correct
21 Correct 17 ms 14720 KB Output is correct
22 Correct 17 ms 14720 KB Output is correct
23 Correct 17 ms 14720 KB Output is correct
24 Correct 18 ms 14720 KB Output is correct
25 Correct 21 ms 14720 KB Output is correct
26 Correct 17 ms 14592 KB Output is correct
27 Correct 18 ms 14848 KB Output is correct
28 Correct 19 ms 14848 KB Output is correct
29 Correct 21 ms 14720 KB Output is correct
30 Correct 22 ms 14720 KB Output is correct
31 Correct 18 ms 14848 KB Output is correct
32 Correct 18 ms 14848 KB Output is correct
33 Correct 348 ms 25316 KB Output is correct
34 Correct 540 ms 39884 KB Output is correct
35 Correct 66 ms 18808 KB Output is correct
36 Correct 636 ms 33764 KB Output is correct
37 Correct 1065 ms 58448 KB Output is correct
38 Correct 973 ms 44656 KB Output is correct
39 Correct 596 ms 34156 KB Output is correct
40 Correct 753 ms 35064 KB Output is correct
41 Correct 670 ms 34284 KB Output is correct
42 Correct 656 ms 34668 KB Output is correct
43 Correct 438 ms 33900 KB Output is correct
44 Correct 895 ms 46040 KB Output is correct
45 Correct 841 ms 44356 KB Output is correct
46 Correct 841 ms 43956 KB Output is correct
47 Correct 435 ms 34000 KB Output is correct
48 Correct 954 ms 48712 KB Output is correct
49 Correct 873 ms 45168 KB Output is correct
50 Correct 483 ms 29824 KB Output is correct
51 Correct 1076 ms 54512 KB Output is correct
52 Correct 94 ms 19292 KB Output is correct
53 Correct 653 ms 33072 KB Output is correct
54 Correct 1052 ms 59408 KB Output is correct
55 Correct 946 ms 44912 KB Output is correct
56 Correct 716 ms 32820 KB Output is correct
57 Correct 790 ms 37232 KB Output is correct
58 Correct 818 ms 36144 KB Output is correct
59 Correct 723 ms 34576 KB Output is correct
60 Correct 631 ms 33388 KB Output is correct
61 Correct 1194 ms 50940 KB Output is correct
62 Correct 868 ms 42096 KB Output is correct
63 Correct 1030 ms 43700 KB Output is correct
64 Correct 431 ms 33812 KB Output is correct
65 Correct 933 ms 49132 KB Output is correct
66 Correct 918 ms 45292 KB Output is correct
67 Correct 54 ms 17400 KB Output is correct
68 Correct 402 ms 35456 KB Output is correct
69 Correct 539 ms 35600 KB Output is correct
70 Correct 701 ms 36316 KB Output is correct
71 Correct 1104 ms 61680 KB Output is correct
72 Correct 925 ms 47660 KB Output is correct
73 Correct 823 ms 35456 KB Output is correct
74 Correct 770 ms 39528 KB Output is correct
75 Correct 739 ms 37788 KB Output is correct
76 Correct 757 ms 37480 KB Output is correct
77 Correct 615 ms 36528 KB Output is correct
78 Correct 1020 ms 52180 KB Output is correct
79 Correct 895 ms 49332 KB Output is correct
80 Correct 979 ms 44784 KB Output is correct
81 Correct 416 ms 36668 KB Output is correct
82 Correct 918 ms 51292 KB Output is correct
83 Correct 901 ms 47980 KB Output is correct
84 Correct 688 ms 36408 KB Output is correct
85 Correct 1197 ms 62156 KB Output is correct
86 Correct 1033 ms 48112 KB Output is correct
87 Correct 761 ms 36932 KB Output is correct
88 Correct 868 ms 40332 KB Output is correct
89 Correct 726 ms 38504 KB Output is correct
90 Correct 732 ms 37516 KB Output is correct
91 Correct 625 ms 38508 KB Output is correct
92 Correct 1122 ms 58164 KB Output is correct
93 Correct 923 ms 43904 KB Output is correct
94 Correct 792 ms 41464 KB Output is correct
95 Correct 497 ms 36972 KB Output is correct
96 Correct 1020 ms 52116 KB Output is correct
97 Correct 963 ms 48496 KB Output is correct
98 Correct 752 ms 36200 KB Output is correct
99 Correct 1131 ms 62584 KB Output is correct
100 Correct 985 ms 48632 KB Output is correct
101 Correct 758 ms 36332 KB Output is correct
102 Correct 874 ms 40040 KB Output is correct
103 Correct 784 ms 38636 KB Output is correct
104 Correct 737 ms 37892 KB Output is correct
105 Correct 623 ms 37260 KB Output is correct
106 Correct 989 ms 48500 KB Output is correct
107 Correct 932 ms 50004 KB Output is correct
108 Correct 911 ms 42752 KB Output is correct
109 Correct 426 ms 37736 KB Output is correct
110 Correct 920 ms 52592 KB Output is correct
111 Correct 1010 ms 49056 KB Output is correct