Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define ll long long
- #include <bits/stdc++.h>
- using namespace std;
- const int OO = 1e9;
- const double EPS = 1e-9;
- int n;
- vector<vector<int>> graph;
- int mem[1000000][2];
- int solve(int idx, int rem, int p) {
- if(mem[idx][rem] != -1) {
- return mem[idx][rem];
- }
- int &ret = mem[idx][rem];
- ret = rem;
- for(int e : graph[idx]) {
- if(e != p)
- if(rem)
- ret += min(solve(e,0,idx),solve(e,1,idx));
- else
- ret += solve(e,1,idx);
- }
- return ret;
- }
- int main()
- {
- ios_base::sync_with_stdio(NULL);
- cin.tie(0);
- cout.tie(0);
- cin >> n;
- memset(mem,-1,sizeof(mem));
- graph.resize(n);
- for(int i = 0; i < n-1; i++) {
- int a,b;
- cin >> a >> b;
- a--,b--;
- graph[a].push_back(b);
- graph[b].push_back(a);
- }
- cout << min(solve(0,0,-1),solve(0,1,-1)) << "\n";
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement