-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTree_Matching.cpp
More file actions
44 lines (38 loc) · 770 Bytes
/
Tree_Matching.cpp
File metadata and controls
44 lines (38 loc) · 770 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <bits/stdc++.h>
using namespace std;
#define forn(i,k,e) for(int i = k; i < e; i++)
/*
*/
signed main() {
int n;
cin >> n;
map<int, vector<int>> adj;
vector<bool> taken(n + 1, false);
vector<bool> visited(n + 1, false);
forn(i, 1, n) {
int a, b;
cin >> a >> b;
adj[a].push_back(b);
adj[b].push_back(a);
// cout << a << "-" << b << endl;
}
int ans = 0;
auto dfs = [&](auto && self, int cur) -> void {
visited[cur] = 1;
for (int nxt : adj[cur]) {
if (!visited[nxt]) {
visited[nxt] = 1;
self(self, nxt);
if (!taken[cur] && !taken[nxt]) {
taken[cur] = 1;
taken[nxt] = 1;
ans++;
// cout << cur << " " << nxt << endl;
}
}
}
};
dfs(dfs, adj.begin()->first);
cout << ans << endl;
return 0;
}