Submission #468819

# Submission time Handle Problem Language Result Execution time Memory
468819 2021-08-29T18:38:10 Z wdjpng Mousetrap (CEOI17_mousetrap) C++17
25 / 100
842 ms 78576 KB
#include <bits/stdc++.h>
//#define double double
#define int long long
#define rep(i,n) for(int i = 0; i < n; i++)
#define all(a) a.begin(), a.end()

using namespace std;
vector<vector<int>>E;
int t,m;


int dfs(int v, int p)
{
    priority_queue<int>pq;
    int children = 0;
    for(int w : E[v])
    {
        if(w==p||w==t) continue; 
        children++;
        pq.push(-dfs(w,v));
        if(pq.size()>2) pq.pop();
    }

    int sum = min((int)1, children); //block biggest child
    if(children>1)
    {
        sum-=pq.top()-1; // cost of second biggest child + cleaning
        sum+=children-2; // block children 3 .. children
    }
    return sum;
}
vector<int>path;
vector<int>par;
void find_path(int v)
{
    if(v==m)
    {
        while (v!=t)
        {
           path.push_back(v); 
            v = par[v];
        }
        
        return;
    }
    for(int w : E[v])
    {
        if(w==par[v]) continue;
        par[w]=v;
        find_path(w);
    }
}

vector<int>f;

int get(int k)
{
    k++;
    int sum =0;
    for(int i = k; i>0;i-=i&-1) sum+=f[i];
    return sum;
}

void update(int k)
{
    k++;
    for(int i = k; i<f.size(); i+=i&-i) f[i]++;
}

signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    
    int n;
    cin>>n>>t>>m;
    t--;
    m--;
    if(t==m)
    {
        cout<<0<<"\n";
        exit(0);
    }
    E.resize(n);
    par.assign(n, -1);

    rep(i, n-1)
    {
        int a,b;
        cin>>a>>b;
        a--;b--;
        E[a].push_back(b);
        E[b].push_back(a);
    }
    find_path(t);
    //path from mouse to trap;
    vector<int>suff(path.size());
    for(int i = path.size()-1; i>=0; i--)
    {
        suff[i] = E[path[i]].size()-1;
        if(i<path.size()-1) suff[i]+=suff[i+1]-1;
    }
    
    vector<pair<int, int>>costs;
    rep(i, path.size())
    {
        int v = path[i];
        for(int w : E[v])
        {
            if((i>0&&w==path[i-1]||(i+1<path.size()&&w==path[i+1]))||w==t) continue;
            int c = dfs(w, v) + suff[i]; // suff because all except that edge need to be blocked and that edge needs to be cleaned
            costs.push_back({c, i});
        }
    }

    sort(all(costs));
    reverse(all(costs));
    
    int maxx = 0;
    f.assign(n+1, 0);
    vector<int>cs(path.size());
    for(auto cost : costs)
    {
        bool pos = true;
        int sum=0;
        rep(i, path.size())
        {
            sum+=cs[i];
            if(i == cost.second) sum++;
            if(sum>i+1) pos=false;
        }
        if(pos) cs[cost.second]++;
        else {maxx=cost.first; break;}
    }
    cout<<maxx<<"\n";
}

Compilation message

mousetrap.cpp: In function 'void update(long long int)':
mousetrap.cpp:67:21: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   67 |     for(int i = k; i<f.size(); i+=i&-i) f[i]++;
      |                    ~^~~~~~~~~
mousetrap.cpp: In function 'int main()':
mousetrap.cpp:101:13: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  101 |         if(i<path.size()-1) suff[i]+=suff[i+1]-1;
      |            ~^~~~~~~~~~~~~~
mousetrap.cpp:4:35: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
    4 | #define rep(i,n) for(int i = 0; i < n; i++)
......
  105 |     rep(i, path.size())
      |         ~~~~~~~~~~~~~~             
mousetrap.cpp:105:5: note: in expansion of macro 'rep'
  105 |     rep(i, path.size())
      |     ^~~
mousetrap.cpp:110:40: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  110 |             if((i>0&&w==path[i-1]||(i+1<path.size()&&w==path[i+1]))||w==t) continue;
      |                                     ~~~^~~~~~~~~~~~
mousetrap.cpp:110:20: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
  110 |             if((i>0&&w==path[i-1]||(i+1<path.size()&&w==path[i+1]))||w==t) continue;
mousetrap.cpp:4:35: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
    4 | #define rep(i,n) for(int i = 0; i < n; i++)
......
  126 |         rep(i, path.size())
      |             ~~~~~~~~~~~~~~         
mousetrap.cpp:126:9: note: in expansion of macro 'rep'
  126 |         rep(i, path.size())
      |         ^~~
# Verdict Execution time Memory Grader output
1 Incorrect 0 ms 204 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 384 ms 78576 KB Output is correct
2 Correct 347 ms 70736 KB Output is correct
3 Correct 842 ms 76768 KB Output is correct
4 Correct 377 ms 38520 KB Output is correct
5 Correct 825 ms 76968 KB Output is correct
6 Correct 825 ms 76844 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 0 ms 204 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 0 ms 204 KB Output isn't correct
2 Halted 0 ms 0 KB -