Submission #770169

# Submission time Handle Problem Language Result Execution time Memory
770169 2023-06-30T21:47:59 Z canadavid1 Village (BOI20_village) C++17
56 / 100
279 ms 57236 KB
#include <bits/stdc++.h>
using namespace std;

struct Node
{
    unordered_set<Node*> n;
    vector<int> leaf;
    int idx;
    bool exists = true;
    void remove()
    {
        for(auto i : n) i->n.erase(this);
        n.clear();
        exists = false;
    }
};

/*
#define BLOCKSIZE 1024
template<typename T,typename... Args>
T* newT(Args... args)
{
    static T* arr = nullptr;
    static int idx = 0;
    if(idx == BLOCKSIZE)
    {
        arr = new T[BLOCKSIZE];
        idx = 0;
    }
    T* out = &arr[idx];
    out->~T();
    idx++;
    return new(out) T(args ...);
}*/


pair<vector<int>,int> doMin(int N, vector<Node>& house)
{

    if(N==2) return {{0,2,1},2};
    
    // prune leaves
    vector<Node*> leaf;
    for(int i = 1; i <= N; i++)
    {
        house[i].idx = i;
        if(house[i].n.size()>1) continue;
        leaf.push_back(&house[i]);        
    }
    for(auto i : leaf)
    {

        (*begin(i->n))->n.erase(i);
        (*begin(i->n))->leaf.push_back(i->idx);
        i->exists = false;
    }
    vector<Node*> semileaves;
    for(int i = 1; i <= N; i++)
    {
        if (!house[i].exists) continue;
        if (house[i].n.size()<=1) semileaves.push_back(&house[i]);
    }
    vector<int> destinations(N+1);
    int count=0;
    while(semileaves.size())
    {
        auto n = semileaves.back();
        semileaves.pop_back();
        //cerr << "remove semileaf " << n->idx << " with " << n->leaf.size() << " leaves\n";
        while(n->leaf.size()>2)
        {
            auto l1 = n->leaf.back(); n->leaf.pop_back();
            auto l2 = n->leaf.back(); n->leaf.pop_back();
            destinations[l1] = l2;
            destinations[l2] = l1;
            count += 4;
        }
        if(n->leaf.size()==2)
        {
            auto l1 = n->leaf.back(); n->leaf.pop_back();
            auto l2 = n->leaf.back(); n->leaf.pop_back();
            destinations[n->idx] = l1;
            destinations[l1] = l2;
            destinations[l2] = n->idx;
            count += 4;
        }
        else
        { // n->leaf.size() == 1
            auto l1 = n->leaf.back(); n->leaf.pop_back();
            destinations[n->idx] = l1;
            destinations[l1] = n->idx;
            count += 2;
        }
        if (n->n.size()==0) continue;
        auto p = *begin(n->n);
        p->n.erase(n);
        if(p->n.size()==1)
        {
            if(p->leaf.size())
            {
                // make semileaf
                semileaves.push_back(p);
                // done :-)
            }
            else
            {
                // make leaf
                auto p2 = *begin(p->n);
                p->remove();
                p2->leaf.push_back(p->idx);
                if (p2->n.size()==1) semileaves.push_back(p2);
            }
        }
    }
    return {destinations,count};
}


pair<Node*,int> findFurthestNode(Node* s)
{
    queue<pair<Node*,int>> q; q.push({s,0});
    unordered_set<Node*> v;
    pair<Node*,int> s2;
    int size;
    while(q.size())
    {
        tie(s,size) = q.front(); q.pop();
        if(v.count(s)) continue;
        s2 = {s,size};
        v.insert(s);
        for(auto i : s->n) q.push({i,size+1});
    }
    return s2;
}

pair<vector<int>,int> doMax(int N, vector<Node>& house)
{
    if(N==2) return {{0,2,1},2};
    for(int i = 1; i <= N; i++) house[i].idx = i;
    int size = N;
    Node *aHouse = &house[1];
    vector<int> destination(N+1);
    int count = 0;
    while(size > 3)
    {
        auto[s,ignore] = findFurthestNode(aHouse);
        auto[t,d] = findFurthestNode(s);
        destination[s->idx] = t->idx;
        destination[t->idx] = s->idx;
        count += 2*d;
        aHouse = *begin(t->n);
        s->remove();
        t->remove();
        size -= 2;
    }
    if(size==3)
    {
        if(aHouse->n.size()==1) aHouse = *begin(aHouse->n);
        vector<int> h;
        for(auto i : aHouse->n) h.push_back(i->idx);
        destination[h[0]] = h[1];
        destination[h[1]] = aHouse->idx;
        destination[aHouse->idx] = h[0];
        count+=4;
        
    }
    else // size==2
    {
        Node *house2 = *begin(aHouse->n);
        destination[aHouse->idx] = house2->idx;
        destination[house2->idx] = aHouse->idx;
        count+=2;
    }
    return {destination,count};
}


int main()
{
    cin.tie(nullptr)->sync_with_stdio(false);
    int N;
    cin >> N;
    vector<Node> house(N+1);
    vector<Node> house2(N+1);
    for(int i = 1; i < N;i++)
    {
        int a,b;
        cin >> a >> b;
        house[a].n.insert(&house[b]);
        house[b].n.insert(&house[a]);
        house2[a].n.insert(&house2[b]);
        house2[b].n.insert(&house2[a]);
    }
    auto[destinations,count] = doMin(N,house);
    auto[destinations2,count2] = N <= 100 ? doMax(N,house2) : pair<vector<int>,int>(vector<int>(N+1,1),0);
    cout << count << " " << count2 << "\n";
    for(int i = 1; i <= N; i++) cout << destinations[i] << " "; 
    cout << "\n";
    for(int i = 1; i <= N; i++) cout << destinations2[i] << " "; 
    cout << "\n";
    

}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Partially correct 1 ms 340 KB Partially correct
2 Partially correct 1 ms 596 KB Partially correct
3 Partially correct 1 ms 468 KB Partially correct
4 Partially correct 1 ms 852 KB Partially correct
5 Partially correct 1 ms 852 KB Partially correct
6 Partially correct 1 ms 852 KB Partially correct
7 Partially correct 1 ms 828 KB Partially correct
8 Partially correct 2 ms 852 KB Partially correct
9 Partially correct 1 ms 724 KB Partially correct
10 Partially correct 1 ms 852 KB Partially correct
11 Partially correct 1 ms 852 KB Partially correct
12 Partially correct 1 ms 852 KB Partially correct
13 Partially correct 1 ms 852 KB Partially correct
14 Partially correct 1 ms 852 KB Partially correct
15 Partially correct 1 ms 724 KB Partially correct
16 Partially correct 2 ms 852 KB Partially correct
17 Partially correct 1 ms 852 KB Partially correct
18 Partially correct 2 ms 852 KB Partially correct
19 Partially correct 1 ms 852 KB Partially correct
20 Partially correct 1 ms 852 KB Partially correct
21 Partially correct 1 ms 852 KB Partially correct
22 Partially correct 1 ms 852 KB Partially correct
23 Partially correct 1 ms 724 KB Partially correct
24 Partially correct 1 ms 724 KB Partially correct
25 Partially correct 1 ms 596 KB Partially correct
26 Partially correct 1 ms 852 KB Partially correct
27 Partially correct 1 ms 596 KB Partially correct
28 Partially correct 2 ms 852 KB Partially correct
29 Partially correct 1 ms 852 KB Partially correct
30 Partially correct 2 ms 852 KB Partially correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Partially correct 1 ms 340 KB Partially correct
19 Partially correct 1 ms 596 KB Partially correct
20 Partially correct 1 ms 468 KB Partially correct
21 Partially correct 1 ms 852 KB Partially correct
22 Partially correct 1 ms 852 KB Partially correct
23 Partially correct 1 ms 852 KB Partially correct
24 Partially correct 1 ms 828 KB Partially correct
25 Partially correct 2 ms 852 KB Partially correct
26 Partially correct 1 ms 724 KB Partially correct
27 Partially correct 1 ms 852 KB Partially correct
28 Partially correct 1 ms 852 KB Partially correct
29 Partially correct 1 ms 852 KB Partially correct
30 Partially correct 1 ms 852 KB Partially correct
31 Partially correct 1 ms 852 KB Partially correct
32 Partially correct 1 ms 724 KB Partially correct
33 Partially correct 2 ms 852 KB Partially correct
34 Partially correct 1 ms 852 KB Partially correct
35 Partially correct 2 ms 852 KB Partially correct
36 Partially correct 1 ms 852 KB Partially correct
37 Partially correct 1 ms 852 KB Partially correct
38 Partially correct 1 ms 852 KB Partially correct
39 Partially correct 1 ms 852 KB Partially correct
40 Partially correct 1 ms 724 KB Partially correct
41 Partially correct 1 ms 724 KB Partially correct
42 Partially correct 1 ms 596 KB Partially correct
43 Partially correct 1 ms 852 KB Partially correct
44 Partially correct 1 ms 596 KB Partially correct
45 Partially correct 2 ms 852 KB Partially correct
46 Partially correct 1 ms 852 KB Partially correct
47 Partially correct 2 ms 852 KB Partially correct
48 Partially correct 222 ms 48848 KB Partially correct
49 Partially correct 229 ms 53684 KB Partially correct
50 Partially correct 227 ms 53632 KB Partially correct
51 Partially correct 170 ms 41744 KB Partially correct
52 Partially correct 231 ms 53000 KB Partially correct
53 Partially correct 199 ms 47728 KB Partially correct
54 Partially correct 106 ms 26872 KB Partially correct
55 Partially correct 219 ms 53540 KB Partially correct
56 Partially correct 244 ms 53552 KB Partially correct
57 Partially correct 231 ms 52900 KB Partially correct
58 Partially correct 234 ms 53560 KB Partially correct
59 Partially correct 233 ms 53704 KB Partially correct
60 Partially correct 240 ms 57236 KB Partially correct
61 Partially correct 250 ms 55764 KB Partially correct
62 Partially correct 267 ms 55884 KB Partially correct
63 Partially correct 270 ms 52468 KB Partially correct
64 Partially correct 235 ms 55300 KB Partially correct
65 Partially correct 278 ms 56300 KB Partially correct
66 Partially correct 248 ms 52832 KB Partially correct
67 Partially correct 175 ms 41724 KB Partially correct
68 Partially correct 234 ms 47504 KB Partially correct
69 Partially correct 279 ms 56548 KB Partially correct
70 Partially correct 251 ms 53036 KB Partially correct
71 Partially correct 169 ms 39340 KB Partially correct
72 Partially correct 210 ms 44596 KB Partially correct
73 Partially correct 269 ms 56492 KB Partially correct
74 Partially correct 237 ms 51596 KB Partially correct
75 Partially correct 245 ms 53624 KB Partially correct
76 Partially correct 225 ms 54384 KB Partially correct
77 Partially correct 203 ms 51436 KB Partially correct
78 Partially correct 144 ms 36268 KB Partially correct
79 Partially correct 183 ms 42412 KB Partially correct
80 Partially correct 224 ms 53604 KB Partially correct
81 Partially correct 261 ms 52608 KB Partially correct
82 Partially correct 265 ms 54980 KB Partially correct