Submission #304708

# Submission time Handle Problem Language Result Execution time Memory
304708 2020-09-21T17:51:58 Z JovanK26 Mousetrap (CEOI17_mousetrap) C++14
100 / 100
1045 ms 158584 KB
#include <bits/stdc++.h>

using namespace std;
vector<int> tree[1000001];
int prt[1000001];
int f[1000001];
vector< pair<int,int> > subtrees;
int ispath[1000001];
int n,m,t;
int dfs1(int cur,int prev)
{
    int par=-1;
    for(int i=0;i<tree[cur].size();i++)
    {
        if(tree[cur][i]==prev)par=i;
    }
    if(par!=-1)
    {
        tree[cur].erase(tree[cur].begin()+par);
    }
    prt[cur]=prev;
    int rez=-1;
    if(cur==m)rez=0;
    for(int i=0;i<tree[cur].size();i++)
    {
        rez=max(rez,dfs1(tree[cur][i],cur));
    }
    ispath[cur]=rez;
    if(rez==-1)return rez;
    return rez+1;
}
void dfs2(int cur,int w)
{
    if(tree[cur].size()==0)
    {
        f[cur]=w;
        return;
    }
    if(ispath[cur]==-1)
    {
        int neww=w+tree[cur].size();
        for(int i=0;i<tree[cur].size();i++)
        {
            dfs2(tree[cur][i],neww);
        }
        if(tree[cur].size()==1)
        {
            f[cur]=w+1;
            return;
        }
        int best=-1;
        int sbest=-1;
        for(int i=0;i<tree[cur].size();i++)
        {
            if(f[tree[cur][i]]>best)
            {
                sbest=best;
                best=f[tree[cur][i]];
                continue;
            }
            if(f[tree[cur][i]]>sbest)
            {
                sbest=f[tree[cur][i]];
            }
        }
        f[cur]=sbest;
        return;
    }
    int neww=w+tree[cur].size()-1;
    if(cur==t)neww=0;
    if(cur==m)neww++;
    for(int i=0;i<tree[cur].size();i++)
    {
        dfs2(tree[cur][i],neww);
    }
    if(cur==t)return;
    for(int i=0;i<tree[cur].size();i++)
    {
        if(ispath[tree[cur][i]]==-1)
        {
            subtrees.push_back(make_pair(ispath[cur],f[tree[cur][i]]));
        }
    }
}
int main()
{

    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin >> n >> t >> m;
    m--;
    t--;
    int a,b;
    for(int i=0;i<n-1;i++)
    {
        cin >> a >> b;
        a--;
        b--;
        tree[a].push_back(b);
        tree[b].push_back(a);
    }
    int pathl=dfs1(t,-1);
    dfs2(t,0);
    sort(subtrees.begin(),subtrees.end());
    int l=0;
    int r=n;
    int med;
    while(l<r)
    {
        med=(l+r)/2;
        int x=0;
        int y=0;
        int pt=0;
        for(int i=0;i<pathl;i++)
        {
            if(pt>=subtrees.size())
            {
                r=med;
                break;
            }
            x++;
            int ydelta=0;
            while(pt<subtrees.size() && subtrees[pt].first<=i)
            {
               if(subtrees[pt].second+y>med)
               {
                   ydelta++;
                   x--;
               }
               pt++;
            }
            y+=ydelta;
            if(x<0 || y>med)
            {
                l=med+1;
                break;
            }
        }
    }
    cout << l<<'\n';
    return 0;
}

Compilation message

mousetrap.cpp: In function 'int dfs1(int, int)':
mousetrap.cpp:13:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   13 |     for(int i=0;i<tree[cur].size();i++)
      |                 ~^~~~~~~~~~~~~~~~~
mousetrap.cpp:24:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   24 |     for(int i=0;i<tree[cur].size();i++)
      |                 ~^~~~~~~~~~~~~~~~~
mousetrap.cpp: In function 'void dfs2(int, int)':
mousetrap.cpp:42:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   42 |         for(int i=0;i<tree[cur].size();i++)
      |                     ~^~~~~~~~~~~~~~~~~
mousetrap.cpp:53:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   53 |         for(int i=0;i<tree[cur].size();i++)
      |                     ~^~~~~~~~~~~~~~~~~
mousetrap.cpp:72:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   72 |     for(int i=0;i<tree[cur].size();i++)
      |                 ~^~~~~~~~~~~~~~~~~
mousetrap.cpp:77:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   77 |     for(int i=0;i<tree[cur].size();i++)
      |                 ~^~~~~~~~~~~~~~~~~
mousetrap.cpp: In function 'int main()':
mousetrap.cpp:117:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  117 |             if(pt>=subtrees.size())
      |                ~~^~~~~~~~~~~~~~~~~
mousetrap.cpp:124:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  124 |             while(pt<subtrees.size() && subtrees[pt].first<=i)
      |                   ~~^~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 15 ms 23808 KB Output is correct
2 Correct 15 ms 23808 KB Output is correct
3 Correct 15 ms 23808 KB Output is correct
4 Correct 15 ms 23808 KB Output is correct
5 Correct 15 ms 23808 KB Output is correct
6 Correct 15 ms 23808 KB Output is correct
7 Correct 15 ms 23808 KB Output is correct
8 Correct 15 ms 23808 KB Output is correct
9 Correct 14 ms 23808 KB Output is correct
10 Correct 15 ms 23808 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 357 ms 66808 KB Output is correct
2 Correct 324 ms 71416 KB Output is correct
3 Correct 1006 ms 76920 KB Output is correct
4 Correct 488 ms 52472 KB Output is correct
5 Correct 1045 ms 76832 KB Output is correct
6 Correct 1036 ms 76792 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 15 ms 23808 KB Output is correct
2 Correct 15 ms 23808 KB Output is correct
3 Correct 15 ms 23808 KB Output is correct
4 Correct 15 ms 23808 KB Output is correct
5 Correct 15 ms 23808 KB Output is correct
6 Correct 15 ms 23808 KB Output is correct
7 Correct 15 ms 23808 KB Output is correct
8 Correct 15 ms 23808 KB Output is correct
9 Correct 14 ms 23808 KB Output is correct
10 Correct 15 ms 23808 KB Output is correct
11 Correct 15 ms 23808 KB Output is correct
12 Correct 15 ms 23936 KB Output is correct
13 Correct 16 ms 23936 KB Output is correct
14 Correct 15 ms 23936 KB Output is correct
15 Correct 16 ms 23936 KB Output is correct
16 Correct 15 ms 23936 KB Output is correct
17 Correct 15 ms 23936 KB Output is correct
18 Correct 16 ms 23936 KB Output is correct
19 Correct 15 ms 23928 KB Output is correct
20 Correct 16 ms 23936 KB Output is correct
21 Correct 15 ms 23936 KB Output is correct
22 Correct 15 ms 23936 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 15 ms 23808 KB Output is correct
2 Correct 15 ms 23808 KB Output is correct
3 Correct 15 ms 23808 KB Output is correct
4 Correct 15 ms 23808 KB Output is correct
5 Correct 15 ms 23808 KB Output is correct
6 Correct 15 ms 23808 KB Output is correct
7 Correct 15 ms 23808 KB Output is correct
8 Correct 15 ms 23808 KB Output is correct
9 Correct 14 ms 23808 KB Output is correct
10 Correct 15 ms 23808 KB Output is correct
11 Correct 357 ms 66808 KB Output is correct
12 Correct 324 ms 71416 KB Output is correct
13 Correct 1006 ms 76920 KB Output is correct
14 Correct 488 ms 52472 KB Output is correct
15 Correct 1045 ms 76832 KB Output is correct
16 Correct 1036 ms 76792 KB Output is correct
17 Correct 15 ms 23808 KB Output is correct
18 Correct 15 ms 23936 KB Output is correct
19 Correct 16 ms 23936 KB Output is correct
20 Correct 15 ms 23936 KB Output is correct
21 Correct 16 ms 23936 KB Output is correct
22 Correct 15 ms 23936 KB Output is correct
23 Correct 15 ms 23936 KB Output is correct
24 Correct 16 ms 23936 KB Output is correct
25 Correct 15 ms 23928 KB Output is correct
26 Correct 16 ms 23936 KB Output is correct
27 Correct 15 ms 23936 KB Output is correct
28 Correct 15 ms 23936 KB Output is correct
29 Correct 18 ms 23808 KB Output is correct
30 Correct 370 ms 80228 KB Output is correct
31 Correct 365 ms 80248 KB Output is correct
32 Correct 435 ms 154872 KB Output is correct
33 Correct 431 ms 158584 KB Output is correct
34 Correct 1026 ms 81272 KB Output is correct
35 Correct 1010 ms 81528 KB Output is correct
36 Correct 1028 ms 90860 KB Output is correct
37 Correct 1025 ms 90864 KB Output is correct
38 Correct 780 ms 92780 KB Output is correct
39 Correct 788 ms 92912 KB Output is correct
40 Correct 776 ms 92692 KB Output is correct