D - Dream Team Gym - 101840D

Before the World Cup, we would like to form the ultimate dream team. The team that will win against
all teams. We have a big group of the top N players in the world, where each player has a number
representing his skill level. Two players i and j with skill levels ai and aj have a compatibility of passing
the ball between each other; this compatibility level is measured by the greatest common divisor of their
skill levels (i.e. compatibility(i, j) = gcd(ai
, aj )). We would like to decide a strategy that connects all
the players of the dream team with a tree of connections between the players. If two players are directly
connected in the chosen strategy, then they will pass the ball between each others during the matches, with
the compatibility level between them. The compatibility of the strategy is the sum of the compatibility
levels of its connections. What is the maximum total compatibility of the chosen strategy that connects
all players?
Input
The first line of the input contains a single integer 1 ≤ T ≤ 50 the number of test cases. Each test case
consists of one line that contains N + 1 space separated integers; the first integer is N, followed by the
skill levels a1, · · · , an; where 1 ≤ N ≤ 105 and 1 ≤ ai ≤ 105
for all i.
Output
For each test case, print a single line displaying the case number and the maximum compatibility of the
dream team’s strategy.
Example
dream.in standard output
2
2 3 6
5 5 6 7 10 21
Case 1: 3
Case 2: 17
Note
In the second test case we connect the players 3−5 with compatibility 7, players 1−4 with compatibility 5,
players 2−5 with compatibility 3 and players 2−4 with compatibility 2; therefore the total compatibility
of the dream team’s strategy is 17.

题意:给n个点,任意两个点之间的权值为gcd(a,b),求构建一棵树的最大权值和

#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn = 1e5+10;
int par[maxn];
vector<int> g[maxn];
int Find(int x)
{
    while(x!=par[x])
    {
        x = par[x];
    }
    return x;
}

int main()
{
     freopen("dream.in", "r", stdin);
    int t;
    cin>>t;
    int kase = 0;
    while(t--)
    {
        int n;
        cin>>n;
        int i;
        for(i=0; i<maxn; i++)
        {
            par[i] = i;
            g[i].clear();
        }
        for(i=0; i<n; i++)
        {
            int x;
            scanf("%d",&x);
            for(int j=1; j<=sqrt(x); j++)
            {
                if(x%j==0)
                {
                    g[j].push_back(i);//记录因子为j的数有哪一个
                    if(x/j!=j)
                        g[x/j].push_back(i);
                }
            }
        }
        ll ans = 0;
        for(i=maxn-10; i>=1; i--)  // 枚举因子
        {
            if(g[i].empty())
            {
                continue;
            }
            else
            {
                for(int j=0; j<(int)g[i].size()-1; j++)
                {
                    int x = Find(g[i][j]);
                    int y = Find(g[i][j+1]);
                    if(x!=y)
                    {
                        par[x] = y;
                        ans +=i;
                    }
                }
            }
        }
        printf("Case %d: %lld\n",++kase,ans);
    }
    return 0;
}

因为数据很水,所以也可以用生成树做

#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn = 1e5+10;
int a[maxn];
int par[maxn];
struct node
{
    int from;
    int to;
    int w;
}edge[maxn<<2];

int cmp(struct node a,struct node b)
{
    return a.w>b.w;
}

void init()
{
    for(int i=0; i<maxn; i++)
    {
        par[i] = i;
    }
}

int Find(int x)
{
    while(x!=par[x])
    {
        x = par[x];
    }
    return x;
}

int main()
{
     freopen("dream.in", "r", stdin);
    int t;
    cin>>t;
    int kase = 0;
    while(t--)
    {
        init();
        int n;
        cin>>n;
        int i;
        for(i=0; i<n; i++)
        {
            scanf("%d",&a[i]);
        }
        int j;
        int len = 0;
        for(i=0; i<n-1; i++)
        {
            for(j=i+1; j<n; j++)
            {
                edge[len].from = i;
                edge[len].to = j;
                edge[len].w = __gcd(a[i],a[j]);
                len++;
            }
        }
        ll ans = 0;
        sort(edge,edge+len,cmp);
        for(i=0; i<len; i++)
        {
            int x = Find(edge[i].from);
            int y = Find(edge[i].to);
            if(x!=y)
            {
                par[x] = y;
                ans +=edge[i].w;
            }
        }
        printf("Case %d: %lld\n",++kase,ans);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值