cerc2017 justified Jungle

Source: Internet
Author: User

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 ****************************************************************/
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.