Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma comment(linker, "/STACK:167177216")
- #include <stdio.h>
- #include <stack>
- #include <math.h>
- #include <iostream>
- #include <algorithm>
- #include <string.h>
- #include <string>
- #include <memory.h>
- #include <vector>
- #include <map>
- #include <queue>
- #include <set>
- #include <time.h>
- #include <cassert>
- #include <cstring>
- //#include <unordered_set>
- using namespace std;
- #define mp make_pair
- #define pb push_back
- #define pii pair<int, int>
- #define forn(i, n) for(int i = 0; i < (int)(n); i++)
- #define x first
- #define y second
- typedef long long li;
- typedef long double ld;
- typedef unsigned long long uli;
- const int INF = 1e9;
- const ld eps = 1e-9;
- const li MOD = (li)(4e6 + 37);
- const li INF64 = (li)(INF) * (li)(INF);
- const int ddx[] = {-1, 1, 1, -1};
- const int ddy[] = {1, 1, -1, -1};
- const int dx[] = {-1, -1, 0, 1, 1, 1, 0, -1};
- const int dy[] = {0, 1, 1, 1, 0, -1, -1, -1};
- const int dx4[] = {-1, 0, 1, 0};
- const int dy4[] = {0, 1, 0, -1};
- const int dxh[] = {-1, -1, -1, 1, 1, 1, 1, -1};
- const int dyh[] = {1, -1, -1, -1, -1, 1, 1, 1};
- const string dirs[] = {"RIGHT", "UP", "LEFT", "DOWN"};
- const int N = 1000;
- vector<int> g[N]; //spiski smezhnosti dereva
- bool used[N]; //poseshali vershinu ili net
- int depth[N]; //massiv glubin depth[i] == glubina vershini i
- int max_deep = 0; // max glubina v dereve
- int n; //kolichestvo vershin v dereve i kolichestvo reber
- //poisk v glubinu: prinimaet vershinu i ee glubinu
- void dfs(int v, int deep)
- {
- used[v] = true;
- depth[v] = deep;
- max_deep = max(max_deep, deep);
- for(int i = 0; i < g[v].size(); i++)
- {
- int to = g[v][i];
- if(!used[to])
- {
- used[to] = true;
- dfs(to, deep + 1);
- }
- }
- }
- //sama funkciya
- //type idet po vsem vershinam i esli glubina vershini ravna trebuemoy i vershina list(u nee tolko 1 rebro) to uvelichivaet otvet
- int cnt(int deep)
- {
- if(deep < 1 || deep > max_deep)
- return 0;
- if(deep == 1)
- return 0;
- int res = 0;
- for(int i = 1; i <= n; i++)
- if(depth[i] == deep && g[i].size() == 1)
- res++;
- return res;
- }
- int main()
- {
- //freopen("input.txt", "r", stdin);
- //freopen("output.txt", "w", stdout);
- //freopen("errors.txt", "w", stderr);
- //ios_base::sync_with_stdio(false);
- cin >> n;
- for(int i = 0; i < n - 1; i++)
- {
- int a, b;
- cin >> a >> b;
- g[a].pb(b);
- g[b].pb(a);
- }
- //cout << "777" << endl;
- dfs(1, 1);
- //cout << "WTF" << endl;
- for(int i = 1; i <= max_deep; i++)
- {
- cout << "kolichestvo list'ev na glubine " << i << " == " << cnt(i) << endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement