-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsamp.cpp
More file actions
89 lines (79 loc) · 1.84 KB
/
samp.cpp
File metadata and controls
89 lines (79 loc) · 1.84 KB
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include <bits/stdc++.h>
using namespace std;
// Pragmas
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#pragma GCC optimize("Ofast")
#define fast \
{ \
ios ::sync_with_stdio(false); \
cin.tie(0); \
cout.tie(0); \
}
#define pb push_back
#define ll long long
#define vi vector<int>
#define vll vector<long long>
#define f(i, a, b) for (ll i = a; i < b; i++)
#define pii pair<int, int>
#define pll pair<ll, ll>
#define endl "\n"
#define ALL(v) v.begin(), v.end()
#define nl cout << "\n";
#define mp make_pair
#define ff first
#define ss second
vector<vector<int>> adj;
vector<int> dp, child, in;
void dfs(int root, int par)
{
dp[root] = 0;
child[root] = 1;
for (auto x : adj[root])
{
if (x == par)
continue;
dfs(x, root);
child[root] += child[x];
}
vector<int> mine;
for (auto x : adj[root])
{
if (x == par)
continue;
mine.pb(x);
}
if (mine.size() >= 2)
dp[root] = max(child[mine[0]] - 1 + dp[mine[1]], child[mine[1]] - 1 + dp[mine[0]]);
else if (mine.size() == 1)
dp[root] = child[mine[0]] - 1;
}
int main()
{
fast
// srand(time(NULL));
ll ntc = 1;
cin >> ntc;
f(asdfghjk, 1, ntc + 1)
{
ll n;
cin >> n;
adj = vector<vector<int>>(n + 1);
dp = vector<int>(n + 1);
child = vector<int>(n + 1);
in = vector<int>(n + 1);
for (int i = 0; i < n - 1; ++i)
{
int a;
int b;
cin >> a >> b;
adj[a].pb(b);
adj[b].pb(a);
in[a]++;
in[b]++;
}
dfs(1, -1);
cout << dp[1] << endl;
}
return 0;
}