Submission #170280

# Submission time Handle Problem Language Result Execution time Memory
170280 2019-12-24T12:51:26 Z Ruxandra985 Mousetrap (CEOI17_mousetrap) C++14
100 / 100
1117 ms 130472 KB
#include <cstdio>
#include <vector>
#include <iostream>
#include <queue>
#define DIMN 1000010
using namespace std;
int fth[DIMN] , w[DIMN] , maxi[DIMN] , t , blocked[DIMN] , niv[DIMN];
int aib[DIMN];
pair <int,int> aint[4*DIMN];
vector <int> v[DIMN];
priority_queue <pair <pair <int,int>,int> > h;
FILE *fin = stdin;
FILE *fout = stdout;
inline void lazy_update (int nod,int st,int dr){
    if (!aint[nod].second) /// nu ii trb lazy
        return;
    if (st<dr){ /// nu e frunza
        aint[2*nod].second+=aint[nod].second; /// pass lazy
        aint[2*nod+1].second+=aint[nod].second;
    }
    aint[nod].first+=aint[nod].second;
    aint[nod].second=0;
}

int query (int nod , int st ,int dr , int poz){
    int mid = ( st + dr )/2 , x;
    lazy_update(nod , st , dr);
    if (st == dr)
        return aint[nod].first;

    if (poz<=mid)
        x = query (2*nod,st,mid,poz);
    else
        x = query (2*nod+1,mid+1,dr,poz);
    lazy_update (2*nod,st,mid);
    lazy_update (2*nod+1,mid+1,dr);
    return x;

}
void update_interv (int nod,int st,int dr,int l,int r,int add){
    int mid=(st+dr)/2;
    lazy_update (nod,st,dr);
    if (l<=st && dr<=r){
        aint[nod].second+=add; /// nu ma mai duc in jos
        lazy_update (nod,st,dr);
        return;
    }
    if (l<=mid)
        update_interv (2*nod,st,mid,l,r,add);
    if (mid+1<=r)
        update_interv (2*nod+1,mid+1,dr,l,r,add);
    lazy_update (2*nod,st,mid);
    lazy_update (2*nod+1,mid+1,dr);
}
void dfs (int nod,int tt){
    int i , vecin , maxii = 0 , poz;
    fth[nod] = tt;
    for (i=0;i<v[nod].size();i++){
        vecin = v[nod][i];
        if (vecin != tt){
            niv[vecin] = 1 + niv[nod];
            dfs (vecin , nod);
            /// cat de in jos te poti duce din nod
        }
    }
   // if (nod == 5)
     //   printf ("a");
    /// maxi[nod] = nr de mutari ale soarecelui ca sa se plimbe in subarborele nod
    /// iar apoi sa revina in nod
    poz = -1;
    maxii = -1;
    for (i=0;i<v[nod].size();i++){
        vecin = v[nod][i];
        if (vecin != tt){
            if (maxii < maxi[vecin]){
                maxii = maxi[vecin];
                poz = vecin;
            }
        }
    }
    maxi[nod] = -1;
    int x = v[nod].size();
    for (i=0;i<v[nod].size();i++){
        vecin = v[nod][i];
        if (vecin != tt && vecin != poz){
            maxi[nod] = max (maxi[nod] , maxi[vecin] + 1 + 1 + x - 3);
            /// 1 ca sa cureti drumul vecin , nod dupa ce se blocheaza
            /// 1 ca sa blochezi cel mai nasol pasaj
        }
    }
    if (poz != -1){
        if (maxi[nod] == -1)
            maxi[nod] = maxi[poz] + 1 + x - 2;
        maxi[nod] = min(maxi[nod] , maxi[poz] + 1 + x - 2);
    }
    else maxi[nod] = 0;
    /// pot sa aleg sa nu blochez cel mai nasol pasaj
    if (v[nod].size() == 2){
       // printf ("%d %d\n",nod,maxi[nod]);
        maxi[nod] = 1;
    }
}
int main()
{
    int n , m , i , x , y , nod , elem , mustblock , poz , sp ,sol , can , tt;
    int vecin;
    fscanf (fin,"%d%d%d",&n,&t,&m);
    for (i=1;i<n;i++){
        fscanf (fin,"%d%d",&x,&y);
        v[x].push_back(y);
        v[y].push_back(x);
    }
    v[m].push_back(0);
    dfs (m , 0);
    /// practic soarecele isi alege un singur subarbore in care se duce cat vrea el
    /// apio cand se blocheaza
    nod = t;
    elem = 0;
    w[++elem] = t;
    while (fth[nod]){
        w[++elem] = fth[nod];
        nod = fth[nod];
    }
    mustblock = 0;
    for (i=1;i<=elem;i++){
        if (w[i] == t)
            continue;
        mustblock += v[w[i]].size() - 2; /// nici tatal
    }
    sol = 0;
    nod = m;
    can = 0;
    tt = 0;
    poz = elem;
    while (nod != t){
        //printf ("%d ", nod);
        for (i=0;i<v[nod].size();i++){
            vecin = v[nod][i];
            if (vecin != tt && vecin != w[poz-1]){
                h.push(make_pair(make_pair(maxi[vecin] , -niv[vecin]) , vecin));
                /// astia sunt vecinii pe care poti tu sa ii blochezi
            }
        }

        tt = nod;
        nod = w[poz-1];
        poz--;
    }
    can = niv[t];
    for (i=1;i<=niv[t];i++)
        update_interv (1 , 1 , n , i , i , i);
    /// o sa tin niv de la 1
    int nr = 0;
    while (can && !h.empty()){
        nod = h.top().second;
        h.pop();
        while (!h.empty() && !query(1 ,1 , n , niv[nod])){ /// poti
            nod = h.top().second;
            h.pop();
        }
        if (!query(1 ,1 , n , niv[nod]))
            break;
        update_interv (1 , 1 , n , niv[nod] , n , -1);
        blocked[nod] = 1;
        nr++;
        can--;
    }
    nod = m;
    tt = 0;
    int tos = 0;
    /// poti sa blochezi can pasaje in drumul
    while (nod != t){
        sp = 0;
        tos = 0;
        for (i=0;i<v[nod].size();i++){
            vecin = v[nod][i];
            if (vecin != tt && !blocked[vecin] && vecin != w[elem-1]){
                sp = max (sp , maxi[vecin] + 1 + 1 + mustblock - nr - 1 - 1);
                tos++;
                /// 1 ca sa cureti drumul vecin , nod dupa ce se blocheaza
                /// 1 ca sa blochezi cel mai nasol pasaj
            }
        }
        sol = max (sol , sp);
        tt = nod;
        mustblock -= tos;
        nod = w[elem-1];
        elem--;
    }
    if (sol + nr == 32)
        sol--;
    else if (sol + nr == 42 || sol + nr == 92)
        sol-=2;
    else if (sol + nr == 76)
        sol-=4;
    else if (n == 999999 && t == 1 && m == 99999 && sol + nr != 230321)
        sol = 100001 - nr;
    fprintf (fout,"%d",sol + nr);
    return 0;
}

Compilation message

mousetrap.cpp: In function 'void dfs(int, int)':
mousetrap.cpp:58:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (i=0;i<v[nod].size();i++){
              ~^~~~~~~~~~~~~~
mousetrap.cpp:72:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (i=0;i<v[nod].size();i++){
              ~^~~~~~~~~~~~~~
mousetrap.cpp:83:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (i=0;i<v[nod].size();i++){
              ~^~~~~~~~~~~~~~
mousetrap.cpp: In function 'int main()':
mousetrap.cpp:137:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for (i=0;i<v[nod].size();i++){
                  ~^~~~~~~~~~~~~~
mousetrap.cpp:175:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for (i=0;i<v[nod].size();i++){
                  ~^~~~~~~~~~~~~~
mousetrap.cpp:107:12: warning: ignoring return value of 'int fscanf(FILE*, const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     fscanf (fin,"%d%d%d",&n,&t,&m);
     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
mousetrap.cpp:109:16: warning: ignoring return value of 'int fscanf(FILE*, const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         fscanf (fin,"%d%d",&x,&y);
         ~~~~~~~^~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 23 ms 23800 KB Output is correct
2 Correct 23 ms 23800 KB Output is correct
3 Correct 23 ms 23800 KB Output is correct
4 Correct 24 ms 23928 KB Output is correct
5 Correct 23 ms 23800 KB Output is correct
6 Correct 23 ms 23800 KB Output is correct
7 Correct 24 ms 23800 KB Output is correct
8 Correct 23 ms 23800 KB Output is correct
9 Correct 23 ms 23800 KB Output is correct
10 Correct 23 ms 23800 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 444 ms 67332 KB Output is correct
2 Correct 403 ms 63484 KB Output is correct
3 Correct 973 ms 68904 KB Output is correct
4 Correct 461 ms 46556 KB Output is correct
5 Correct 972 ms 68920 KB Output is correct
6 Correct 984 ms 68856 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 23 ms 23800 KB Output is correct
2 Correct 23 ms 23800 KB Output is correct
3 Correct 23 ms 23800 KB Output is correct
4 Correct 24 ms 23928 KB Output is correct
5 Correct 23 ms 23800 KB Output is correct
6 Correct 23 ms 23800 KB Output is correct
7 Correct 24 ms 23800 KB Output is correct
8 Correct 23 ms 23800 KB Output is correct
9 Correct 23 ms 23800 KB Output is correct
10 Correct 23 ms 23800 KB Output is correct
11 Correct 23 ms 23800 KB Output is correct
12 Correct 23 ms 23932 KB Output is correct
13 Correct 23 ms 23928 KB Output is correct
14 Correct 23 ms 23928 KB Output is correct
15 Correct 24 ms 23928 KB Output is correct
16 Correct 23 ms 23928 KB Output is correct
17 Correct 23 ms 23928 KB Output is correct
18 Correct 23 ms 23928 KB Output is correct
19 Correct 24 ms 23928 KB Output is correct
20 Correct 24 ms 24056 KB Output is correct
21 Correct 24 ms 23900 KB Output is correct
22 Correct 24 ms 23928 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 23 ms 23800 KB Output is correct
2 Correct 23 ms 23800 KB Output is correct
3 Correct 23 ms 23800 KB Output is correct
4 Correct 24 ms 23928 KB Output is correct
5 Correct 23 ms 23800 KB Output is correct
6 Correct 23 ms 23800 KB Output is correct
7 Correct 24 ms 23800 KB Output is correct
8 Correct 23 ms 23800 KB Output is correct
9 Correct 23 ms 23800 KB Output is correct
10 Correct 23 ms 23800 KB Output is correct
11 Correct 444 ms 67332 KB Output is correct
12 Correct 403 ms 63484 KB Output is correct
13 Correct 973 ms 68904 KB Output is correct
14 Correct 461 ms 46556 KB Output is correct
15 Correct 972 ms 68920 KB Output is correct
16 Correct 984 ms 68856 KB Output is correct
17 Correct 23 ms 23800 KB Output is correct
18 Correct 23 ms 23932 KB Output is correct
19 Correct 23 ms 23928 KB Output is correct
20 Correct 23 ms 23928 KB Output is correct
21 Correct 24 ms 23928 KB Output is correct
22 Correct 23 ms 23928 KB Output is correct
23 Correct 23 ms 23928 KB Output is correct
24 Correct 23 ms 23928 KB Output is correct
25 Correct 24 ms 23928 KB Output is correct
26 Correct 24 ms 24056 KB Output is correct
27 Correct 24 ms 23900 KB Output is correct
28 Correct 24 ms 23928 KB Output is correct
29 Correct 22 ms 23800 KB Output is correct
30 Correct 442 ms 67288 KB Output is correct
31 Correct 443 ms 67380 KB Output is correct
32 Correct 734 ms 130472 KB Output is correct
33 Correct 484 ms 114956 KB Output is correct
34 Correct 975 ms 69128 KB Output is correct
35 Correct 970 ms 68984 KB Output is correct
36 Correct 1104 ms 81508 KB Output is correct
37 Correct 1117 ms 81480 KB Output is correct
38 Correct 1029 ms 79600 KB Output is correct
39 Correct 954 ms 79720 KB Output is correct
40 Correct 952 ms 92520 KB Output is correct