답안 #411904

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
411904 2021-05-26T08:20:54 Z Ruxandra985 Village (BOI20_village) C++14
50 / 100
95 ms 17072 KB
#include <bits/stdc++.h>
#define DIMN 100010
using namespace std;
int n , mini , maxi , fth , centroid;
int which[DIMN] , wm[DIMN] , sub[DIMN] , under[DIMN];
vector <int> v[DIMN] , su , supra;

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 tt){
    int i , vecin;


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

            under[vecin] = 1;

        }
    }
}


int main()
{
    FILE *fin = stdin;
    FILE *fout = stdout;
    int x , y , i;
    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 , fth);

    for (i = 1 ; i <= n ; i++){
        if (under[i])
            su.push_back(i);
        else if (i != centroid)
            supra.push_back(i);
    }

    while (!su.empty() && !supra.empty()){
        swap(wm[su.back()] , wm[supra.back()]);
        su.pop_back();
        supra.pop_back();
    }

    for (i = 0 ; i < v[centroid].size() ; i++){
        int vecin = v[centroid][i];
        swap(wm[centroid] , wm[vecin]);
        break;
    }


    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:62:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   62 |     for (i = 0 ; i < v[nod].size() ; i++){
      |                  ~~^~~~~~~~~~~~~~~
Village.cpp: In function 'int main()':
Village.cpp:119:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  119 |     for (i = 0 ; i < v[centroid].size() ; i++){
      |                  ~~^~~~~~~~~~~~~~~~~~~~
Village.cpp:79:12: warning: ignoring return value of 'int fscanf(FILE*, const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   79 |     fscanf (fin,"%d",&n);
      |     ~~~~~~~^~~~~~~~~~~~~
Village.cpp:81:16: warning: ignoring return value of 'int fscanf(FILE*, const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   81 |         fscanf (fin,"%d%d",&x,&y);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Partially correct 3 ms 2652 KB Partially correct
2 Correct 2 ms 2636 KB Output is correct
3 Correct 2 ms 2636 KB Output is correct
4 Partially correct 2 ms 2636 KB Partially correct
5 Correct 2 ms 2636 KB Output is correct
6 Correct 2 ms 2636 KB Output is correct
7 Partially correct 2 ms 2648 KB Partially correct
8 Partially correct 2 ms 2764 KB Partially correct
9 Partially correct 2 ms 2636 KB Partially correct
10 Partially correct 2 ms 2636 KB Partially correct
11 Partially correct 2 ms 2644 KB Partially correct
12 Partially correct 2 ms 2636 KB Partially correct
13 Partially correct 2 ms 2636 KB Partially correct
14 Partially correct 2 ms 2648 KB Partially correct
15 Partially correct 2 ms 2648 KB Partially correct
16 Partially correct 2 ms 2644 KB Partially correct
17 Partially correct 2 ms 2636 KB Partially correct
# 결과 실행 시간 메모리 Grader output
1 Partially correct 2 ms 2636 KB Partially correct
2 Partially correct 2 ms 2636 KB Partially correct
3 Partially correct 2 ms 2636 KB Partially correct
4 Partially correct 3 ms 2636 KB Partially correct
5 Partially correct 3 ms 2636 KB Partially correct
6 Partially correct 3 ms 2652 KB Partially correct
7 Partially correct 3 ms 2764 KB Partially correct
8 Partially correct 3 ms 2652 KB Partially correct
9 Partially correct 3 ms 2636 KB Partially correct
10 Partially correct 3 ms 2652 KB Partially correct
11 Partially correct 3 ms 2636 KB Partially correct
12 Partially correct 3 ms 2636 KB Partially correct
13 Partially correct 3 ms 2652 KB Partially correct
14 Partially correct 3 ms 2636 KB Partially correct
15 Partially correct 3 ms 2680 KB Partially correct
16 Partially correct 3 ms 2636 KB Partially correct
17 Partially correct 3 ms 2636 KB Partially correct
18 Partially correct 3 ms 2636 KB Partially correct
19 Partially correct 3 ms 2648 KB Partially correct
20 Partially correct 3 ms 2636 KB Partially correct
21 Partially correct 3 ms 2636 KB Partially correct
22 Partially correct 3 ms 2636 KB Partially correct
23 Partially correct 3 ms 2648 KB Partially correct
24 Partially correct 3 ms 2636 KB Partially correct
25 Partially correct 3 ms 2648 KB Partially correct
26 Partially correct 4 ms 2636 KB Partially correct
27 Correct 2 ms 2636 KB Output is correct
28 Partially correct 3 ms 2648 KB Partially correct
29 Partially correct 3 ms 2652 KB Partially correct
30 Partially correct 3 ms 2636 KB Partially correct
# 결과 실행 시간 메모리 Grader output
1 Partially correct 3 ms 2652 KB Partially correct
2 Correct 2 ms 2636 KB Output is correct
3 Correct 2 ms 2636 KB Output is correct
4 Partially correct 2 ms 2636 KB Partially correct
5 Correct 2 ms 2636 KB Output is correct
6 Correct 2 ms 2636 KB Output is correct
7 Partially correct 2 ms 2648 KB Partially correct
8 Partially correct 2 ms 2764 KB Partially correct
9 Partially correct 2 ms 2636 KB Partially correct
10 Partially correct 2 ms 2636 KB Partially correct
11 Partially correct 2 ms 2644 KB Partially correct
12 Partially correct 2 ms 2636 KB Partially correct
13 Partially correct 2 ms 2636 KB Partially correct
14 Partially correct 2 ms 2648 KB Partially correct
15 Partially correct 2 ms 2648 KB Partially correct
16 Partially correct 2 ms 2644 KB Partially correct
17 Partially correct 2 ms 2636 KB Partially correct
18 Partially correct 2 ms 2636 KB Partially correct
19 Partially correct 2 ms 2636 KB Partially correct
20 Partially correct 2 ms 2636 KB Partially correct
21 Partially correct 3 ms 2636 KB Partially correct
22 Partially correct 3 ms 2636 KB Partially correct
23 Partially correct 3 ms 2652 KB Partially correct
24 Partially correct 3 ms 2764 KB Partially correct
25 Partially correct 3 ms 2652 KB Partially correct
26 Partially correct 3 ms 2636 KB Partially correct
27 Partially correct 3 ms 2652 KB Partially correct
28 Partially correct 3 ms 2636 KB Partially correct
29 Partially correct 3 ms 2636 KB Partially correct
30 Partially correct 3 ms 2652 KB Partially correct
31 Partially correct 3 ms 2636 KB Partially correct
32 Partially correct 3 ms 2680 KB Partially correct
33 Partially correct 3 ms 2636 KB Partially correct
34 Partially correct 3 ms 2636 KB Partially correct
35 Partially correct 3 ms 2636 KB Partially correct
36 Partially correct 3 ms 2648 KB Partially correct
37 Partially correct 3 ms 2636 KB Partially correct
38 Partially correct 3 ms 2636 KB Partially correct
39 Partially correct 3 ms 2636 KB Partially correct
40 Partially correct 3 ms 2648 KB Partially correct
41 Partially correct 3 ms 2636 KB Partially correct
42 Partially correct 3 ms 2648 KB Partially correct
43 Partially correct 4 ms 2636 KB Partially correct
44 Correct 2 ms 2636 KB Output is correct
45 Partially correct 3 ms 2648 KB Partially correct
46 Partially correct 3 ms 2652 KB Partially correct
47 Partially correct 3 ms 2636 KB Partially correct
48 Partially correct 74 ms 8836 KB Partially correct
49 Partially correct 91 ms 9480 KB Partially correct
50 Partially correct 90 ms 9512 KB Partially correct
51 Partially correct 66 ms 8124 KB Partially correct
52 Partially correct 88 ms 9468 KB Partially correct
53 Partially correct 74 ms 8764 KB Partially correct
54 Partially correct 44 ms 9672 KB Partially correct
55 Partially correct 94 ms 17072 KB Partially correct
56 Partially correct 95 ms 13028 KB Partially correct
57 Partially correct 85 ms 11844 KB Partially correct
58 Partially correct 92 ms 10688 KB Partially correct
59 Partially correct 80 ms 9660 KB Partially correct
60 Partially correct 67 ms 9456 KB Partially correct
61 Partially correct 70 ms 9404 KB Partially correct
62 Partially correct 72 ms 9772 KB Partially correct
63 Partially correct 73 ms 9400 KB Partially correct
64 Partially correct 77 ms 9804 KB Partially correct
65 Partially correct 78 ms 9916 KB Partially correct
66 Partially correct 71 ms 9384 KB Partially correct
67 Partially correct 56 ms 7740 KB Partially correct
68 Partially correct 69 ms 8864 KB Partially correct
69 Partially correct 78 ms 9916 KB Partially correct
70 Partially correct 65 ms 9500 KB Partially correct
71 Partially correct 49 ms 7764 KB Partially correct
72 Partially correct 53 ms 8452 KB Partially correct
73 Partially correct 73 ms 9968 KB Partially correct
74 Partially correct 63 ms 9376 KB Partially correct
75 Partially correct 93 ms 9448 KB Partially correct
76 Partially correct 77 ms 9340 KB Partially correct
77 Partially correct 70 ms 9576 KB Partially correct
78 Partially correct 47 ms 7352 KB Partially correct
79 Partially correct 64 ms 8160 KB Partially correct
80 Partially correct 82 ms 9448 KB Partially correct
81 Partially correct 74 ms 9660 KB Partially correct
82 Partially correct 83 ms 9764 KB Partially correct