-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1829F.cpp
74 lines (67 loc) · 1.36 KB
/
1829F.cpp
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/*
RunAt - grey
*/
#include <iostream>
#include <map>
#include <algorithm>
#include <vector>
using namespace std;
typedef int long long ll;
#define p2(a,b) cout << a << " " << b << "\n";
void solve() {
ll n,m;
cin >> n >> m;
vector<vector<ll>> adj(n+1);
for (int i=1; i<n; i++) {
ll x,y;
cin >> x >> y;
adj[x].push_back(y);
adj[y].push_back(x);
}
map<ll,ll> mp;
ll cntLeaves = 0;
for (auto v : adj) {
ll sz = v.size();
if (sz == 0) continue;
mp[sz]++;
if (sz == 1) cntLeaves++;
}
/*
If there are only two sizes available.
One will be 1, which will be leaf nodes.
Another will be all the other vertices
including the root.
{
1: x*y;
y+1: x+1;
}
*/
if (mp.size() == 2) {
for (auto [sz,count] : mp) {
if (sz != 1) {
p2(count-1,cntLeaves/(count-1));
return;
}
}
}
/*
In this case, we have sz = 1, x, y+1
{
1: x*y;
x: 1;
y+1: x;
}
*/
for (auto [sz,count] : mp) {
if (count == 1) {
p2(sz, cntLeaves/sz);
return;
}
}
}
int main() {
int tests = 1;
cin >> tests;
while (tests--) solve();
return 0;
}