#include <iostream>
#include <cstdio>
#include <cmath>
#include <vector>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn = 50009;
__int64 start[maxn];
__int64 end[maxn];
__int64 child[maxn];
__int64 val[maxn];
int vis[maxn];
vector<int>v[maxn];
int n, m, k, tol;
void dfs1(int u)
{
vis[u] = 1;
start[u]=tol++;
for(int i=0; i<v[u].size(); i++)
if(!vis[ v[u][i] ])
{
dfs1( v[u][i] );
}
end[u] = tol++;
child[u] = (end[u]-start[u])/2;
}
void dfs2(int u)
{
vis[u] = 1;
for(int i=0; i<v[u].size(); i++)
{
int uu = v[u][i];
if(!vis[uu])
{
dfs2(uu);
val[u] += val[uu] + child[uu] + 1;
}
}
}
void dfs3(int u)
{
vis[u] = 1;
for(int i=0; i<v[u].size(); i++)
{
int uu = v[u][i];
if(!vis[uu])
{
val[uu] += val[u] - val[uu] - child[uu] -1 + n - child[uu] -1;
dfs3(uu);
}
}
}
int main()
{
int t, a, b;
scanf("%d", &t);
while(t--)
{
tol = 0;
memset(start, 0, sizeof(start));
memset(end, 0, sizeof(end));
memset(val, 0, sizeof(val));
scanf("%d%d%d", &n, &m, &k);
for(int i=1; i<=n; i++) v[i].clear();
for(int i=1; i<n; i++)
{
scanf("%d%d", &a, &b);
v[a].push_back(b);
v[b].push_back(a);
}
memset(vis, 0, sizeof(vis));
dfs1(1);
memset(vis, 0, sizeof(vis));
dfs2(1);
memset(vis, 0, sizeof(vis));
dfs3(1);
__int64 ans = 1000000000000000;
for(int i=1; i<=n; i++)
{
ans = min(ans, val[i]);
}
printf("%I64d\n", ans*m*m*k);
int tmp = -1;
for(int i=1; i<=n; i++)
if(ans==val[i])
{
if(tmp != -1) printf("%d ", tmp);
tmp = i;
}
printf("%d\n\n", tmp);
}
return 0;
}
POJ-4045-数据结构+dfs
最新推荐文章于 2024-08-28 20:17:14 发布