답안 #769348

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
769348 2023-06-29T12:41:19 Z canadavid1 Village (BOI20_village) C++14
50 / 100
177 ms 30464 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);
        exists = false;
    }
};

vector<Node> house;
/*
#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 ...);
}*/

int main()
{
    cin.tie(nullptr)->sync_with_stdio(false);
    int N;
    cin >> N;
    house.resize(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]);
    }
    if(N==2) {cout << "2 0\n2 1\n1 1\n";exit(0);}
    // 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);
            }
        }
    }
    cout << count << " 0\n";
    for(int i = 1; i <= N; i++) cout << destinations[i] << " "; cout << "\n";
    for(int i = 1; i <= N; i++) cout << "1 "; cout << "\n";
    

}

Compilation message

Village.cpp: In function 'int main()':
Village.cpp:125:5: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
  125 |     for(int i = 1; i <= N; i++) cout << destinations[i] << " "; cout << "\n";
      |     ^~~
Village.cpp:125:65: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
  125 |     for(int i = 1; i <= N; i++) cout << destinations[i] << " "; cout << "\n";
      |                                                                 ^~~~
Village.cpp:126:5: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
  126 |     for(int i = 1; i <= N; i++) cout << "1 "; cout << "\n";
      |     ^~~
Village.cpp:126:47: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
  126 |     for(int i = 1; i <= N; i++) cout << "1 "; cout << "\n";
      |                                               ^~~~
# 결과 실행 시간 메모리 Grader output
1 Partially correct 1 ms 212 KB Partially correct
2 Partially correct 1 ms 212 KB Partially correct
3 Partially correct 0 ms 212 KB Partially correct
4 Partially correct 0 ms 280 KB Partially correct
5 Partially correct 0 ms 212 KB Partially correct
6 Partially correct 1 ms 212 KB Partially correct
7 Partially correct 0 ms 212 KB Partially correct
8 Partially correct 1 ms 212 KB Partially correct
9 Partially correct 0 ms 212 KB Partially correct
10 Partially correct 0 ms 212 KB Partially correct
11 Partially correct 0 ms 212 KB Partially correct
12 Partially correct 1 ms 212 KB Partially correct
13 Partially correct 1 ms 212 KB Partially correct
14 Partially correct 0 ms 212 KB Partially correct
15 Partially correct 1 ms 212 KB Partially correct
16 Partially correct 0 ms 212 KB Partially correct
17 Partially correct 0 ms 212 KB Partially correct
# 결과 실행 시간 메모리 Grader output
1 Partially correct 1 ms 340 KB Partially correct
2 Partially correct 1 ms 468 KB Partially correct
3 Partially correct 1 ms 340 KB Partially correct
4 Partially correct 2 ms 596 KB Partially correct
5 Partially correct 1 ms 596 KB Partially correct
6 Partially correct 1 ms 596 KB Partially correct
7 Partially correct 1 ms 468 KB Partially correct
8 Partially correct 1 ms 596 KB Partially correct
9 Partially correct 1 ms 468 KB Partially correct
10 Partially correct 1 ms 596 KB Partially correct
11 Partially correct 1 ms 468 KB Partially correct
12 Partially correct 1 ms 596 KB Partially correct
13 Partially correct 1 ms 596 KB Partially correct
14 Partially correct 1 ms 596 KB Partially correct
15 Partially correct 1 ms 468 KB Partially correct
16 Partially correct 1 ms 596 KB Partially correct
17 Partially correct 1 ms 596 KB Partially correct
18 Partially correct 1 ms 596 KB Partially correct
19 Partially correct 1 ms 596 KB Partially correct
20 Partially correct 1 ms 596 KB Partially correct
21 Partially correct 1 ms 596 KB Partially correct
22 Partially correct 1 ms 596 KB Partially correct
23 Partially correct 1 ms 468 KB Partially correct
24 Partially correct 1 ms 468 KB Partially correct
25 Partially correct 1 ms 340 KB Partially correct
26 Partially correct 1 ms 468 KB Partially correct
27 Partially correct 1 ms 340 KB Partially correct
28 Partially correct 1 ms 596 KB Partially correct
29 Partially correct 1 ms 596 KB Partially correct
30 Partially correct 1 ms 596 KB Partially correct
# 결과 실행 시간 메모리 Grader output
1 Partially correct 1 ms 212 KB Partially correct
2 Partially correct 1 ms 212 KB Partially correct
3 Partially correct 0 ms 212 KB Partially correct
4 Partially correct 0 ms 280 KB Partially correct
5 Partially correct 0 ms 212 KB Partially correct
6 Partially correct 1 ms 212 KB Partially correct
7 Partially correct 0 ms 212 KB Partially correct
8 Partially correct 1 ms 212 KB Partially correct
9 Partially correct 0 ms 212 KB Partially correct
10 Partially correct 0 ms 212 KB Partially correct
11 Partially correct 0 ms 212 KB Partially correct
12 Partially correct 1 ms 212 KB Partially correct
13 Partially correct 1 ms 212 KB Partially correct
14 Partially correct 0 ms 212 KB Partially correct
15 Partially correct 1 ms 212 KB Partially correct
16 Partially correct 0 ms 212 KB Partially correct
17 Partially correct 0 ms 212 KB Partially correct
18 Partially correct 1 ms 340 KB Partially correct
19 Partially correct 1 ms 468 KB Partially correct
20 Partially correct 1 ms 340 KB Partially correct
21 Partially correct 2 ms 596 KB Partially correct
22 Partially correct 1 ms 596 KB Partially correct
23 Partially correct 1 ms 596 KB Partially correct
24 Partially correct 1 ms 468 KB Partially correct
25 Partially correct 1 ms 596 KB Partially correct
26 Partially correct 1 ms 468 KB Partially correct
27 Partially correct 1 ms 596 KB Partially correct
28 Partially correct 1 ms 468 KB Partially correct
29 Partially correct 1 ms 596 KB Partially correct
30 Partially correct 1 ms 596 KB Partially correct
31 Partially correct 1 ms 596 KB Partially correct
32 Partially correct 1 ms 468 KB Partially correct
33 Partially correct 1 ms 596 KB Partially correct
34 Partially correct 1 ms 596 KB Partially correct
35 Partially correct 1 ms 596 KB Partially correct
36 Partially correct 1 ms 596 KB Partially correct
37 Partially correct 1 ms 596 KB Partially correct
38 Partially correct 1 ms 596 KB Partially correct
39 Partially correct 1 ms 596 KB Partially correct
40 Partially correct 1 ms 468 KB Partially correct
41 Partially correct 1 ms 468 KB Partially correct
42 Partially correct 1 ms 340 KB Partially correct
43 Partially correct 1 ms 468 KB Partially correct
44 Partially correct 1 ms 340 KB Partially correct
45 Partially correct 1 ms 596 KB Partially correct
46 Partially correct 1 ms 596 KB Partially correct
47 Partially correct 1 ms 596 KB Partially correct
48 Partially correct 127 ms 25684 KB Partially correct
49 Partially correct 136 ms 28200 KB Partially correct
50 Partially correct 135 ms 28148 KB Partially correct
51 Partially correct 78 ms 21864 KB Partially correct
52 Partially correct 127 ms 27848 KB Partially correct
53 Partially correct 117 ms 25136 KB Partially correct
54 Partially correct 33 ms 13772 KB Partially correct
55 Partially correct 100 ms 27212 KB Partially correct
56 Partially correct 126 ms 27724 KB Partially correct
57 Partially correct 109 ms 27500 KB Partially correct
58 Partially correct 128 ms 27964 KB Partially correct
59 Partially correct 150 ms 28212 KB Partially correct
60 Partially correct 95 ms 30464 KB Partially correct
61 Partially correct 154 ms 29556 KB Partially correct
62 Partially correct 115 ms 29624 KB Partially correct
63 Partially correct 106 ms 27760 KB Partially correct
64 Partially correct 117 ms 29132 KB Partially correct
65 Partially correct 177 ms 29680 KB Partially correct
66 Partially correct 122 ms 28104 KB Partially correct
67 Partially correct 88 ms 22116 KB Partially correct
68 Partially correct 104 ms 25072 KB Partially correct
69 Partially correct 154 ms 29884 KB Partially correct
70 Partially correct 118 ms 28136 KB Partially correct
71 Partially correct 99 ms 20892 KB Partially correct
72 Partially correct 98 ms 23616 KB Partially correct
73 Partially correct 120 ms 29868 KB Partially correct
74 Partially correct 109 ms 27300 KB Partially correct
75 Partially correct 162 ms 28180 KB Partially correct
76 Partially correct 119 ms 28256 KB Partially correct
77 Partially correct 123 ms 27328 KB Partially correct
78 Partially correct 81 ms 19088 KB Partially correct
79 Partially correct 101 ms 22108 KB Partially correct
80 Partially correct 133 ms 28152 KB Partially correct
81 Partially correct 128 ms 28000 KB Partially correct
82 Partially correct 136 ms 28992 KB Partially correct