Submission #411929

# Submission time Handle Problem Language Result Execution time Memory
411929 2021-05-26T09:21:09 Z Ruxandra985 Village (BOI20_village) C++14
75 / 100
124 ms 18944 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];
priority_queue <pair <int , int> > h;

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;


    if (nod != centroid)
        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 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 , 0);

    a = 1;
    b = 2;

    elem = v[centroid].size();

    for (i = 1 ; i <= elem ; i++)
        h.push(make_pair(w[i].size() , i));

    while (h.size() >= 2){

        a = h.top().second;
        h.pop();
        b = h.top().second;
        h.pop();

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

        if (w[a].size())
            h.push(make_pair(w[a].size() , a));
        if (w[b].size())
            h.push(make_pair(w[b].size() , b));

    }

    if (h.empty()){
        if (centroid == 1){
            swap(wm[centroid] , wm[2]);
        }
        else swap(wm[centroid] , wm[centroid - 1]);
    }
    else {
        a = h.top().second;
        swap(wm[w[a].back()] , wm[centroid]);
    }

    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:14:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   14 |     for (i = 0 ; i < v[nod].size() ; i++){
      |                  ~~^~~~~~~~~~~~~~~
Village.cpp:43:28: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   43 |             for (i = 0 ; i < v[nod].size() ; i++){
      |                          ~~^~~~~~~~~~~~~~~
Village.cpp: In function 'void dfs2(int, int)':
Village.cpp:68:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   68 |     for (i = 0 ; i < v[nod].size() ; i++){
      |                  ~~^~~~~~~~~~~~~~~
Village.cpp: In function 'int main()':
Village.cpp:86:12: warning: ignoring return value of 'int fscanf(FILE*, const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   86 |     fscanf (fin,"%d",&n);
      |     ~~~~~~~^~~~~~~~~~~~~
Village.cpp:88:16: warning: ignoring return value of 'int fscanf(FILE*, const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   88 |         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 4904 KB Output is correct
4 Correct 3 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 3 ms 4940 KB Output is correct
8 Correct 3 ms 4940 KB Output is correct
9 Correct 4 ms 4940 KB Output is correct
10 Correct 4 ms 4940 KB Output is correct
11 Correct 3 ms 4940 KB Output is correct
12 Correct 3 ms 4940 KB Output is correct
13 Correct 3 ms 4940 KB Output is correct
14 Correct 3 ms 4940 KB Output is correct
15 Correct 4 ms 5068 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 Correct 3 ms 4940 KB Output is correct
2 Correct 4 ms 4940 KB Output is correct
3 Correct 4 ms 4992 KB Output is correct
4 Correct 4 ms 4940 KB Output is correct
5 Correct 4 ms 5068 KB Output is correct
6 Correct 4 ms 5068 KB Output is correct
7 Correct 4 ms 5068 KB Output is correct
8 Correct 4 ms 5068 KB Output is correct
9 Correct 4 ms 5068 KB Output is correct
10 Correct 4 ms 5068 KB Output is correct
11 Correct 4 ms 5068 KB Output is correct
12 Correct 4 ms 5068 KB Output is correct
13 Correct 4 ms 5068 KB Output is correct
14 Correct 4 ms 4940 KB Output is correct
15 Correct 4 ms 5068 KB Output is correct
16 Correct 4 ms 5068 KB Output is correct
17 Correct 4 ms 5068 KB Output is correct
18 Correct 4 ms 4940 KB Output is correct
19 Correct 4 ms 5068 KB Output is correct
20 Correct 4 ms 5028 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 4940 KB Output is correct
24 Correct 4 ms 4940 KB Output is correct
25 Correct 4 ms 4940 KB Output is correct
26 Correct 4 ms 5068 KB Output is correct
27 Correct 4 ms 4940 KB Output is correct
28 Correct 6 ms 5068 KB Output is correct
29 Correct 4 ms 5068 KB Output is correct
30 Correct 4 ms 5068 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 4904 KB Output is correct
4 Correct 3 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 3 ms 4940 KB Output is correct
8 Correct 3 ms 4940 KB Output is correct
9 Correct 4 ms 4940 KB Output is correct
10 Correct 4 ms 4940 KB Output is correct
11 Correct 3 ms 4940 KB Output is correct
12 Correct 3 ms 4940 KB Output is correct
13 Correct 3 ms 4940 KB Output is correct
14 Correct 3 ms 4940 KB Output is correct
15 Correct 4 ms 5068 KB Output is correct
16 Correct 3 ms 4940 KB Output is correct
17 Correct 3 ms 4940 KB Output is correct
18 Correct 3 ms 4940 KB Output is correct
19 Correct 4 ms 4940 KB Output is correct
20 Correct 4 ms 4992 KB Output is correct
21 Correct 4 ms 4940 KB Output is correct
22 Correct 4 ms 5068 KB Output is correct
23 Correct 4 ms 5068 KB Output is correct
24 Correct 4 ms 5068 KB Output is correct
25 Correct 4 ms 5068 KB Output is correct
26 Correct 4 ms 5068 KB Output is correct
27 Correct 4 ms 5068 KB Output is correct
28 Correct 4 ms 5068 KB Output is correct
29 Correct 4 ms 5068 KB Output is correct
30 Correct 4 ms 5068 KB Output is correct
31 Correct 4 ms 4940 KB Output is correct
32 Correct 4 ms 5068 KB Output is correct
33 Correct 4 ms 5068 KB Output is correct
34 Correct 4 ms 5068 KB Output is correct
35 Correct 4 ms 4940 KB Output is correct
36 Correct 4 ms 5068 KB Output is correct
37 Correct 4 ms 5028 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 4940 KB Output is correct
41 Correct 4 ms 4940 KB Output is correct
42 Correct 4 ms 4940 KB Output is correct
43 Correct 4 ms 5068 KB Output is correct
44 Correct 4 ms 4940 KB Output is correct
45 Correct 6 ms 5068 KB Output is correct
46 Correct 4 ms 5068 KB Output is correct
47 Correct 4 ms 5068 KB Output is correct
48 Correct 84 ms 10952 KB Output is correct
49 Correct 87 ms 11572 KB Output is correct
50 Correct 84 ms 11456 KB Output is correct
51 Correct 72 ms 10120 KB Output is correct
52 Correct 93 ms 11444 KB Output is correct
53 Correct 78 ms 10836 KB Output is correct
54 Correct 45 ms 11728 KB Output is correct
55 Partially correct 107 ms 18944 KB Partially correct
56 Partially correct 97 ms 14860 KB Partially correct
57 Correct 124 ms 13656 KB Output is correct
58 Correct 94 ms 12612 KB Output is correct
59 Correct 88 ms 11588 KB Output is correct
60 Correct 84 ms 14844 KB Output is correct
61 Correct 76 ms 12292 KB Output is correct
62 Correct 87 ms 11676 KB Output is correct
63 Correct 80 ms 11344 KB Output is correct
64 Correct 97 ms 11820 KB Output is correct
65 Correct 95 ms 11968 KB Output is correct
66 Correct 89 ms 11356 KB Output is correct
67 Correct 56 ms 10308 KB Output is correct
68 Correct 66 ms 10840 KB Output is correct
69 Correct 81 ms 11968 KB Output is correct
70 Correct 75 ms 11484 KB Output is correct
71 Correct 51 ms 9688 KB Output is correct
72 Correct 66 ms 10448 KB Output is correct
73 Correct 101 ms 11968 KB Output is correct
74 Correct 70 ms 11376 KB Output is correct
75 Correct 111 ms 11328 KB Output is correct
76 Correct 91 ms 11296 KB Output is correct
77 Correct 86 ms 11584 KB Output is correct
78 Correct 54 ms 9420 KB Output is correct
79 Correct 68 ms 10180 KB Output is correct
80 Correct 91 ms 11336 KB Output is correct
81 Correct 83 ms 11696 KB Output is correct
82 Correct 83 ms 11720 KB Output is correct