#include<bits/stdc++.h>
using namespace std;
#define int long long
void dfs(int u, vector<vector<int>> &e, vector<int> &d){
d[u] = 1;
for(int i = 0;i<e[u].size();i++){
int v = e[u][i];
if(d[v]!=-1) continue;
dfs(v, e, d);
d[u]+=d[v];
}
}
void dfs2(int u, int p, vector<vector<int>> &e, vector<int> &dp, vector<int> &d){
dp[u] = 0;
for(int i = 0;i<e[u].size();i++){
int v = e[u][i];
if(v==p) continue;
dp[u] = max(dp[u], d[v]-1);
dfs2(v, u, e, dp, d);
int s = 0;
for(int j = 0;j<e[u].size();j++){
if(j==i) continue;
int w = e[u][j];
if(w==p) continue;
s+=d[w]-1;
}
dp[u] = max(dp[u], dp[v]+s);
}
}
void solve(){
int n;cin>>n;
vector<vector<int>> e(n);
for(int i = 0;i<n-1;i++){
int u, v;cin>>u>>v;u--;v--;
e[u].push_back(v);
e[v].push_back(u);
}
vector<int> d(n, -1);
dfs(0, e, d);
vector<int> dp(n, -1);
dfs2(0, -1, e, dp, d);
cout<<dp[0]<<endl;
return;
}
int32_t main(){
cin.tie(0);
cout.tie(0);
ios_base::sync_with_stdio(false);
int t = 1;
cin>>t;
while(t--){
solve();
}
return 0;
}
Compilation message
rack.cpp: In function 'void dfs(long long int, std::vector<std::vector<long long int> >&, std::vector<long long int>&)':
rack.cpp:9:20: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
9 | for(int i = 0;i<e[u].size();i++){
| ~^~~~~~~~~~~~
rack.cpp: In function 'void dfs2(long long int, long long int, std::vector<std::vector<long long int> >&, std::vector<long long int>&, std::vector<long long int>&)':
rack.cpp:19:20: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
19 | for(int i = 0;i<e[u].size();i++){
| ~^~~~~~~~~~~~
rack.cpp:25:24: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
25 | for(int j = 0;j<e[u].size();j++){
| ~^~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1 ms |
348 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1 ms |
348 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1 ms |
348 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |