We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
#include<algorithm>#include<cstdio>#include<vector>usingnamespacestd;#define FOR(i, a, b) for (int i = (a); i < (b); i++)#define REP(i, n) FOR(i, 0, n)#define pb push_backtypedeflonglongll;intri(){intx;scanf("%d",&x);returnx;}constintN=10000,MOD=1000000007;intfac[N];vector<int>e[N];intf(intn){intpath=n,ans=1;REP(i,n)if(e[i].size()==1)path--;REP(i,n)if(e[i].size()>1){intleaf=0,inner=0;for(autov:e[i])if(e[v].size()==1)leaf++;elseinner++;if(inner>2)return0;ans=ll(ans)*fac[leaf]%MOD;}returnpath==1?ans:ans*2%MOD;}intmain(){for(intcc=ri();cc--;){intn=ri();REP(i,n)e[i].clear();fac[0]=1;FOR(i,1,n)fac[i]=ll(fac[i-1])*i%MOD;REP(i,n-1){intx=ri(),y=ri();e[x].pb(y);e[y].pb(x);}printf("%d\n",f(n));}}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
ByteLandian Tours
You are viewing a single comment's thread. Return to all comments →
C++ Solution