Test instructions: How many kinds of cutting method of a tree make each separated sub-tree nodes the same number, and output each method needs to cut the number of edges, ascending output;
First, to divide the tree, the rest of the subtree must be a factor of the total number of nodes (according to test instructions, excluding the number itself);
To make a table, the number of 1~1e6 has a maximum of 240 factors, then only the enumeration factor, given is 6s;
First, to have a root tree, enumerate each edge, as long as the number of sub-tree nodes under this edge is a multiple of the enumeration factor, ans++;
Finally, if [n/(enumerated factor) is]-1==ans, the enumerated factor is valid;
Because if it happens to be evenly divided, the number of edges that need to be cut is [n/factor]-1;
AC Code:
#include <iostream> #include <string.h> #include <math.h> #include <algorithm> using namespace
Std
const int MAXN=1E6+10;
int q,head[2*maxn],sub[maxn],vis[maxn],sto[300],k,s[300];
struct point{int to;
int next;
}PT[2*MAXN];
void Init () {q=k=0;
memset (head,-1,sizeof (head));
memset (vis,0,sizeof (VIS));
} void Add (int u,int v) {Pt[q].next=head[u];
Pt[q].to=v;
head[u]=q++;
} int dfs (int st) {vis[st]=1;
for (int i=head[st];i!=-1;i=pt[i].next) {int to=pt[i].to;
if (!vis[to]) {Sub[st]+=dfs (to);
}} return sub[st];
} void carry (int x) {int r;
for (int i=1;i<=sqrt (x+0.5); i++) {if (x%i==0) {r=x/i;
if (r==i) sto[k++]=r;
else {sto[k++]=i;
Sto[k++]=r;
}}}} int main () {Std::ios::sync_with_stdio (false);
Cin.tie (0); Cout.tie (0);
int n,cnt,u,v,p=0;
Init ();
cin>>n;
for (int i=1;i<=n-1;i++) {cin>>u>>v;
Add (U,V);
Add (V,u);
} carry (n);
for (int i=1;i<=n;i++) sub[i]=1;
DFS (1);
for (int i=1;i<=n;i++)//cout<<i<< "*******" <<sub[i]<<endl;
for (int i=0;i<k;i++) {cnt=0;
for (int j=2;j<=n;j++) {if (sub[j]%sto[i]==0) cnt++;
} if (n/sto[i]-1==cnt&&cnt!=0) s[p++]= (n/sto[i]-1);
} sort (s,s+p);
for (int i=0;i<p;i++) {if (i==0) cout<<s[i];
else cout<< "" <<s[i];
} cout<<endl;
return 0; }/************************************************************** problem:5085 user:dp43 language:c++ R Esult: Correct time:2452 ms memory:56248 KB ****************************************************************/