Submission #475708

# Submission time Handle Problem Language Result Execution time Memory
475708 2021-09-23T17:42:01 Z blue Meetings 2 (JOI21_meetings2) C++17
100 / 100
673 ms 53256 KB
#include <iostream>
#include <vector>
#include <queue>
#include <set>
using namespace std;
 
const int maxN = 200'000;
const int logN = 19;
 
int N;
vector<int> edge[1+maxN];
 
int anc[1+maxN][1+logN];
vector<int> depth(1+maxN);
 
void dfs(int u)
{
    for(int v: edge[u])
    {
        if(anc[u][0] == v) continue;
        anc[v][0] = u;
        depth[v] = depth[u] + 1;
        dfs(v);
    }
}
 
int lca(int u, int v)
{
    if(depth[u] > depth[v]) swap(u, v);
    for(int e = logN; e >= 0; e--)
    {
        if(depth[ anc[v][e] ] >= depth[u])
            v = anc[v][e];
    }
    if(u == v) return u;
 
    for(int e = logN; e >= 0; e--)
    {
        if(anc[u][e] == anc[v][e]) continue;
        u = anc[u][e];
        v = anc[v][e];
    }
 
    u = anc[u][0];
    return u;
}
 
int dist(int u, int v)
{
    return depth[u] + depth[v] - 2*depth[lca(u, v)];
}
 
 
 
 
 
 
int main()
{
    cin >> N;
    for(int e = 1; e <= N-1; e++)
    {
        int A, B;
        cin >> A >> B;
        edge[A].push_back(B);
        edge[B].push_back(A);
    }
 
    anc[1][0] = anc[0][0] = 0;
    depth[1] = 1;
    dfs(1);
 
 
 
 
    for(int e = 1; e <= logN; e++)
    {
        for(int u = 0; u <= N; u++)
        {
            anc[u][e] = anc[ anc[u][e-1] ][e-1];
        }
    }
 
    vector<int> added(N+1, 0); //add time
    int add_id = 0;
 
    vector<int> counter(N+1, 0);
    vector<int> leaf_size(1+N, 1);
    set<int> tbv[1+N];
    int visit_count = 0;
 
    for(int u = 1; u <= N; u++)
    {
        if(edge[u].size() == 1)
        {
            tbv[1].insert(u);
            added[u] = 1;
        }
    }
 
    vector<int> suspicious;
 
    for(int s = 1; s <= N; s++)
    {
        for(int u: tbv[s])
        {
            bool flag = 0;
            for(int v: edge[u])
            {
                if(added[v]) continue;
                flag = 1;
 
                leaf_size[v] += leaf_size[u];
                counter[v]++;
 
                if(counter[v] == edge[v].size() - 1)
                {
                    // cerr << "adding " << v << " from " << u << '\n';
                    add_id++;
                    added[v] = add_id;
                    tbv[leaf_size[v]].insert(v);
                }
            }
            if(flag == 0)
                suspicious.push_back(u);
        }
    }
 
    // for(int s:suspicious) cerr << s << ' ' << leaf_size[s] << '\n';
    // cerr << '\n';
 
    // cerr << suspicious.size() << '\n';
 
    // for(int s = 1; s <= N; s++)
    // {
    //     cerr << "s = " << s << ": ";
    //     for(int u: tbv[s]) cerr << u << ' ';
    //     cerr << '\n';
    // }
 
    vector<int> res(N+1, 1);
 
    int curr1 = -1, curr2 = -1;
    int J = N/2;
 
    for(int s = N; s >= 1; s--)
    {
        for(int u: tbv[s])
        {
            // cerr << "adding " << u << '\n';
            if(curr1 == -1)
                curr1 = u;
            else if(curr2 == -1)
                curr2 = u;
            else
            {
                // cerr << dist(u, curr1) << ' ' << dist(u, curr2) << ' ' << dist(curr1, curr2) << '\n';
                if(dist(u, curr1) > dist(curr1, curr2))
                {
                    // cerr << "entered 1\n";
                    curr2 = u;
                }
                else if(dist(u, curr2) > dist(curr1, curr2))
                {
                    // cerr << "entered 2\n";
                    curr1 = u;
                }
            }
 
            if(curr1 == -1 || curr2 == -1) continue;
            // cerr << "diameter = " << curr1 << ' ' << curr2 << '\n';
            // cerr << "min leaf size = " << min(leaf_size[curr1], leaf_size[curr2]) << ' ' << dist(curr1, curr2) + 1 << '\n';
            //
            // cerr << "J = " << J << '\n';
 
            while(J > min(leaf_size[curr1], leaf_size[curr2]))
            {
                J--;
                // cerr << "j--\n";
            }
 
            if(J == min(leaf_size[curr1], leaf_size[curr2]))
            {
                res[2*J] = dist(curr1, curr2) + 1;
            }
        }
    }
 
    for(int q = 2*(N/2) - 2; q >= 2; q -= 2)
        res[q] = max(res[q], res[q+2]);
 
    for(int i = 1; i <= N; i++) cout << res[i] << ' ';
    cout << '\n';
}

Compilation message

meetings2.cpp: In function 'int main()':
meetings2.cpp:116:31: warning: comparison of integer expressions of different signedness: '__gnu_cxx::__alloc_traits<std::allocator<int>, int>::value_type' {aka 'int'} and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  116 |                 if(counter[v] == edge[v].size() - 1)
meetings2.cpp:90:9: warning: unused variable 'visit_count' [-Wunused-variable]
   90 |     int visit_count = 0;
      |         ^~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5708 KB Output is correct
2 Correct 3 ms 5708 KB Output is correct
3 Correct 3 ms 5708 KB Output is correct
4 Correct 3 ms 5796 KB Output is correct
5 Correct 3 ms 5708 KB Output is correct
6 Correct 3 ms 5708 KB Output is correct
7 Correct 3 ms 5708 KB Output is correct
8 Correct 3 ms 5792 KB Output is correct
9 Correct 3 ms 5708 KB Output is correct
10 Correct 3 ms 5708 KB Output is correct
11 Correct 3 ms 5708 KB Output is correct
12 Correct 3 ms 5708 KB Output is correct
13 Correct 4 ms 5792 KB Output is correct
14 Correct 4 ms 5836 KB Output is correct
15 Correct 3 ms 5788 KB Output is correct
16 Correct 4 ms 5792 KB Output is correct
17 Correct 3 ms 5708 KB Output is correct
18 Correct 3 ms 5796 KB Output is correct
19 Correct 3 ms 5792 KB Output is correct
20 Correct 3 ms 5708 KB Output is correct
21 Correct 3 ms 5708 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5708 KB Output is correct
2 Correct 3 ms 5708 KB Output is correct
3 Correct 3 ms 5708 KB Output is correct
4 Correct 3 ms 5796 KB Output is correct
5 Correct 3 ms 5708 KB Output is correct
6 Correct 3 ms 5708 KB Output is correct
7 Correct 3 ms 5708 KB Output is correct
8 Correct 3 ms 5792 KB Output is correct
9 Correct 3 ms 5708 KB Output is correct
10 Correct 3 ms 5708 KB Output is correct
11 Correct 3 ms 5708 KB Output is correct
12 Correct 3 ms 5708 KB Output is correct
13 Correct 4 ms 5792 KB Output is correct
14 Correct 4 ms 5836 KB Output is correct
15 Correct 3 ms 5788 KB Output is correct
16 Correct 4 ms 5792 KB Output is correct
17 Correct 3 ms 5708 KB Output is correct
18 Correct 3 ms 5796 KB Output is correct
19 Correct 3 ms 5792 KB Output is correct
20 Correct 3 ms 5708 KB Output is correct
21 Correct 3 ms 5708 KB Output is correct
22 Correct 10 ms 6708 KB Output is correct
23 Correct 11 ms 6708 KB Output is correct
24 Correct 10 ms 6604 KB Output is correct
25 Correct 10 ms 6604 KB Output is correct
26 Correct 10 ms 6684 KB Output is correct
27 Correct 11 ms 6604 KB Output is correct
28 Correct 10 ms 6712 KB Output is correct
29 Correct 11 ms 6604 KB Output is correct
30 Correct 10 ms 6700 KB Output is correct
31 Correct 12 ms 6708 KB Output is correct
32 Correct 12 ms 6708 KB Output is correct
33 Correct 10 ms 6700 KB Output is correct
34 Correct 10 ms 6604 KB Output is correct
35 Correct 9 ms 6604 KB Output is correct
36 Correct 10 ms 6704 KB Output is correct
37 Correct 11 ms 6732 KB Output is correct
38 Correct 10 ms 6704 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5708 KB Output is correct
2 Correct 3 ms 5708 KB Output is correct
3 Correct 3 ms 5708 KB Output is correct
4 Correct 3 ms 5796 KB Output is correct
5 Correct 3 ms 5708 KB Output is correct
6 Correct 3 ms 5708 KB Output is correct
7 Correct 3 ms 5708 KB Output is correct
8 Correct 3 ms 5792 KB Output is correct
9 Correct 3 ms 5708 KB Output is correct
10 Correct 3 ms 5708 KB Output is correct
11 Correct 3 ms 5708 KB Output is correct
12 Correct 3 ms 5708 KB Output is correct
13 Correct 4 ms 5792 KB Output is correct
14 Correct 4 ms 5836 KB Output is correct
15 Correct 3 ms 5788 KB Output is correct
16 Correct 4 ms 5792 KB Output is correct
17 Correct 3 ms 5708 KB Output is correct
18 Correct 3 ms 5796 KB Output is correct
19 Correct 3 ms 5792 KB Output is correct
20 Correct 3 ms 5708 KB Output is correct
21 Correct 3 ms 5708 KB Output is correct
22 Correct 10 ms 6708 KB Output is correct
23 Correct 11 ms 6708 KB Output is correct
24 Correct 10 ms 6604 KB Output is correct
25 Correct 10 ms 6604 KB Output is correct
26 Correct 10 ms 6684 KB Output is correct
27 Correct 11 ms 6604 KB Output is correct
28 Correct 10 ms 6712 KB Output is correct
29 Correct 11 ms 6604 KB Output is correct
30 Correct 10 ms 6700 KB Output is correct
31 Correct 12 ms 6708 KB Output is correct
32 Correct 12 ms 6708 KB Output is correct
33 Correct 10 ms 6700 KB Output is correct
34 Correct 10 ms 6604 KB Output is correct
35 Correct 9 ms 6604 KB Output is correct
36 Correct 10 ms 6704 KB Output is correct
37 Correct 11 ms 6732 KB Output is correct
38 Correct 10 ms 6704 KB Output is correct
39 Correct 550 ms 51828 KB Output is correct
40 Correct 526 ms 50884 KB Output is correct
41 Correct 553 ms 51936 KB Output is correct
42 Correct 575 ms 52732 KB Output is correct
43 Correct 547 ms 52864 KB Output is correct
44 Correct 550 ms 52648 KB Output is correct
45 Correct 673 ms 52992 KB Output is correct
46 Correct 543 ms 53028 KB Output is correct
47 Correct 503 ms 53116 KB Output is correct
48 Correct 419 ms 53216 KB Output is correct
49 Correct 541 ms 53256 KB Output is correct
50 Correct 484 ms 53252 KB Output is correct
51 Correct 464 ms 53028 KB Output is correct