ACM International Collegiate Programming Contest, Egyptian Collegiate Programming Contest ,Problem D

Problem D. Dream Team
Input file: dream.in
Output file: standard output
Balloon Color: Pink
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.

队友过的;
其实我感觉数据还是太水了;

#include
#include
#include
#include
using namespace std;
typedef long long ll;
const int maxn = 1e5 + 5;
vector<int>g[maxn];
int p[maxn];
int n;

int find(int x) {
    return (p[x] == x) ? x : (p[x] = find(p[x]));
}

int main() {
    freopen("dream.in", "r", stdin);
    int T; scanf("%d", &T);
    int kase = 1;
    while (T--) {
        for (int i = 0; i <= maxn; i++) {
            g[i].clear();
            p[i] = i;
        }

        scanf("%d", &n);
        for (int i = 1; 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);
                    if (x / j != j)g[x / j].push_back(i);
                }
            }
        }

        ll ans = 0;
        for (int i = maxn - 5; i >= 1; i--) {
            if (g[i].empty())continue;
            for (int j = 0; j < g[i].size()-1; j++) {
                int x = find(g[i][j]);
                int y = find(g[i][j + 1]);
                if (x != y) {
                    p[y] = x;
                    ans += i;
                }
            }
        }
        printf("Case %d: %d\n", kase++, ans);
    }
    return 0;
}

你可能感兴趣的:(ACM International Collegiate Programming Contest, Egyptian Collegiate Programming Contest ,Problem D)