Submission #1094663

# Submission time Handle Problem Language Result Execution time Memory
1094663 2024-09-30T08:58:30 Z andrei_iorgulescu Village (BOI20_village) C++14
25 / 100
88 ms 24884 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 / 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();
    ans_mic = 0;
    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 Partially correct 2 ms 4956 KB Partially correct
2 Partially correct 2 ms 4956 KB Partially correct
3 Partially correct 2 ms 4956 KB Partially correct
4 Partially correct 2 ms 4952 KB Partially correct
5 Partially correct 2 ms 4956 KB Partially correct
6 Partially correct 2 ms 4952 KB Partially correct
7 Partially correct 2 ms 4956 KB Partially correct
8 Partially correct 3 ms 4952 KB Partially correct
9 Partially correct 3 ms 4956 KB Partially correct
10 Partially correct 3 ms 5188 KB Partially correct
11 Partially correct 2 ms 4968 KB Partially correct
12 Partially correct 3 ms 5116 KB Partially correct
13 Partially correct 2 ms 4968 KB Partially correct
14 Partially correct 2 ms 4968 KB Partially correct
15 Partially correct 2 ms 4968 KB Partially correct
16 Partially correct 2 ms 4968 KB Partially correct
17 Partially correct 2 ms 4968 KB Partially correct
# Verdict Execution time Memory Grader output
1 Partially correct 2 ms 4968 KB Partially correct
2 Partially correct 2 ms 5476 KB Partially correct
3 Partially correct 2 ms 5224 KB Partially correct
4 Partially correct 4 ms 5224 KB Partially correct
5 Partially correct 3 ms 5224 KB Partially correct
6 Partially correct 3 ms 5224 KB Partially correct
7 Partially correct 3 ms 5224 KB Partially correct
8 Partially correct 2 ms 5224 KB Partially correct
9 Partially correct 3 ms 5224 KB Partially correct
10 Partially correct 4 ms 5224 KB Partially correct
11 Partially correct 3 ms 5224 KB Partially correct
12 Partially correct 4 ms 5224 KB Partially correct
13 Partially correct 3 ms 5224 KB Partially correct
14 Partially correct 4 ms 5224 KB Partially correct
15 Partially correct 4 ms 5208 KB Partially correct
16 Partially correct 3 ms 5212 KB Partially correct
17 Partially correct 3 ms 5212 KB Partially correct
18 Partially correct 3 ms 5212 KB Partially correct
19 Partially correct 4 ms 5212 KB Partially correct
20 Partially correct 2 ms 5212 KB Partially correct
21 Partially correct 3 ms 5208 KB Partially correct
22 Partially correct 3 ms 5212 KB Partially correct
23 Partially correct 3 ms 5212 KB Partially correct
24 Partially correct 2 ms 5212 KB Partially correct
25 Partially correct 2 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 2 ms 5212 KB Partially correct
29 Partially correct 4 ms 5208 KB Partially correct
30 Partially correct 3 ms 5212 KB Partially correct
# Verdict Execution time Memory Grader output
1 Partially correct 2 ms 4956 KB Partially correct
2 Partially correct 2 ms 4956 KB Partially correct
3 Partially correct 2 ms 4956 KB Partially correct
4 Partially correct 2 ms 4952 KB Partially correct
5 Partially correct 2 ms 4956 KB Partially correct
6 Partially correct 2 ms 4952 KB Partially correct
7 Partially correct 2 ms 4956 KB Partially correct
8 Partially correct 3 ms 4952 KB Partially correct
9 Partially correct 3 ms 4956 KB Partially correct
10 Partially correct 3 ms 5188 KB Partially correct
11 Partially correct 2 ms 4968 KB Partially correct
12 Partially correct 3 ms 5116 KB Partially correct
13 Partially correct 2 ms 4968 KB Partially correct
14 Partially correct 2 ms 4968 KB Partially correct
15 Partially correct 2 ms 4968 KB Partially correct
16 Partially correct 2 ms 4968 KB Partially correct
17 Partially correct 2 ms 4968 KB Partially correct
18 Partially correct 2 ms 4968 KB Partially correct
19 Partially correct 2 ms 5476 KB Partially correct
20 Partially correct 2 ms 5224 KB Partially correct
21 Partially correct 4 ms 5224 KB Partially correct
22 Partially correct 3 ms 5224 KB Partially correct
23 Partially correct 3 ms 5224 KB Partially correct
24 Partially correct 3 ms 5224 KB Partially correct
25 Partially correct 2 ms 5224 KB Partially correct
26 Partially correct 3 ms 5224 KB Partially correct
27 Partially correct 4 ms 5224 KB Partially correct
28 Partially correct 3 ms 5224 KB Partially correct
29 Partially correct 4 ms 5224 KB Partially correct
30 Partially correct 3 ms 5224 KB Partially correct
31 Partially correct 4 ms 5224 KB Partially correct
32 Partially correct 4 ms 5208 KB Partially correct
33 Partially correct 3 ms 5212 KB Partially correct
34 Partially correct 3 ms 5212 KB Partially correct
35 Partially correct 3 ms 5212 KB Partially correct
36 Partially correct 4 ms 5212 KB Partially correct
37 Partially correct 2 ms 5212 KB Partially correct
38 Partially correct 3 ms 5208 KB Partially correct
39 Partially correct 3 ms 5212 KB Partially correct
40 Partially correct 3 ms 5212 KB Partially correct
41 Partially correct 2 ms 5212 KB Partially correct
42 Partially correct 2 ms 5212 KB Partially correct
43 Partially correct 3 ms 5212 KB Partially correct
44 Partially correct 2 ms 5212 KB Partially correct
45 Partially correct 2 ms 5212 KB Partially correct
46 Partially correct 4 ms 5208 KB Partially correct
47 Partially correct 3 ms 5212 KB Partially correct
48 Partially correct 59 ms 13420 KB Partially correct
49 Partially correct 67 ms 14132 KB Partially correct
50 Partially correct 62 ms 14164 KB Partially correct
51 Partially correct 53 ms 12260 KB Partially correct
52 Partially correct 88 ms 14148 KB Partially correct
53 Partially correct 47 ms 13420 KB Partially correct
54 Partially correct 32 ms 14604 KB Partially correct
55 Incorrect 69 ms 24884 KB Output isn't correct
56 Halted 0 ms 0 KB -