Submission #964336

# Submission time Handle Problem Language Result Execution time Memory
964336 2024-04-16T16:29:21 Z codefox Village (BOI20_village) C++14
100 / 100
72 ms 24028 KB
#include<bits/stdc++.h>

using namespace std;

#pragma GCC optimize("Ofast")

#define int long long

vector<vector<int>> graph;
vector<int> subsize;
vector<int> subtree;
int cent = -1;
int ds = 0;
int n;

vector<int> match;
vector<int> matched;
int dds = 0;

void dp(int i, int p)
{
    vector<int> nodes;
    for (int ele:graph[i])
    {
        if (ele == p) continue;
        dp(ele, i);
        if (matched[ele]==0) nodes.push_back(ele);
    }
    if (nodes.size()==0) return;
    dds += nodes.size()*2;
    match[i] = nodes[0];
    for (int j = 0; j < nodes.size()-1; j++)
    {
        match[nodes[j]] = nodes[j+1];
    }
    match[nodes.back()] = i;
    matched[i] = 1;
}

void dfs(int i, int p)
{
    int sum = 0;
    bool b = 1;
    for (int ele:graph[i])
    {
        if (ele == p) continue;
        dfs(ele, i);
        if (subsize[ele]*2>n) b = 0;
        subsize[i] += subsize[ele];
        sum += subsize[ele];
    }
    sum = n-sum-1;
    if (sum*2>n) b=0;
    if (b) cent = i;
    subsize[i]++;
}

void dist(int i, int p, int d)
{
    for (int ele:graph[i])
    {
        if (ele == p) continue;
        dist(ele, i, d+1);
    }
    ds += d;
}

void sub(int i, int p)
{
    subtree.push_back(i);
    for (int ele:graph[i])
    {
        if (ele == p) continue;
        sub(ele, i);
    }
}

bool comp(vector<int> &a, vector<int> &b)
{
    if (a.size() > b.size()) return 1;
    else return 0;
}

int32_t main()
{
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);

    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    cin >> n;

    graph.assign(n, vector<int>());
    subsize.assign(n, 0);
    match.assign(n, 0);
    matched.assign(n, 0);

    for (int i = 1; i < n; i++)
    {
        int a, b;
        cin >> a >> b;
        a--; b--;
        graph[a].push_back(b);
        graph[b].push_back(a);
    }
    dfs(0, -1);
    dp(0, -1);
    if (!matched[0])
    {
        dds+=2;
        int ele = graph[0].back();
        match[0] = match[ele];
        match[ele] = 0;
    }

    int mx = -1;

    dist(cent, -1, 0);
    mx = ds*2;

    vector<vector<int>> subs;
    for (int i = 0; i < graph[cent].size(); i++)
    {
        sub(graph[cent][i], cent);
        subs.push_back(subtree);
        subtree.clear();
    }
    subs.push_back(vector<int>(1, cent));
    sort(subs.begin(), subs.end(), comp);

    cout << dds << " " << mx << endl;

    vector<int> left;
    vector<int> ini;
    vector<int> part(n, 0);
    for (auto ele:subs)
    {
        for (int e:ele) 
        {
            ini.push_back(e);
            left.push_back(e);
        }
    }
    for (int i = 0; i < subs[0].size(); i++)
    {
        part[ini[i]] = left.back();
        left.pop_back();
    }
    int i = subs[0].size();
    for (int ele:left) 
    {
        part[ini[i]] = ele;
        i++;
    }
    for (int i = 0; i <n; i++) cout << match[i]+1 << " ";
    cout << endl;
    for (int ele:part) cout << ele+1 << " ";
}

Compilation message

Village.cpp: In function 'void dp(long long int, long long int)':
Village.cpp:32: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]
   32 |     for (int j = 0; j < nodes.size()-1; j++)
      |                     ~~^~~~~~~~~~~~~~~~
Village.cpp: In function 'int32_t main()':
Village.cpp:124: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]
  124 |     for (int i = 0; i < graph[cent].size(); i++)
      |                     ~~^~~~~~~~~~~~~~~~~~~~
Village.cpp:146: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]
  146 |     for (int i = 0; i < subs[0].size(); i++)
      |                     ~~^~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 344 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 344 KB Output is correct
14 Correct 0 ms 344 KB Output is correct
15 Correct 0 ms 452 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 1 ms 604 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 456 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 ms 344 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 604 KB Output is correct
8 Correct 1 ms 604 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 600 KB Output is correct
11 Correct 1 ms 604 KB Output is correct
12 Correct 1 ms 600 KB Output is correct
13 Correct 1 ms 604 KB Output is correct
14 Correct 1 ms 456 KB Output is correct
15 Correct 1 ms 604 KB Output is correct
16 Correct 1 ms 604 KB Output is correct
17 Correct 1 ms 604 KB Output is correct
18 Correct 1 ms 604 KB Output is correct
19 Correct 1 ms 600 KB Output is correct
20 Correct 1 ms 604 KB Output is correct
21 Correct 1 ms 604 KB Output is correct
22 Correct 1 ms 604 KB Output is correct
23 Correct 1 ms 348 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 1 ms 348 KB Output is correct
26 Correct 1 ms 600 KB Output is correct
27 Correct 1 ms 348 KB Output is correct
28 Correct 1 ms 600 KB Output is correct
29 Correct 1 ms 604 KB Output is correct
30 Correct 1 ms 604 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 344 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 344 KB Output is correct
14 Correct 0 ms 344 KB Output is correct
15 Correct 0 ms 452 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 1 ms 604 KB Output is correct
18 Correct 1 ms 456 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 1 ms 344 KB Output is correct
22 Correct 1 ms 348 KB Output is correct
23 Correct 1 ms 348 KB Output is correct
24 Correct 1 ms 604 KB Output is correct
25 Correct 1 ms 604 KB Output is correct
26 Correct 1 ms 348 KB Output is correct
27 Correct 1 ms 600 KB Output is correct
28 Correct 1 ms 604 KB Output is correct
29 Correct 1 ms 600 KB Output is correct
30 Correct 1 ms 604 KB Output is correct
31 Correct 1 ms 456 KB Output is correct
32 Correct 1 ms 604 KB Output is correct
33 Correct 1 ms 604 KB Output is correct
34 Correct 1 ms 604 KB Output is correct
35 Correct 1 ms 604 KB Output is correct
36 Correct 1 ms 600 KB Output is correct
37 Correct 1 ms 604 KB Output is correct
38 Correct 1 ms 604 KB Output is correct
39 Correct 1 ms 604 KB Output is correct
40 Correct 1 ms 348 KB Output is correct
41 Correct 1 ms 348 KB Output is correct
42 Correct 1 ms 348 KB Output is correct
43 Correct 1 ms 600 KB Output is correct
44 Correct 1 ms 348 KB Output is correct
45 Correct 1 ms 600 KB Output is correct
46 Correct 1 ms 604 KB Output is correct
47 Correct 1 ms 604 KB Output is correct
48 Correct 46 ms 14200 KB Output is correct
49 Correct 62 ms 15360 KB Output is correct
50 Correct 53 ms 15516 KB Output is correct
51 Correct 41 ms 11768 KB Output is correct
52 Correct 52 ms 14652 KB Output is correct
53 Correct 58 ms 14008 KB Output is correct
54 Correct 31 ms 11660 KB Output is correct
55 Correct 68 ms 24028 KB Output is correct
56 Correct 63 ms 19808 KB Output is correct
57 Correct 72 ms 18196 KB Output is correct
58 Correct 57 ms 16956 KB Output is correct
59 Correct 52 ms 15288 KB Output is correct
60 Correct 64 ms 20364 KB Output is correct
61 Correct 45 ms 16716 KB Output is correct
62 Correct 46 ms 15400 KB Output is correct
63 Correct 53 ms 14472 KB Output is correct
64 Correct 48 ms 15140 KB Output is correct
65 Correct 40 ms 15448 KB Output is correct
66 Correct 45 ms 14420 KB Output is correct
67 Correct 33 ms 12552 KB Output is correct
68 Correct 35 ms 12900 KB Output is correct
69 Correct 42 ms 15476 KB Output is correct
70 Correct 41 ms 14284 KB Output is correct
71 Correct 27 ms 10892 KB Output is correct
72 Correct 36 ms 12160 KB Output is correct
73 Correct 50 ms 15960 KB Output is correct
74 Correct 37 ms 14004 KB Output is correct
75 Correct 59 ms 15812 KB Output is correct
76 Correct 49 ms 15236 KB Output is correct
77 Correct 54 ms 14856 KB Output is correct
78 Correct 32 ms 10268 KB Output is correct
79 Correct 37 ms 11748 KB Output is correct
80 Correct 56 ms 15804 KB Output is correct
81 Correct 43 ms 14656 KB Output is correct
82 Correct 43 ms 14616 KB Output is correct