#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
#include<vector>
#include<sstream>
#include<string>
#include<climits>
#include<stack>
#include<set>
#include<bitset>
#include<cmath>
#include<deque>
#include<map>
#include<queue>
#define iinf 2000000000
#define linf 1000000000000000000LL
#define dinf 1e200
#define eps 1e-5
#define all(v) (v).begin(),(v).end()
#define sz(x) x.size()
#define pb push_back
#define mp make_pair
#define lng long long
#define sqr(a) ((a)*(a))
#define pii pair<int,int>
#define pll pair<lng,lng>
#define pss pair<string,string>
#define pdd pair<double,double>
#define X first
#define Y second
#define pi 3.14159265359
#define ff(i,xi,n) for(int i=xi;i<=(int)(n);++i)
#define ffd(i,xi,n) for(int i=xi;i>=(int)(n);--i)
#define ffl(i,r) for(int i=head[r];i!=-1;i=edge[i].next)
#define cc(i,j) memset(i,j,sizeof(i))
#define two(x) ((lng)1<<(x))
#define N 55555
#define M 1000000
#define lson l , mid , rt << 1
#define rson mid + 1 , r , rt << 1 | 1
#define Mod n
#define Pmod(x) (x%Mod+Mod)%Mod
using namespace std;
typedef vector<int> vi;
typedef vector<string> vs;
typedef unsigned int uint;
typedef unsigned lng ulng;
template<class T> inline void checkmax(T &x,T y){if(x<y) x=y;}
template<class T> inline void checkmin(T &x,T y){if(x>y) x=y;}
template<class T> inline T Min(T x,T y){return (x>y?y:x);}
template<class T> inline T Max(T x,T y){return (x<y?y:x);}
template<class T> T gcd(T a,T b){return (a%b)==0?b:gcd(b,a%b);}
template<class T> T lcm(T a,T b){return a*b/gcd(a,b);}
template<class T> T Abs(T a){return a>0?a:(-a);}
template<class T> inline T lowbit(T n){return (n^(n-1))&n;}
template<class T> inline int countbit(T n){return (n==0)?0:(1+countbit(n&(n-1)));}
template<class T> inline bool isPrimeNumber(T n)
{if(n<=1)return false;for (T i=2;i*i<=n;i++) if (n%i==0) return false;return true;}
struct pp
{
int v,next;
}edge[3*N];
struct ps
{
lng val;
int id;
ps(lng _val=0,int _id=0):val(_val),id(_id){}
}hehe[N];
bool cmp(ps p,ps q)
{
return p.val<q.val||(q.val==p.val&&p.id<q.id);
}
lng n,I,R,tot,dp1[N],dp2[N],head[N],num[N];
int ncase;
inline void addedge(int u,int v)
{
edge[tot].v=v,edge[tot].next=head[u],head[u]=tot++;
}
int dfs1(int r,int pre)
{
dp1[r]=0;
int res=1;
ffl(i,r)
{
int v=edge[i].v;
if(v==pre) continue;
res+=dfs1(v,r);
dp1[r]+=dp1[v];
}
dp1[r]+=res-1;
num[r]=res;
return res;
}
void dfs2(int r,int pre)
{
ffl(i,r)
{
int v=edge[i].v;
if(v==pre) continue;
dp2[v]=n+dp2[r]+dp1[r]-dp1[v]-num[v]*2;
dfs2(v,r);
}
}
int main()
{
scanf("%d",&ncase);
while(ncase--)
{
cc(head,-1);
tot=0;
scanf("%lld%lld%lld",&n,&I,&R);
ff(i,2,n)
{
int u,v;
scanf("%d%d",&u,&v);
addedge(u,v);
addedge(v,u);
}
dp2[1]=0;
dfs1(1,-1);
dfs2(1,-1);
ff(i,1,n)
hehe[i]=ps(dp1[i]+dp2[i],i);
sort(hehe+1,hehe+1+n,cmp);
printf("%lld\n",I*I*R*hehe[1].val);
printf("%d",hehe[1].id);
for(int i=2;hehe[i].val==hehe[1].val;++i)
printf(" %d",hehe[i].id);
puts("");
puts("");
}
return 0;
}
poj 4045 树形dp
最新推荐文章于 2022-02-24 21:59:13 发布