답안 #170356

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
170356 2019-12-24T20:52:50 Z Ruxandra985 Mousetrap (CEOI17_mousetrap) C++14
100 / 100
1114 ms 131244 KB
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#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];
pair <int,int> s[DIMN];
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){
        maxi[nod] = 1;
    }
}
int cmp (pair <int,int> a , pair <int,int> b){
    if (a.first != b.first)
        return a.first > b.first;
    return a.second < b.second;

}
int main()
{
    FILE *fin = stdin;
    FILE *fout = stdout;
    int n , m , i , x , y , nod , elem , mustblock , poz , sp ,sol , can , tt , el , p;
    int vecin , until;
    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);
    }
    //printf ("%d %d\n",v[t].size() , v[m].size());
    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++){
        //printf ("%d %d\n",w[i],v[w[i]].size() - 2);
        if (w[i] == t)
            continue;
        mustblock += v[w[i]].size() - 2; /// nici tatal
    }
    sol = 2000000000;
    nod = m;
    can = 0;
    tt = 0;
    poz = elem;
    el = 0;
    nod = m;
    tt = 0;
    int tos = 0;
    /// poti sa blochezi can pasaje in drumul
    can = 0;
    until = 0;
    while (nod != t){
        sp = 0;
        can++;
        tos = 0;
        if (v[nod].size() == 2){ /// efectiv nu ai ce sa faci


        }
        else {
            el = 0;
            for (i=0;i<v[nod].size();i++){
                vecin = v[nod][i];
                if (vecin != tt && vecin != w[elem-1]){
                    s[++el] = make_pair(maxi[vecin] , vecin);

                    tos++;
                    /// 1 ca sa cureti drumul vecin , nod dupa ce se blocheaza
                    /// 1 ca sa blochezi cel mai nasol pasaj
                }
            }
            sort (s + 1 , s + el + 1 , cmp);
            for (i=1;i<=el && can;i++){
                /// pe asta il blochezi la timp
                if (!maxi[s[i].second])
                    break;
                if (i == el)
                    sol = min( sol ,  maxi[s[i].second] + mustblock);
                can--;
            }
            if (!can && i<=el){
                sol = min ( sol ,  maxi[s[i].second] + mustblock);
                fprintf (fout,"%d\n",sol);
                return 0;
            }
        }

        tt = nod;
        nod = w[elem-1];
        elem--;
    }
    sol = min (sol , mustblock);
    fprintf (fout,"%d",mustblock);
    return 0;
}

Compilation message

mousetrap.cpp: In function 'void dfs(int, int)':
mousetrap.cpp:15:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (i=0;i<v[nod].size();i++){
              ~^~~~~~~~~~~~~~
mousetrap.cpp:29:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (i=0;i<v[nod].size();i++){
              ~^~~~~~~~~~~~~~
mousetrap.cpp:40: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:118:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
             for (i=0;i<v[nod].size();i++){
                      ~^~~~~~~~~~~~~~
mousetrap.cpp:69:54: warning: variable 'poz' set but not used [-Wunused-but-set-variable]
     int n , m , i , x , y , nod , elem , mustblock , poz , sp ,sol , can , tt , el , p;
                                                      ^~~
mousetrap.cpp:69:60: warning: variable 'sp' set but not used [-Wunused-but-set-variable]
     int n , m , i , x , y , nod , elem , mustblock , poz , sp ,sol , can , tt , el , p;
                                                            ^~
mousetrap.cpp:69:86: warning: unused variable 'p' [-Wunused-variable]
     int n , m , i , x , y , nod , elem , mustblock , poz , sp ,sol , can , tt , el , p;
                                                                                      ^
mousetrap.cpp:70:17: warning: variable 'until' set but not used [-Wunused-but-set-variable]
     int vecin , until;
                 ^~~~~
mousetrap.cpp:71: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:73: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);
         ~~~~~~~^~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 23 ms 23928 KB Output is correct
2 Correct 23 ms 23928 KB Output is correct
3 Correct 23 ms 23800 KB Output is correct
4 Correct 27 ms 23800 KB Output is correct
5 Correct 22 ms 23800 KB Output is correct
6 Correct 23 ms 23928 KB Output is correct
7 Correct 22 ms 23800 KB Output is correct
8 Correct 27 ms 23800 KB Output is correct
9 Correct 22 ms 23800 KB Output is correct
10 Correct 27 ms 23800 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 438 ms 66920 KB Output is correct
2 Correct 398 ms 62584 KB Output is correct
3 Correct 951 ms 67980 KB Output is correct
4 Correct 474 ms 45940 KB Output is correct
5 Correct 1089 ms 68172 KB Output is correct
6 Correct 966 ms 68164 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 23 ms 23928 KB Output is correct
2 Correct 23 ms 23928 KB Output is correct
3 Correct 23 ms 23800 KB Output is correct
4 Correct 27 ms 23800 KB Output is correct
5 Correct 22 ms 23800 KB Output is correct
6 Correct 23 ms 23928 KB Output is correct
7 Correct 22 ms 23800 KB Output is correct
8 Correct 27 ms 23800 KB Output is correct
9 Correct 22 ms 23800 KB Output is correct
10 Correct 27 ms 23800 KB Output is correct
11 Correct 22 ms 23800 KB Output is correct
12 Correct 23 ms 23928 KB Output is correct
13 Correct 24 ms 23928 KB Output is correct
14 Correct 25 ms 23928 KB Output is correct
15 Correct 24 ms 23928 KB Output is correct
16 Correct 24 ms 23928 KB Output is correct
17 Correct 24 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 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
# 결과 실행 시간 메모리 Grader output
1 Correct 23 ms 23928 KB Output is correct
2 Correct 23 ms 23928 KB Output is correct
3 Correct 23 ms 23800 KB Output is correct
4 Correct 27 ms 23800 KB Output is correct
5 Correct 22 ms 23800 KB Output is correct
6 Correct 23 ms 23928 KB Output is correct
7 Correct 22 ms 23800 KB Output is correct
8 Correct 27 ms 23800 KB Output is correct
9 Correct 22 ms 23800 KB Output is correct
10 Correct 27 ms 23800 KB Output is correct
11 Correct 438 ms 66920 KB Output is correct
12 Correct 398 ms 62584 KB Output is correct
13 Correct 951 ms 67980 KB Output is correct
14 Correct 474 ms 45940 KB Output is correct
15 Correct 1089 ms 68172 KB Output is correct
16 Correct 966 ms 68164 KB Output is correct
17 Correct 22 ms 23800 KB Output is correct
18 Correct 23 ms 23928 KB Output is correct
19 Correct 24 ms 23928 KB Output is correct
20 Correct 25 ms 23928 KB Output is correct
21 Correct 24 ms 23928 KB Output is correct
22 Correct 24 ms 23928 KB Output is correct
23 Correct 24 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 23 ms 23928 KB Output is correct
27 Correct 24 ms 23928 KB Output is correct
28 Correct 23 ms 23928 KB Output is correct
29 Correct 24 ms 23928 KB Output is correct
30 Correct 465 ms 80200 KB Output is correct
31 Correct 439 ms 80376 KB Output is correct
32 Correct 480 ms 131244 KB Output is correct
33 Correct 481 ms 127428 KB Output is correct
34 Correct 990 ms 81156 KB Output is correct
35 Correct 1114 ms 81272 KB Output is correct
36 Correct 1002 ms 85868 KB Output is correct
37 Correct 1028 ms 85800 KB Output is correct
38 Correct 809 ms 89048 KB Output is correct
39 Correct 781 ms 89044 KB Output is correct
40 Correct 810 ms 88816 KB Output is correct