Skip to content

Commit d209670

Browse files
Add Codforces Solutions 1336A, 1352G
1 parent 911b3bc commit d209670

2 files changed

Lines changed: 115 additions & 0 deletions

File tree

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
#include <iostream>
2+
#include <vector>
3+
#include <algorithm>
4+
5+
using namespace std;
6+
7+
using ll = long long;
8+
9+
int main() {
10+
ios::sync_with_stdio(false);
11+
cin.tie(nullptr);
12+
13+
int n, k;
14+
cin >> n >> k;
15+
16+
vector<vector<int>> adj(n + 1);
17+
18+
for (int i = 0; i < n - 1; i++) {
19+
int u, v;
20+
cin >> u >> v;
21+
22+
adj[u].push_back(v);
23+
adj[v].push_back(u);
24+
}
25+
26+
vector<int> parent(n + 1, 0);
27+
vector<int> depth(n + 1, 0);
28+
vector<int> subtree(n + 1, 1);
29+
vector<int> order;
30+
31+
order.reserve(n);
32+
order.push_back(1);
33+
parent[1] = -1;
34+
35+
for (int i = 0; i < static_cast<int>(order.size()); i++) {
36+
int u = order[i];
37+
38+
for (int v : adj[u]) {
39+
if (v == parent[u]) {
40+
continue;
41+
}
42+
43+
parent[v] = u;
44+
depth[v] = depth[u] + 1;
45+
order.push_back(v);
46+
}
47+
}
48+
49+
for (int i = n - 1; i > 0; i--) {
50+
int u = order[i];
51+
subtree[parent[u]] += subtree[u];
52+
}
53+
54+
vector<ll> score;
55+
score.reserve(n);
56+
57+
for (int u = 1; u <= n; u++) {
58+
score.push_back(
59+
static_cast<ll>(depth[u]) - subtree[u] + 1
60+
);
61+
}
62+
63+
sort(score.begin(), score.end(), greater<ll>());
64+
65+
ll ans = 0;
66+
67+
for (int i = 0; i < k; i++) {
68+
ans += score[i];
69+
}
70+
71+
cout << ans << '\n';
72+
73+
return 0;
74+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#include <iostream>
2+
3+
using namespace std;
4+
5+
void solve() {
6+
int n;
7+
cin >> n;
8+
9+
if (n <= 3) {
10+
cout << -1 << '\n';
11+
return;
12+
}
13+
14+
for (int i = n; i >= 1; i--) {
15+
if (i % 2 == 1) {
16+
cout << i << ' ';
17+
}
18+
}
19+
20+
cout << "4 2 ";
21+
22+
for (int i = 6; i <= n; i += 2) {
23+
cout << i << ' ';
24+
}
25+
26+
cout << '\n';
27+
}
28+
29+
int main() {
30+
ios::sync_with_stdio(false);
31+
cin.tie(nullptr);
32+
33+
int t;
34+
cin >> t;
35+
36+
while (t--) {
37+
solve();
38+
}
39+
40+
return 0;
41+
}

0 commit comments

Comments
 (0)