Submission #411921

# Submission time Handle Problem Language Result Execution time Memory
411921 2021-05-26T09:12:01 Z Ruxandra985 Village (BOI20_village) C++14
56 / 100
156 ms 18872 KB
#include <bits/stdc++.h>
#define DIMN 100010
using namespace std;
int n , mini , maxi , fth , centroid;
int which[DIMN] , wm[DIMN] , sub[DIMN] , f[DIMN];
vector <int> v[DIMN] , w[DIMN];

void dfs (int nod , int tt){
    int i , vecin , ok = 1;



    for (i = 0 ; i < v[nod].size() ; i++){
        vecin = v[nod][i];
        if (vecin != tt){
            dfs (vecin , nod);
            sub[nod] += sub[vecin] + 1;

            if (sub[vecin] + 1 > n / 2)
                ok = 0;

        }
    }

    if (n - sub[nod] - 1 > n / 2)
        ok = 0;

    if (ok && !centroid){
        centroid = nod;
        fth = tt;
    }

    maxi = maxi + 2 * min(sub[nod] + 1 , n - 1 - sub[nod]);

    if (which[nod] == nod){ /// nu l ai schimbat inca

        if (tt){
            swap(which[nod] , which[tt]);
            mini += 2;
        }
        else {
            for (i = 0 ; i < v[nod].size() ; i++){
                vecin = v[nod][i];
                if (vecin != tt){

                    swap(which[vecin] , which[nod]);
                    mini += 2;
                    break;

                }
            }
        }

    }

}

void dfs2 (int nod , int poz){
    int i , vecin;

    f[nod] = 1;

    w[poz].push_back(nod);

    for (i = 0 ; i < v[nod].size() ; i++){
        vecin = v[nod][i];
        if (!f[vecin]){

            if (nod == centroid)
                poz++;

            dfs2 (vecin , poz);

        }
    }
}

int cmp (vector <int> a , vector <int> b){
    return a.size() > b.size();
}


int main()
{
    FILE *fin = stdin;
    FILE *fout = stdout;
    int x , y , i , a , b , elem;
    fscanf (fin,"%d",&n);
    for (i = 1 ; i < n ; i++){
        fscanf (fin,"%d%d",&x,&y);
        v[x].push_back(y);
        v[y].push_back(x);
    }

    /// solve pentru minim

    mini = 0;

    for (i = 1 ; i <= n ; i++){
        which[i] = i; /// ce nod e acum in locul in care se afla nodul i?
        wm[i] = i;
    }

    dfs (1 , 0);

    fprintf (fout,"%d %d\n" , mini , maxi);

    for (i = 1 ; i <= n ; i++)
        fprintf (fout,"%d ",which[i]);

    fprintf (fout,"\n");

    dfs2 (centroid , 1);

    a = 1;
    b = 2;

    elem = v[centroid].size();

    swap(w[1] , w[elem + 1]);

    sort (w + 1 , w + elem + 1 , cmp);

    while (a != b && a <= elem && b <= elem){

        swap(wm[w[a].back()] , wm[w[b].back()]);
        w[a].pop_back();
        w[b].pop_back();

        if (w[a].empty()){
            while (a <= elem){

                if (!w[a].empty())
                    break;

                a++;
            }

            if (a > elem)
                break;
        }

        b = max(b , a + 1);

        while (b <= elem){

            if (!w[b].empty())
                break;

            b++;
        }

        if (b > elem)
            break;

    }

    /// momentan ai pus tot ce era de pus

    int ok = 1;

    for (i = 1 ; i <= elem ; i++){
        if (!w[i].empty()){
            ok = 0;
            swap(wm[centroid] , wm[w[i][0]]);
            break;
        }
    }


    if (ok){
        if (centroid == 1){
            swap(wm[centroid] , wm[2]);
        }
        else swap(wm[centroid] , wm[centroid - 1]);
    }

    for (i = 1 ; i <= n ; i++)
        fprintf (fout,"%d ",wm[i]);

    return 0;
}

Compilation message

Village.cpp: In function 'void dfs(int, int)':
Village.cpp:13:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   13 |     for (i = 0 ; i < v[nod].size() ; i++){
      |                  ~~^~~~~~~~~~~~~~~
Village.cpp:42:28: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   42 |             for (i = 0 ; i < v[nod].size() ; i++){
      |                          ~~^~~~~~~~~~~~~~~
Village.cpp: In function 'void dfs2(int, int)':
Village.cpp:65:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   65 |     for (i = 0 ; i < v[nod].size() ; i++){
      |                  ~~^~~~~~~~~~~~~~~
Village.cpp: In function 'int main()':
Village.cpp:88:12: warning: ignoring return value of 'int fscanf(FILE*, const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   88 |     fscanf (fin,"%d",&n);
      |     ~~~~~~~^~~~~~~~~~~~~
Village.cpp:90:16: warning: ignoring return value of 'int fscanf(FILE*, const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   90 |         fscanf (fin,"%d%d",&x,&y);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4940 KB Output is correct
2 Correct 3 ms 4940 KB Output is correct
3 Correct 3 ms 4940 KB Output is correct
4 Correct 3 ms 4940 KB Output is correct
5 Correct 3 ms 4940 KB Output is correct
6 Correct 3 ms 4940 KB Output is correct
7 Correct 4 ms 4940 KB Output is correct
8 Correct 4 ms 4940 KB Output is correct
9 Correct 3 ms 4940 KB Output is correct
10 Correct 3 ms 4940 KB Output is correct
11 Correct 3 ms 4924 KB Output is correct
12 Correct 3 ms 4940 KB Output is correct
13 Correct 3 ms 4940 KB Output is correct
14 Correct 4 ms 4940 KB Output is correct
15 Correct 3 ms 4940 KB Output is correct
16 Correct 3 ms 4940 KB Output is correct
17 Correct 3 ms 4940 KB Output is correct
# Verdict Execution time Memory Grader output
1 Partially correct 3 ms 4940 KB Partially correct
2 Correct 4 ms 4940 KB Output is correct
3 Correct 4 ms 4940 KB Output is correct
4 Partially correct 4 ms 5068 KB Partially correct
5 Partially correct 4 ms 5068 KB Partially correct
6 Correct 4 ms 5072 KB Output is correct
7 Correct 4 ms 5068 KB Output is correct
8 Correct 4 ms 4940 KB Output is correct
9 Partially correct 4 ms 5064 KB Partially correct
10 Correct 5 ms 5068 KB Output is correct
11 Correct 4 ms 5004 KB Output is correct
12 Correct 5 ms 5068 KB Output is correct
13 Correct 4 ms 5068 KB Output is correct
14 Partially correct 5 ms 5068 KB Partially correct
15 Correct 4 ms 5068 KB Output is correct
16 Correct 4 ms 5068 KB Output is correct
17 Correct 4 ms 4964 KB Output is correct
18 Correct 4 ms 5068 KB Output is correct
19 Correct 4 ms 5068 KB Output is correct
20 Correct 4 ms 4940 KB Output is correct
21 Correct 4 ms 5068 KB Output is correct
22 Correct 4 ms 5068 KB Output is correct
23 Correct 4 ms 5068 KB Output is correct
24 Correct 5 ms 4940 KB Output is correct
25 Correct 4 ms 4940 KB Output is correct
26 Partially correct 5 ms 5068 KB Partially correct
27 Correct 4 ms 4940 KB Output is correct
28 Partially correct 4 ms 5068 KB Partially correct
29 Partially correct 4 ms 5068 KB Partially correct
30 Correct 4 ms 4976 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4940 KB Output is correct
2 Correct 3 ms 4940 KB Output is correct
3 Correct 3 ms 4940 KB Output is correct
4 Correct 3 ms 4940 KB Output is correct
5 Correct 3 ms 4940 KB Output is correct
6 Correct 3 ms 4940 KB Output is correct
7 Correct 4 ms 4940 KB Output is correct
8 Correct 4 ms 4940 KB Output is correct
9 Correct 3 ms 4940 KB Output is correct
10 Correct 3 ms 4940 KB Output is correct
11 Correct 3 ms 4924 KB Output is correct
12 Correct 3 ms 4940 KB Output is correct
13 Correct 3 ms 4940 KB Output is correct
14 Correct 4 ms 4940 KB Output is correct
15 Correct 3 ms 4940 KB Output is correct
16 Correct 3 ms 4940 KB Output is correct
17 Correct 3 ms 4940 KB Output is correct
18 Partially correct 3 ms 4940 KB Partially correct
19 Correct 4 ms 4940 KB Output is correct
20 Correct 4 ms 4940 KB Output is correct
21 Partially correct 4 ms 5068 KB Partially correct
22 Partially correct 4 ms 5068 KB Partially correct
23 Correct 4 ms 5072 KB Output is correct
24 Correct 4 ms 5068 KB Output is correct
25 Correct 4 ms 4940 KB Output is correct
26 Partially correct 4 ms 5064 KB Partially correct
27 Correct 5 ms 5068 KB Output is correct
28 Correct 4 ms 5004 KB Output is correct
29 Correct 5 ms 5068 KB Output is correct
30 Correct 4 ms 5068 KB Output is correct
31 Partially correct 5 ms 5068 KB Partially correct
32 Correct 4 ms 5068 KB Output is correct
33 Correct 4 ms 5068 KB Output is correct
34 Correct 4 ms 4964 KB Output is correct
35 Correct 4 ms 5068 KB Output is correct
36 Correct 4 ms 5068 KB Output is correct
37 Correct 4 ms 4940 KB Output is correct
38 Correct 4 ms 5068 KB Output is correct
39 Correct 4 ms 5068 KB Output is correct
40 Correct 4 ms 5068 KB Output is correct
41 Correct 5 ms 4940 KB Output is correct
42 Correct 4 ms 4940 KB Output is correct
43 Partially correct 5 ms 5068 KB Partially correct
44 Correct 4 ms 4940 KB Output is correct
45 Partially correct 4 ms 5068 KB Partially correct
46 Partially correct 4 ms 5068 KB Partially correct
47 Correct 4 ms 4976 KB Output is correct
48 Partially correct 75 ms 11088 KB Partially correct
49 Correct 96 ms 11700 KB Output is correct
50 Correct 104 ms 11656 KB Output is correct
51 Correct 61 ms 10108 KB Output is correct
52 Correct 85 ms 11452 KB Output is correct
53 Partially correct 76 ms 10940 KB Partially correct
54 Correct 47 ms 11844 KB Output is correct
55 Partially correct 122 ms 18872 KB Partially correct
56 Partially correct 99 ms 15040 KB Partially correct
57 Partially correct 104 ms 13696 KB Partially correct
58 Correct 104 ms 12576 KB Output is correct
59 Partially correct 88 ms 11520 KB Partially correct
60 Correct 156 ms 13880 KB Output is correct
61 Correct 87 ms 11996 KB Output is correct
62 Correct 87 ms 11756 KB Output is correct
63 Correct 79 ms 11416 KB Output is correct
64 Correct 86 ms 11852 KB Output is correct
65 Correct 75 ms 12040 KB Output is correct
66 Correct 72 ms 11444 KB Output is correct
67 Correct 68 ms 10140 KB Output is correct
68 Correct 68 ms 10896 KB Output is correct
69 Correct 83 ms 12108 KB Output is correct
70 Correct 70 ms 11628 KB Output is correct
71 Correct 53 ms 9856 KB Output is correct
72 Correct 58 ms 10528 KB Output is correct
73 Correct 100 ms 12084 KB Output is correct
74 Correct 79 ms 11500 KB Output is correct
75 Partially correct 96 ms 11568 KB Partially correct
76 Partially correct 89 ms 11420 KB Partially correct
77 Partially correct 105 ms 11712 KB Partially correct
78 Partially correct 51 ms 9412 KB Partially correct
79 Partially correct 65 ms 10296 KB Partially correct
80 Partially correct 90 ms 11444 KB Partially correct
81 Partially correct 77 ms 11716 KB Partially correct
82 Partially correct 76 ms 11844 KB Partially correct