Submission #411919

# Submission time Handle Problem Language Result Execution time Memory
411919 2021-05-26T09:08:54 Z Ruxandra985 Village (BOI20_village) C++14
50 / 100
162 ms 19200 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){

        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

    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 4 ms 4944 KB Output is correct
2 Correct 3 ms 4940 KB Output is correct
3 Partially correct 3 ms 4940 KB Partially correct
4 Correct 5 ms 4940 KB Output is correct
5 Correct 4 ms 4940 KB Output is correct
6 Correct 4 ms 4940 KB Output is correct
7 Correct 4 ms 4940 KB Output is correct
8 Partially correct 4 ms 4940 KB Partially correct
9 Correct 4 ms 4888 KB Output is correct
10 Partially correct 3 ms 4952 KB Partially correct
11 Partially correct 3 ms 4940 KB Partially correct
12 Partially correct 3 ms 4940 KB Partially correct
13 Partially correct 3 ms 4940 KB Partially correct
14 Correct 3 ms 4940 KB Output is correct
15 Partially correct 3 ms 4940 KB Partially correct
16 Correct 3 ms 4940 KB Output is correct
17 Partially correct 3 ms 4940 KB Partially correct
# Verdict Execution time Memory Grader output
1 Partially correct 4 ms 4940 KB Partially correct
2 Partially correct 4 ms 4940 KB Partially 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 5068 KB Output is correct
7 Partially correct 4 ms 5068 KB Partially correct
8 Correct 5 ms 5068 KB Output is correct
9 Partially correct 4 ms 5068 KB Partially correct
10 Partially correct 5 ms 5012 KB Partially correct
11 Partially correct 4 ms 5000 KB Partially correct
12 Partially correct 6 ms 5000 KB Partially correct
13 Correct 5 ms 5068 KB Output is correct
14 Partially correct 4 ms 5068 KB Partially correct
15 Partially correct 4 ms 5068 KB Partially correct
16 Partially correct 4 ms 5068 KB Partially correct
17 Correct 4 ms 4996 KB Output is correct
18 Correct 5 ms 5068 KB Output is correct
19 Partially correct 4 ms 5000 KB Partially correct
20 Partially correct 6 ms 5068 KB Partially correct
21 Partially correct 5 ms 5000 KB Partially correct
22 Correct 5 ms 5068 KB Output is correct
23 Partially correct 4 ms 5000 KB Partially correct
24 Partially correct 5 ms 4976 KB Partially correct
25 Partially correct 4 ms 4992 KB Partially correct
26 Partially correct 5 ms 5068 KB Partially correct
27 Correct 4 ms 4996 KB Output is correct
28 Partially correct 4 ms 5000 KB Partially correct
29 Partially correct 4 ms 5068 KB Partially correct
30 Partially correct 5 ms 5068 KB Partially correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 4944 KB Output is correct
2 Correct 3 ms 4940 KB Output is correct
3 Partially correct 3 ms 4940 KB Partially correct
4 Correct 5 ms 4940 KB Output is correct
5 Correct 4 ms 4940 KB Output is correct
6 Correct 4 ms 4940 KB Output is correct
7 Correct 4 ms 4940 KB Output is correct
8 Partially correct 4 ms 4940 KB Partially correct
9 Correct 4 ms 4888 KB Output is correct
10 Partially correct 3 ms 4952 KB Partially correct
11 Partially correct 3 ms 4940 KB Partially correct
12 Partially correct 3 ms 4940 KB Partially correct
13 Partially correct 3 ms 4940 KB Partially correct
14 Correct 3 ms 4940 KB Output is correct
15 Partially correct 3 ms 4940 KB Partially correct
16 Correct 3 ms 4940 KB Output is correct
17 Partially correct 3 ms 4940 KB Partially correct
18 Partially correct 4 ms 4940 KB Partially correct
19 Partially correct 4 ms 4940 KB Partially 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 5068 KB Output is correct
24 Partially correct 4 ms 5068 KB Partially correct
25 Correct 5 ms 5068 KB Output is correct
26 Partially correct 4 ms 5068 KB Partially correct
27 Partially correct 5 ms 5012 KB Partially correct
28 Partially correct 4 ms 5000 KB Partially correct
29 Partially correct 6 ms 5000 KB Partially correct
30 Correct 5 ms 5068 KB Output is correct
31 Partially correct 4 ms 5068 KB Partially correct
32 Partially correct 4 ms 5068 KB Partially correct
33 Partially correct 4 ms 5068 KB Partially correct
34 Correct 4 ms 4996 KB Output is correct
35 Correct 5 ms 5068 KB Output is correct
36 Partially correct 4 ms 5000 KB Partially correct
37 Partially correct 6 ms 5068 KB Partially correct
38 Partially correct 5 ms 5000 KB Partially correct
39 Correct 5 ms 5068 KB Output is correct
40 Partially correct 4 ms 5000 KB Partially correct
41 Partially correct 5 ms 4976 KB Partially correct
42 Partially correct 4 ms 4992 KB Partially correct
43 Partially correct 5 ms 5068 KB Partially correct
44 Correct 4 ms 4996 KB Output is correct
45 Partially correct 4 ms 5000 KB Partially correct
46 Partially correct 4 ms 5068 KB Partially correct
47 Partially correct 5 ms 5068 KB Partially correct
48 Partially correct 106 ms 11312 KB Partially correct
49 Correct 109 ms 11956 KB Output is correct
50 Partially correct 106 ms 11936 KB Partially correct
51 Correct 70 ms 10332 KB Output is correct
52 Correct 91 ms 11728 KB Output is correct
53 Partially correct 87 ms 11160 KB Partially correct
54 Partially correct 63 ms 12100 KB Partially correct
55 Partially correct 128 ms 19200 KB Partially correct
56 Partially correct 107 ms 15384 KB Partially correct
57 Partially correct 128 ms 13992 KB Partially correct
58 Partially correct 102 ms 12880 KB Partially correct
59 Partially correct 101 ms 11856 KB Partially correct
60 Partially correct 162 ms 14196 KB Partially correct
61 Partially correct 108 ms 12268 KB Partially correct
62 Partially correct 79 ms 12072 KB Partially correct
63 Correct 93 ms 11628 KB Output is correct
64 Partially correct 94 ms 12060 KB Partially correct
65 Partially correct 79 ms 12324 KB Partially correct
66 Correct 83 ms 11704 KB Output is correct
67 Partially correct 61 ms 10384 KB Partially correct
68 Correct 86 ms 11172 KB Output is correct
69 Partially correct 84 ms 12348 KB Partially correct
70 Partially correct 73 ms 11792 KB Partially correct
71 Partially correct 52 ms 10112 KB Partially correct
72 Correct 61 ms 10932 KB Output is correct
73 Correct 109 ms 12296 KB Output is correct
74 Correct 71 ms 11736 KB Output is correct
75 Partially correct 94 ms 11824 KB Partially correct
76 Partially correct 91 ms 11792 KB Partially correct
77 Partially correct 85 ms 11884 KB Partially correct
78 Partially correct 52 ms 9696 KB Partially correct
79 Partially correct 61 ms 10480 KB Partially correct
80 Partially correct 89 ms 11736 KB Partially correct
81 Partially correct 85 ms 11928 KB Partially correct
82 Partially correct 85 ms 12012 KB Partially correct