Skip to content

Commit

Permalink
2533: solve in C++17
Browse files Browse the repository at this point in the history
  • Loading branch information
yous committed Sep 23, 2021
1 parent 9dc3e89 commit b3a60e7
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 0 deletions.
8 changes: 8 additions & 0 deletions problem/2533/input
@@ -0,0 +1,8 @@
8
1 2
1 3
1 4
2 5
2 6
4 7
4 8
9 changes: 9 additions & 0 deletions problem/2533/input2
@@ -0,0 +1,9 @@
9
1 2
1 3
2 4
3 5
3 6
4 7
4 8
4 9
81 changes: 81 additions & 0 deletions problem/2533/main.cpp
@@ -0,0 +1,81 @@
#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>

using namespace std;

int N;
vector<vector<int>> ADJ;
vector<pair<int, int>> depths;
vector<int> leaves;
vector<int> parents;
vector<bool> early;

void dfs(int u, int s, int depth) {
depths.emplace_back(-depth, u);
bool has_child = false;
for (auto v : ADJ[u]) {
if (v == s) {
continue;
}
has_child = true;
parents[v] = u;
dfs(v, u, depth + 1);
}
if (!has_child) {
leaves.emplace_back(u);
}
}

int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cin >> N;
ADJ.resize(N);
parents.resize(N);
early.resize(N);
for (int i = 0; i < N - 1; i++) {
int u, v;
cin >> u >> v;
ADJ[u - 1].emplace_back(v - 1);
ADJ[v - 1].emplace_back(u - 1);
}
parents[0] = -1;
dfs(0, -1, 0);
int ans = 0;
for (auto leaf : leaves) {
if (!early[parents[leaf]]) {
early[parents[leaf]] = true;
ans++;
}
}
sort(depths.begin(), depths.end());
for (auto [depth, u] : depths) {
if (early[u]) {
continue;
}
int parent = parents[u];
bool all_early = true;
for (auto v : ADJ[u]) {
if (v == parent) {
continue;
}
if (!early[v]) {
all_early = false;
break;
}
}
if (all_early) {
if (parent != -1 && !early[parent]) {
early[parent] = true;
ans++;
}
} else {
early[u] = true;
ans++;
}
}
cout << ans << "\n";
return 0;
}
1 change: 1 addition & 0 deletions problem/2533/output
@@ -0,0 +1 @@
3
1 change: 1 addition & 0 deletions problem/2533/output2
@@ -0,0 +1 @@
3

0 comments on commit b3a60e7

Please sign in to comment.