#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;
}
};
/*
#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});
Node* p = nullptr;
int size;
while(q.size())
{
tie(s,size) = q.back(); q.pop();
for(auto i : s->n) if (i != p) q.push({i,size+1});
p = s;
}
return {s,size};
}
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] = doMax(N,house2);
vector<int> destinations2(N+1,1);
int count2 = 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 |
Partially correct |
0 ms |
212 KB |
Partially correct |
2 |
Partially correct |
0 ms |
212 KB |
Partially correct |
3 |
Partially correct |
0 ms |
212 KB |
Partially correct |
4 |
Partially correct |
0 ms |
212 KB |
Partially correct |
5 |
Partially correct |
0 ms |
224 KB |
Partially correct |
6 |
Partially correct |
0 ms |
212 KB |
Partially correct |
7 |
Partially correct |
0 ms |
212 KB |
Partially correct |
8 |
Partially correct |
0 ms |
212 KB |
Partially correct |
9 |
Partially correct |
0 ms |
340 KB |
Partially correct |
10 |
Partially correct |
0 ms |
212 KB |
Partially correct |
11 |
Partially correct |
1 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 |
1 ms |
212 KB |
Partially correct |
15 |
Partially correct |
1 ms |
320 KB |
Partially correct |
16 |
Partially correct |
0 ms |
212 KB |
Partially correct |
17 |
Partially correct |
1 ms |
212 KB |
Partially correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Partially correct |
1 ms |
448 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 |
852 KB |
Partially correct |
8 |
Partially correct |
1 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 |
1 ms |
832 KB |
Partially correct |
17 |
Partially correct |
2 ms |
852 KB |
Partially correct |
18 |
Partially correct |
1 ms |
852 KB |
Partially correct |
19 |
Partially correct |
1 ms |
852 KB |
Partially correct |
20 |
Partially correct |
1 ms |
832 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 |
704 KB |
Partially correct |
25 |
Partially correct |
1 ms |
596 KB |
Partially correct |
26 |
Partially correct |
1 ms |
840 KB |
Partially correct |
27 |
Partially correct |
1 ms |
596 KB |
Partially correct |
28 |
Partially correct |
1 ms |
840 KB |
Partially correct |
29 |
Partially correct |
1 ms |
852 KB |
Partially correct |
30 |
Partially correct |
1 ms |
852 KB |
Partially correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Partially correct |
0 ms |
212 KB |
Partially correct |
2 |
Partially correct |
0 ms |
212 KB |
Partially correct |
3 |
Partially correct |
0 ms |
212 KB |
Partially correct |
4 |
Partially correct |
0 ms |
212 KB |
Partially correct |
5 |
Partially correct |
0 ms |
224 KB |
Partially correct |
6 |
Partially correct |
0 ms |
212 KB |
Partially correct |
7 |
Partially correct |
0 ms |
212 KB |
Partially correct |
8 |
Partially correct |
0 ms |
212 KB |
Partially correct |
9 |
Partially correct |
0 ms |
340 KB |
Partially correct |
10 |
Partially correct |
0 ms |
212 KB |
Partially correct |
11 |
Partially correct |
1 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 |
1 ms |
212 KB |
Partially correct |
15 |
Partially correct |
1 ms |
320 KB |
Partially correct |
16 |
Partially correct |
0 ms |
212 KB |
Partially correct |
17 |
Partially correct |
1 ms |
212 KB |
Partially correct |
18 |
Partially correct |
1 ms |
448 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 |
852 KB |
Partially correct |
25 |
Partially correct |
1 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 |
1 ms |
832 KB |
Partially correct |
34 |
Partially correct |
2 ms |
852 KB |
Partially correct |
35 |
Partially correct |
1 ms |
852 KB |
Partially correct |
36 |
Partially correct |
1 ms |
852 KB |
Partially correct |
37 |
Partially correct |
1 ms |
832 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 |
704 KB |
Partially correct |
42 |
Partially correct |
1 ms |
596 KB |
Partially correct |
43 |
Partially correct |
1 ms |
840 KB |
Partially correct |
44 |
Partially correct |
1 ms |
596 KB |
Partially correct |
45 |
Partially correct |
1 ms |
840 KB |
Partially correct |
46 |
Partially correct |
1 ms |
852 KB |
Partially correct |
47 |
Partially correct |
1 ms |
852 KB |
Partially correct |
48 |
Partially correct |
193 ms |
49896 KB |
Partially correct |
49 |
Partially correct |
222 ms |
54852 KB |
Partially correct |
50 |
Partially correct |
230 ms |
54812 KB |
Partially correct |
51 |
Partially correct |
167 ms |
42676 KB |
Partially correct |
52 |
Partially correct |
212 ms |
54120 KB |
Partially correct |
53 |
Partially correct |
227 ms |
48796 KB |
Partially correct |
54 |
Partially correct |
89 ms |
27400 KB |
Partially correct |
55 |
Partially correct |
197 ms |
54708 KB |
Partially correct |
56 |
Partially correct |
204 ms |
54600 KB |
Partially correct |
57 |
Partially correct |
227 ms |
53988 KB |
Partially correct |
58 |
Partially correct |
213 ms |
54724 KB |
Partially correct |
59 |
Partially correct |
221 ms |
54860 KB |
Partially correct |
60 |
Partially correct |
255 ms |
58260 KB |
Partially correct |
61 |
Partially correct |
243 ms |
56928 KB |
Partially correct |
62 |
Partially correct |
243 ms |
56992 KB |
Partially correct |
63 |
Partially correct |
227 ms |
53476 KB |
Partially correct |
64 |
Partially correct |
222 ms |
56464 KB |
Partially correct |
65 |
Partially correct |
264 ms |
57452 KB |
Partially correct |
66 |
Partially correct |
238 ms |
54004 KB |
Partially correct |
67 |
Partially correct |
170 ms |
42616 KB |
Partially correct |
68 |
Partially correct |
260 ms |
48476 KB |
Partially correct |
69 |
Partially correct |
263 ms |
57588 KB |
Partially correct |
70 |
Partially correct |
255 ms |
54056 KB |
Partially correct |
71 |
Partially correct |
160 ms |
40136 KB |
Partially correct |
72 |
Partially correct |
205 ms |
45464 KB |
Partially correct |
73 |
Partially correct |
263 ms |
57652 KB |
Partially correct |
74 |
Partially correct |
253 ms |
52724 KB |
Partially correct |
75 |
Partially correct |
205 ms |
54676 KB |
Partially correct |
76 |
Partially correct |
214 ms |
55428 KB |
Partially correct |
77 |
Partially correct |
192 ms |
52420 KB |
Partially correct |
78 |
Partially correct |
121 ms |
37052 KB |
Partially correct |
79 |
Partially correct |
182 ms |
43228 KB |
Partially correct |
80 |
Partially correct |
220 ms |
54748 KB |
Partially correct |
81 |
Partially correct |
261 ms |
53620 KB |
Partially correct |
82 |
Partially correct |
260 ms |
56156 KB |
Partially correct |