Submission #1094673

# Submission time Handle Problem Language Result Execution time Memory
1094673 2024-09-30T09:09:09 Z andrei_iorgulescu Village (BOI20_village) C++14
50 / 100
86 ms 26640 KB
#include <bits/stdc++.h>

using namespace std;

#define int long long

int n;
vector<int> g[100005], f[100005];
int t[100005], sz[100005], h[100005];
int nrf[100005];

int ans_mic, ans_mare, sol_mic[100005], sol_mare[100005];

void dfs(int nod)
{
    sz[nod] = 1;
    for (auto vecin : g[nod])
    {
        if (vecin != t[nod])
        {
            f[nod].push_back(vecin);
            t[vecin] = nod;
            h[vecin] = 1 + h[nod];
            dfs(vecin);
            sz[nod] += sz[vecin];
        }
    }
}

void solve_mic()
{
    vector<bool> scos(n + 1);
    for (int i = 1; i <= n; i++)
        nrf[i] = f[i].size();
    vector<int> nd;
    for (int i = 1; i <= n; i++)
        nd.push_back(i);
    sort(nd.begin(), nd.end(), [](int A, int B)
    {
        return h[A] > h[B];
    });
    for (auto it : nd)
    {
        if (nrf[it] != 0 or it == 1)
        {
            //cout << it << endl;
            if (nrf[it] % 2 == 1)
            {
                vector<int> cn;
                for (auto itt : f[it])
                {
                    if (!scos[itt])
                        cn.push_back(itt), scos[itt] = true;
                }
                scos[it] = true;
                nrf[t[it]]--;
                ans_mic += 2;
                sol_mic[it] = cn[0];
                sol_mic[cn[0]] = it;
                for (int i = 1; i < cn.size(); i += 2)
                {
                    ans_mic += 4;
                    sol_mic[cn[i]] = cn[i + 1];
                    sol_mic[cn[i + 1]] = cn[i];
                }
            }
            else
            {
                vector<int> cn;
                for (auto itt : f[it])
                {
                    if (!scos[itt])
                        cn.push_back(itt), scos[itt] = true;
                }
                if (it != 1)
                {
                    for (int i = 0; i < cn.size(); i += 2)
                    {
                        ans_mic += 4;
                        sol_mic[cn[i]] = cn[i + 1];
                        sol_mic[cn[i + 1]] = cn[i];
                    }
                }
                else
                {
                    if (cn.size() == 0)
                    {
                        int x = f[1][0];
                        int y = sol_mic[x];
                        ans_mic += 2;
                        sol_mic[1] = x;
                        sol_mic[y] = 1;
                    }
                    else
                    {
                        int x = cn[0], y = cn[1];
                        ans_mic += 4;
                        sol_mic[x] = 1;
                        sol_mic[1] = y;
                        sol_mic[y] = x;
                        for (int i = 2; i < cn.size(); i += 2)
                        {
                            ans_mic += 4;
                            sol_mic[cn[i]] = cn[i + 1];
                            sol_mic[cn[i + 1]] = cn[i];
                        }
                    }
                }
            }
        }
    }
}

int find_centroid(int nod)
{
    int ff = 0;
    for (auto it : f[nod])
        if (sz[it] > n / 2)
            ff = it;
    if (ff != 0)
        return find_centroid(ff);
    return nod;
}

vector<int> noduri;

void dfs2(int nod)
{
    noduri.push_back(nod);
    for (auto it : f[nod])
        dfs2(it);
}

void solve_mare()
{
    int c = find_centroid(1);
    for (int i = 1; i <= n; i++)
    {
        f[i].clear();
        t[i] = 0;
        sz[i] = 0;
        h[i] = 0;
    }
    dfs(c);
    for (int i = 1; i <= n; i++)
        ans_mare += 2 * h[i];
    dfs2(c);
    int ofs = (n + 1) / 2;
    for (int i = 0; i < noduri.size(); i++)
    {
        int x = (i + ofs) % n;
        //if (how[noduri[i]] == how[noduri[x]])
          //  assert(false);
        sol_mare[noduri[i]] = noduri[x];
    }
}

signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    cin >> n;
    for (int i = 1; i < n; i++)
    {
        int x, y;
        cin >> x >> y;
        g[x].push_back(y);
        g[y].push_back(x);
    }
    dfs(1);
    solve_mic();
    solve_mare();
    cout << ans_mic << ' ' << ans_mare << '\n';
    for (int i = 1; i <= n; i++)
        cout << sol_mic[i] << ' ';
    cout << '\n';
    for (int i = 1; i <= n; i++)
        cout << sol_mare[i] << ' ';
    return 0;
}

Compilation message

Village.cpp: In function 'void solve_mic()':
Village.cpp:60:35: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   60 |                 for (int i = 1; i < cn.size(); i += 2)
      |                                 ~~^~~~~~~~~~~
Village.cpp:77:39: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   77 |                     for (int i = 0; i < cn.size(); i += 2)
      |                                     ~~^~~~~~~~~~~
Village.cpp:101:43: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  101 |                         for (int i = 2; i < cn.size(); i += 2)
      |                                         ~~^~~~~~~~~~~
Village.cpp: In function 'void solve_mare()':
Village.cpp:149:23: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  149 |     for (int i = 0; i < noduri.size(); i++)
      |                     ~~^~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4956 KB Output is correct
2 Correct 2 ms 4956 KB Output is correct
3 Correct 2 ms 4956 KB Output is correct
4 Correct 2 ms 4956 KB Output is correct
5 Correct 2 ms 4952 KB Output is correct
6 Correct 3 ms 4956 KB Output is correct
7 Correct 2 ms 4956 KB Output is correct
8 Correct 2 ms 4956 KB Output is correct
9 Correct 3 ms 4952 KB Output is correct
10 Correct 3 ms 4956 KB Output is correct
11 Correct 2 ms 4932 KB Output is correct
12 Correct 2 ms 4956 KB Output is correct
13 Partially correct 3 ms 4956 KB Partially correct
14 Correct 3 ms 4956 KB Output is correct
15 Partially correct 2 ms 4952 KB Partially correct
16 Partially correct 2 ms 4956 KB Partially correct
17 Partially correct 2 ms 4956 KB Partially correct
# Verdict Execution time Memory Grader output
1 Partially correct 2 ms 5212 KB Partially correct
2 Partially correct 2 ms 5212 KB Partially correct
3 Partially correct 2 ms 5208 KB Partially correct
4 Partially correct 3 ms 5212 KB Partially correct
5 Partially correct 3 ms 5212 KB Partially correct
6 Partially correct 3 ms 5212 KB Partially correct
7 Correct 2 ms 5208 KB Output is correct
8 Partially correct 4 ms 5212 KB Partially correct
9 Partially correct 3 ms 5212 KB Partially correct
10 Partially correct 2 ms 5212 KB Partially correct
11 Partially correct 3 ms 5212 KB Partially correct
12 Correct 2 ms 5212 KB Output is correct
13 Correct 4 ms 5212 KB Output is correct
14 Partially correct 3 ms 5212 KB Partially correct
15 Partially correct 2 ms 5212 KB Partially correct
16 Partially correct 4 ms 5212 KB Partially correct
17 Partially correct 2 ms 5212 KB Partially correct
18 Partially correct 4 ms 5464 KB Partially correct
19 Partially correct 3 ms 5208 KB Partially correct
20 Partially correct 2 ms 5212 KB Partially correct
21 Partially correct 3 ms 5212 KB Partially correct
22 Partially correct 3 ms 5248 KB Partially correct
23 Partially correct 3 ms 5208 KB Partially correct
24 Partially correct 3 ms 5212 KB Partially correct
25 Partially correct 2 ms 5212 KB Partially correct
26 Partially correct 4 ms 5208 KB Partially correct
27 Partially correct 2 ms 5212 KB Partially correct
28 Partially correct 3 ms 5208 KB Partially correct
29 Partially correct 3 ms 5212 KB Partially correct
30 Partially correct 2 ms 5212 KB Partially correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4956 KB Output is correct
2 Correct 2 ms 4956 KB Output is correct
3 Correct 2 ms 4956 KB Output is correct
4 Correct 2 ms 4956 KB Output is correct
5 Correct 2 ms 4952 KB Output is correct
6 Correct 3 ms 4956 KB Output is correct
7 Correct 2 ms 4956 KB Output is correct
8 Correct 2 ms 4956 KB Output is correct
9 Correct 3 ms 4952 KB Output is correct
10 Correct 3 ms 4956 KB Output is correct
11 Correct 2 ms 4932 KB Output is correct
12 Correct 2 ms 4956 KB Output is correct
13 Partially correct 3 ms 4956 KB Partially correct
14 Correct 3 ms 4956 KB Output is correct
15 Partially correct 2 ms 4952 KB Partially correct
16 Partially correct 2 ms 4956 KB Partially correct
17 Partially correct 2 ms 4956 KB Partially correct
18 Partially correct 2 ms 5212 KB Partially correct
19 Partially correct 2 ms 5212 KB Partially correct
20 Partially correct 2 ms 5208 KB Partially correct
21 Partially correct 3 ms 5212 KB Partially correct
22 Partially correct 3 ms 5212 KB Partially correct
23 Partially correct 3 ms 5212 KB Partially correct
24 Correct 2 ms 5208 KB Output is correct
25 Partially correct 4 ms 5212 KB Partially correct
26 Partially correct 3 ms 5212 KB Partially correct
27 Partially correct 2 ms 5212 KB Partially correct
28 Partially correct 3 ms 5212 KB Partially correct
29 Correct 2 ms 5212 KB Output is correct
30 Correct 4 ms 5212 KB Output is correct
31 Partially correct 3 ms 5212 KB Partially correct
32 Partially correct 2 ms 5212 KB Partially correct
33 Partially correct 4 ms 5212 KB Partially correct
34 Partially correct 2 ms 5212 KB Partially correct
35 Partially correct 4 ms 5464 KB Partially correct
36 Partially correct 3 ms 5208 KB Partially correct
37 Partially correct 2 ms 5212 KB Partially correct
38 Partially correct 3 ms 5212 KB Partially correct
39 Partially correct 3 ms 5248 KB Partially correct
40 Partially correct 3 ms 5208 KB Partially correct
41 Partially correct 3 ms 5212 KB Partially correct
42 Partially correct 2 ms 5212 KB Partially correct
43 Partially correct 4 ms 5208 KB Partially correct
44 Partially correct 2 ms 5212 KB Partially correct
45 Partially correct 3 ms 5208 KB Partially correct
46 Partially correct 3 ms 5212 KB Partially correct
47 Partially correct 2 ms 5212 KB Partially correct
48 Partially correct 56 ms 17152 KB Partially correct
49 Partially correct 86 ms 18380 KB Partially correct
50 Partially correct 60 ms 18356 KB Partially correct
51 Partially correct 43 ms 15724 KB Partially correct
52 Partially correct 63 ms 18228 KB Partially correct
53 Partially correct 62 ms 16896 KB Partially correct
54 Correct 33 ms 15428 KB Output is correct
55 Correct 82 ms 26640 KB Output is correct
56 Partially correct 69 ms 22140 KB Partially correct
57 Partially correct 72 ms 22008 KB Partially correct
58 Partially correct 74 ms 20932 KB Partially correct
59 Partially correct 62 ms 19380 KB Partially correct
60 Correct 36 ms 18484 KB Output is correct
61 Partially correct 65 ms 18516 KB Partially correct
62 Partially correct 45 ms 18748 KB Partially correct
63 Partially correct 48 ms 18096 KB Partially correct
64 Partially correct 46 ms 19396 KB Partially correct
65 Partially correct 84 ms 19564 KB Partially correct
66 Partially correct 47 ms 18144 KB Partially correct
67 Partially correct 29 ms 15364 KB Partially correct
68 Partially correct 47 ms 17196 KB Partially correct
69 Partially correct 49 ms 19384 KB Partially correct
70 Partially correct 50 ms 18408 KB Partially correct
71 Partially correct 52 ms 15276 KB Partially correct
72 Partially correct 33 ms 16460 KB Partially correct
73 Partially correct 74 ms 19392 KB Partially correct
74 Partially correct 48 ms 18116 KB Partially correct
75 Partially correct 61 ms 19500 KB Partially correct
76 Correct 61 ms 19124 KB Output is correct
77 Partially correct 54 ms 19176 KB Partially correct
78 Partially correct 34 ms 14732 KB Partially correct
79 Partially correct 37 ms 15972 KB Partially correct
80 Partially correct 69 ms 19260 KB Partially correct
81 Partially correct 45 ms 19436 KB Partially correct
82 Partially correct 38 ms 18628 KB Partially correct