cerc2017 Justified Jungle

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/m0_37109329/article/details/79963923

题意:一棵树有多少种切法使得每个分离的子树节点数相同,并输出每种方法需要切边的数目,升序输出;

首先,想要均分这个树,那么剩下的子树节点必定是总节点数的因子(根据题意,排除这个数本身);

打个表,1~1e6的数最多有240个因子,那么只需要枚举因子,给的是6s;

先转换为有根树,枚举每条边,只要这个边下的子树节点数是枚举的因子的倍数,ans++;

最后如果[n/(枚举的因子)]-1==ans,则枚举得因子合法;

因为如果恰好能够被均分,需要割的边数为[n/因子]-1;

AC代码:

#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++
    Result: 正确
    Time:2452 ms
    Memory:56248 kb
****************************************************************/

Jungle Roads Jungle Roads

09-22

Problem DescriptionrnrnrnThe Head Elder of the tropical island of Lagrishan has a problem. A burst of foreign aid money was spent on extra roads between villages some years ago. But the jungle overtakes roads relentlessly, so the large road network is too expensive to maintain. The Council of Elders must choose to stop maintaining some roads. The map above on the left shows all the roads in use now and the cost in aacms per month to maintain them. Of course there needs to be some way to get between all the villages on maintained roads, even if the route is not as short as before. The Chief Elder would like to tell the Council of Elders what would be the smallest amount they could spend in aacms per month to maintain roads that would connect all the villages. The villages are labeled A through I in the maps above. The map on the right shows the roads that could be maintained most cheaply, for 216 aacms per month. Your task is to write a program that will solve such problems. rnrnThe input consists of one to 100 data sets, followed by a final line containing only 0. Each data set starts with a line containing only a number n, which is the number of villages, 1 < n < 27, and the villages are labeled with the first n letters of the alphabet, capitalized. Each data set is completed with n-1 lines that start with village labels in alphabetical order. There is no line for the last village. Each line for a village starts with the village label followed by a number, k, of roads from this village to villages with labels later in the alphabet. If k is greater than 0, the line continues with data for each of the k roads. The data for each road is the village label for the other end of the road followed by the monthly maintenance cost in aacms for the road. Maintenance costs will be positive integers less than 100. All data fields in the row are separated by single blanks. The road network will always allow travel between all the villages. The network will never have more than 75 roads. No village will have more than 15 roads going to other villages (before or after in the alphabet). In the sample input below, the first data set goes with the map above. rnrnThe output is one integer per line for each data set: the minimum cost in aacms per month to maintain a road system that connect all the villages. Caution: A brute force solution that examines every possible set of roads will not finish within the one minute time limit. rn rnrnSample Inputrn9rnA 2 B 12 I 25rnB 3 C 10 H 40 I 8rnC 2 D 18 G 55rnD 1 E 44rnE 2 F 60 G 38rnF 0rnG 1 H 35rnH 1 I 35rn3rnA 2 B 10 C 40rnB 1 C 20rn0rn rnrnSample Outputrn216rn30rn rnrnSourcernMid-Central USA 2002

没有更多推荐了,返回首页

私密
私密原因:
请选择设置私密原因
  • 广告
  • 抄袭
  • 版权
  • 政治
  • 色情
  • 无意义
  • 其他
其他原因:
120
出错啦
系统繁忙,请稍后再试

关闭