# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
1090055 |
2024-09-17T16:28:40 Z |
raczek |
Village (BOI20_village) |
C++17 |
|
94 ms |
28044 KB |
#include<bits/stdc++.h>
using namespace std;
#ifdef DEBUG
auto& operator <<(auto& o, pair<auto, auto> p) {return o<<"{"<<p.first<<", "<<p.second<<"}";}
auto& operator <<(auto& o, auto x) {o<<"{"; for(auto v : x) o<<v<<", "; return o<<"}";}
#define debug(X) cerr<<"["#X"]: "<<X<<endl;
#else
#define debug(X) {}
#endif
#define int long long
vector<int> MinSolve(vector<vector<int> > graph)
{
int n = graph.size();
vector<int> result(n+1);
for(int i=0;i<n+1;i++) result[i] = i;
vector<int> par(n+1);
vector<int> deg(n+1);
queue<int> q;
function<void(int, int)> dfs = [&](int a, int fat)
{
par[a] = fat;
for(auto v : graph[a])
if(fat != v)
dfs(v, a);
deg[a] = graph[a].size();
if(a != 1 && graph[a].size() == 1) q.push(a);
};
dfs(1, -1);
while(!q.empty())
{
int a = q.front();
q.pop();
if(a == 1) continue;
if(result[a] == a)
swap(result[a], result[par[a]]), result[0]+=2;
deg[par[a]]--;
if(deg[par[a]] == 1) q.push(par[a]);
}
if(result[1] == 1)
{
result[0] += 2;
swap(result[graph[1][0]], result[1]);
}
return result;
}
vector<int> MaxSolve(vector<vector<int> > graph)
{
int n = graph.size()-1;
vector<int> Sz(n+1);
vector<int> result(n+1);
function<void(int, int)> getSz = [&](int a, int par)
{
Sz[a] = 1;
for(auto v : graph[a])
if(v != par)
{
getSz(v, a);
Sz[a] += Sz[v];
result[0] += Sz[v] * 2;
}
};
getSz(1, -1);
debug(Sz);
debug(n/2);
function<int(int, int)> getCtr = [&](int a, int par)
{
debug(a);
for(auto v : graph[a])
if(v != par)
if(Sz[v] > n/2)
return getCtr(v, a);
return a;
};
int ctr = getCtr(1, -1);
debug(ctr);
result[0] = 0;
getSz(ctr, -1);
vector<vector<int> > grp;
function<void(int, int)> dfs = [&](int a, int par)
{
grp.back().push_back(a);
for(auto v : graph[a])
if(v != par)
dfs(v, a);
};
for(auto v : graph[ctr])
{
grp.push_back({});
dfs(v, ctr);
}
// debug(grp);
grp.push_back({ctr});
sort(grp.begin(), grp.end(),[&](const vector<int>& v1,const vector<int>& v2){return v1.size()>v2.size();});
// debug(grp);
assert(grp[0].size() <= n/2);
// for(auto v : grp) cerr<<v.size()<<" ";
// cout<<endl;
vector<int> pot = grp[0];
for(int i=1;i<=n;i++) result[i] = i;
for(int i=0;i<grp.size()-1;i++)
{
for(int j=i+1;j<grp.size() && grp[i].size() > 0;j++)
{
while(grp[i].size() > 0 && grp[j].size() > 0)
{
result[grp[i].back()] = grp[j].back();
result[grp[j].back()] = grp[i].back();
grp[i].pop_back();
grp[j].pop_back();
}
}
// cout<<grp[i].size()<<" ";
}
int x = 0;
for(int i=0;i<grp.size();i++) if(grp[i].size() > 0) x = i;
for(auto v : grp[x])
{
swap(result[v], result[pot.back()]);
pot.pop_back();
}
for(int i=1;i<=n;i++) assert(result[i] != i);
return result;
}
int32_t main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n;
cin>>n;
vector<vector<int> > graph(n+1);
for(int i=0;i<n-1;i++)
{
int a, b;
cin>>a>>b;
graph[a].push_back(b);
graph[b].push_back(a);
}
vector<int> minn = MinSolve(graph);
vector<int> maxx = MaxSolve(graph);
cout<<minn[0]<<" "<<maxx[0]<<"\n";
for(int i=1;i<=n;i++) cout<<minn[i]<<" ";
cout<<"\n";
for(int i=1;i<=n;i++) cout<<maxx[i]<<" ";
}
Compilation message
In file included from /usr/include/c++/10/cassert:44,
from /usr/include/x86_64-linux-gnu/c++/10/bits/stdc++.h:33,
from Village.cpp:1:
Village.cpp: In function 'std::vector<long long int> MaxSolve(std::vector<std::vector<long long int> >)':
Village.cpp:95:23: warning: comparison of integer expressions of different signedness: 'std::vector<long long int>::size_type' {aka 'long unsigned int'} and 'long long int' [-Wsign-compare]
95 | assert(grp[0].size() <= n/2);
| ~~~~~~~~~~~~~~^~~~~~
Village.cpp:100:15: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::vector<long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
100 | for(int i=0;i<grp.size()-1;i++)
| ~^~~~~~~~~~~~~
Village.cpp:102:18: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::vector<long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
102 | for(int j=i+1;j<grp.size() && grp[i].size() > 0;j++)
| ~^~~~~~~~~~~
Village.cpp:115:15: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::vector<long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
115 | for(int i=0;i<grp.size();i++) if(grp[i].size() > 0) x = i;
| ~^~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
352 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
352 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
352 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
352 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
452 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 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 |
604 KB |
Output is correct |
5 |
Correct |
1 ms |
604 KB |
Output is correct |
6 |
Correct |
1 ms |
604 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 |
600 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
1 ms |
604 KB |
Output is correct |
12 |
Correct |
1 ms |
624 KB |
Output is correct |
13 |
Correct |
1 ms |
604 KB |
Output is correct |
14 |
Correct |
1 ms |
604 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 |
856 KB |
Output is correct |
19 |
Correct |
1 ms |
600 KB |
Output is correct |
20 |
Correct |
1 ms |
600 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 |
604 KB |
Output is correct |
24 |
Correct |
1 ms |
604 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
604 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
604 KB |
Output is correct |
29 |
Correct |
1 ms |
460 KB |
Output is correct |
30 |
Correct |
1 ms |
604 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
352 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
352 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
352 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
352 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
452 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 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 |
604 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 |
600 KB |
Output is correct |
27 |
Correct |
1 ms |
604 KB |
Output is correct |
28 |
Correct |
1 ms |
604 KB |
Output is correct |
29 |
Correct |
1 ms |
624 KB |
Output is correct |
30 |
Correct |
1 ms |
604 KB |
Output is correct |
31 |
Correct |
1 ms |
604 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 |
856 KB |
Output is correct |
36 |
Correct |
1 ms |
600 KB |
Output is correct |
37 |
Correct |
1 ms |
600 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 |
604 KB |
Output is correct |
41 |
Correct |
1 ms |
604 KB |
Output is correct |
42 |
Correct |
1 ms |
348 KB |
Output is correct |
43 |
Correct |
1 ms |
604 KB |
Output is correct |
44 |
Correct |
0 ms |
348 KB |
Output is correct |
45 |
Correct |
1 ms |
604 KB |
Output is correct |
46 |
Correct |
1 ms |
460 KB |
Output is correct |
47 |
Correct |
1 ms |
604 KB |
Output is correct |
48 |
Correct |
60 ms |
16644 KB |
Output is correct |
49 |
Correct |
61 ms |
18424 KB |
Output is correct |
50 |
Correct |
73 ms |
18528 KB |
Output is correct |
51 |
Correct |
47 ms |
14252 KB |
Output is correct |
52 |
Correct |
61 ms |
17968 KB |
Output is correct |
53 |
Correct |
54 ms |
16356 KB |
Output is correct |
54 |
Correct |
36 ms |
13716 KB |
Output is correct |
55 |
Correct |
94 ms |
28044 KB |
Output is correct |
56 |
Correct |
75 ms |
23148 KB |
Output is correct |
57 |
Correct |
71 ms |
20992 KB |
Output is correct |
58 |
Correct |
81 ms |
19816 KB |
Output is correct |
59 |
Correct |
62 ms |
18508 KB |
Output is correct |
60 |
Correct |
63 ms |
22284 KB |
Output is correct |
61 |
Correct |
61 ms |
19632 KB |
Output is correct |
62 |
Correct |
53 ms |
19204 KB |
Output is correct |
63 |
Correct |
49 ms |
18044 KB |
Output is correct |
64 |
Correct |
58 ms |
19068 KB |
Output is correct |
65 |
Correct |
53 ms |
19280 KB |
Output is correct |
66 |
Correct |
50 ms |
17916 KB |
Output is correct |
67 |
Correct |
39 ms |
14860 KB |
Output is correct |
68 |
Correct |
54 ms |
16188 KB |
Output is correct |
69 |
Correct |
56 ms |
19552 KB |
Output is correct |
70 |
Correct |
53 ms |
18012 KB |
Output is correct |
71 |
Correct |
35 ms |
13580 KB |
Output is correct |
72 |
Correct |
45 ms |
15184 KB |
Output is correct |
73 |
Correct |
53 ms |
19536 KB |
Output is correct |
74 |
Correct |
48 ms |
17756 KB |
Output is correct |
75 |
Correct |
77 ms |
18604 KB |
Output is correct |
76 |
Correct |
58 ms |
18440 KB |
Output is correct |
77 |
Correct |
55 ms |
18348 KB |
Output is correct |
78 |
Correct |
36 ms |
12672 KB |
Output is correct |
79 |
Correct |
45 ms |
14588 KB |
Output is correct |
80 |
Correct |
63 ms |
18128 KB |
Output is correct |
81 |
Correct |
53 ms |
18172 KB |
Output is correct |
82 |
Correct |
57 ms |
18452 KB |
Output is correct |