Submission #1094667

# Submission time Handle Problem Language Result Execution time Memory
1094667 2024-09-30T09:00:53 Z andrei_iorgulescu Village (BOI20_village) C++14
25 / 100
71 ms 25920 KB
#include <bits/stdc++.h>

using namespace std;

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;
        sol_mare[noduri[i]] = noduri[x];
    }
}

int 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:58:35: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   58 |                 for (int i = 1; i < cn.size(); i += 2)
      |                                 ~~^~~~~~~~~~~
Village.cpp:75:39: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   75 |                     for (int i = 0; i < cn.size(); i += 2)
      |                                     ~~^~~~~~~~~~~
Village.cpp:99:43: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   99 |                         for (int i = 2; i < cn.size(); i += 2)
      |                                         ~~^~~~~~~~~~~
Village.cpp: In function 'void solve_mare()':
Village.cpp:147:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  147 |     for (int i = 0; i < noduri.size(); i++)
      |                     ~~^~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4952 KB Output is correct
2 Correct 2 ms 4956 KB Output is correct
3 Correct 2 ms 5184 KB Output is correct
4 Correct 3 ms 4956 KB Output is correct
5 Correct 2 ms 4956 KB Output is correct
6 Correct 2 ms 4956 KB Output is correct
7 Correct 3 ms 4956 KB Output is correct
8 Correct 2 ms 4956 KB Output is correct
9 Correct 2 ms 4956 KB Output is correct
10 Correct 3 ms 4956 KB Output is correct
11 Correct 2 ms 4956 KB Output is correct
12 Correct 2 ms 4956 KB Output is correct
13 Partially correct 2 ms 4956 KB Partially correct
14 Correct 2 ms 4956 KB Output is correct
15 Partially correct 2 ms 4956 KB Partially correct
16 Partially correct 2 ms 4956 KB Partially correct
17 Partially correct 3 ms 4956 KB Partially correct
# Verdict Execution time Memory Grader output
1 Partially correct 3 ms 4952 KB Partially correct
2 Partially correct 2 ms 5000 KB Partially correct
3 Partially correct 2 ms 5212 KB Partially correct
4 Partially correct 2 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 5268 KB Output is correct
8 Partially correct 2 ms 5212 KB Partially correct
9 Partially correct 3 ms 5208 KB Partially correct
10 Partially correct 2 ms 5212 KB Partially correct
11 Partially correct 2 ms 5212 KB Partially correct
12 Correct 4 ms 5212 KB Output is correct
13 Correct 2 ms 5212 KB Output is correct
14 Partially correct 2 ms 5212 KB Partially correct
15 Partially correct 4 ms 5208 KB Partially correct
16 Partially correct 3 ms 5212 KB Partially correct
17 Partially correct 2 ms 5008 KB Partially correct
18 Partially correct 2 ms 5212 KB Partially correct
19 Partially correct 2 ms 5212 KB Partially correct
20 Partially correct 3 ms 5212 KB Partially correct
21 Partially correct 3 ms 5212 KB Partially correct
22 Partially correct 2 ms 5208 KB Partially correct
23 Partially correct 3 ms 5212 KB Partially correct
24 Partially correct 3 ms 5212 KB Partially correct
25 Partially correct 2 ms 5212 KB Partially correct
26 Partially correct 2 ms 5160 KB Partially correct
27 Partially correct 3 ms 5216 KB Partially correct
28 Partially correct 2 ms 5224 KB Partially correct
29 Partially correct 3 ms 5212 KB Partially correct
30 Partially correct 3 ms 5212 KB Partially correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4952 KB Output is correct
2 Correct 2 ms 4956 KB Output is correct
3 Correct 2 ms 5184 KB Output is correct
4 Correct 3 ms 4956 KB Output is correct
5 Correct 2 ms 4956 KB Output is correct
6 Correct 2 ms 4956 KB Output is correct
7 Correct 3 ms 4956 KB Output is correct
8 Correct 2 ms 4956 KB Output is correct
9 Correct 2 ms 4956 KB Output is correct
10 Correct 3 ms 4956 KB Output is correct
11 Correct 2 ms 4956 KB Output is correct
12 Correct 2 ms 4956 KB Output is correct
13 Partially correct 2 ms 4956 KB Partially correct
14 Correct 2 ms 4956 KB Output is correct
15 Partially correct 2 ms 4956 KB Partially correct
16 Partially correct 2 ms 4956 KB Partially correct
17 Partially correct 3 ms 4956 KB Partially correct
18 Partially correct 3 ms 4952 KB Partially correct
19 Partially correct 2 ms 5000 KB Partially correct
20 Partially correct 2 ms 5212 KB Partially correct
21 Partially correct 2 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 5268 KB Output is correct
25 Partially correct 2 ms 5212 KB Partially correct
26 Partially correct 3 ms 5208 KB Partially correct
27 Partially correct 2 ms 5212 KB Partially correct
28 Partially correct 2 ms 5212 KB Partially correct
29 Correct 4 ms 5212 KB Output is correct
30 Correct 2 ms 5212 KB Output is correct
31 Partially correct 2 ms 5212 KB Partially correct
32 Partially correct 4 ms 5208 KB Partially correct
33 Partially correct 3 ms 5212 KB Partially correct
34 Partially correct 2 ms 5008 KB Partially correct
35 Partially correct 2 ms 5212 KB Partially correct
36 Partially correct 2 ms 5212 KB Partially correct
37 Partially correct 3 ms 5212 KB Partially correct
38 Partially correct 3 ms 5212 KB Partially correct
39 Partially correct 2 ms 5208 KB Partially correct
40 Partially correct 3 ms 5212 KB Partially correct
41 Partially correct 3 ms 5212 KB Partially correct
42 Partially correct 2 ms 5212 KB Partially correct
43 Partially correct 2 ms 5160 KB Partially correct
44 Partially correct 3 ms 5216 KB Partially correct
45 Partially correct 2 ms 5224 KB Partially correct
46 Partially correct 3 ms 5212 KB Partially correct
47 Partially correct 3 ms 5212 KB Partially correct
48 Partially correct 71 ms 14660 KB Partially correct
49 Partially correct 67 ms 15424 KB Partially correct
50 Partially correct 59 ms 15500 KB Partially correct
51 Partially correct 46 ms 13208 KB Partially correct
52 Partially correct 58 ms 15180 KB Partially correct
53 Partially correct 49 ms 14444 KB Partially correct
54 Correct 34 ms 15296 KB Output is correct
55 Partially correct 70 ms 25920 KB Partially correct
56 Incorrect 69 ms 20288 KB Output isn't correct
57 Halted 0 ms 0 KB -