2017省组队练习 Number of Connected Components UVALive - 7638 (数论+并查集)

题目链接:点击打开链接

Given N nodes, each node is labeled with an integer between 1 and 106(inclusive and labels arenot necessarily distinct). Two nodes have an edge between them, if and only if the GCD (GreatestCommon Divisor) of the labels of these nodes is greater than 1. Count the number of connectedcomponents in the graph.

Input


First line of the input T (T ≤ 100) denotes the number of testcases. Then T cases follow. Each caseconsists of 2 lines. The first line has a number N (1 ≤ N ≤ 105) denoting the number of nodes. Thenext line consists of N numbers. The i-th (1 ≤ i ≤ n) number Xi (1 ≤ Xi ≤ 106) denotes the label ofthe node i.


Output


For each case you have to print a line consisting consisting the case number followed by an integerwhich denotes the number of connected components. Look at the output for sample input for details


Sample Input

2

3

2 3 4

6

2 3 4 5 6 6

Sample Output

Case 1: 2

Case 2: 2


题目大意:有n个数字,如果任意两个数字的gcd不是1,就让这两个数字连一条边,求联通分量的个数

题解:

这个题最后30s过的,真的是一路坎坷。一开始想的是把所有的素数求出来,然后找出这个数的倍数进行建边,但是感觉会超时就没这么写(然而他们却同500s过的,真是醉了)。讲一下我的思路和遇到的问题吧。把每个数进行质因数分解(1特别处理),然后让这个数与他的质因数建边。那么只要有相同质因数的就会在一个联通分量里面。只要有相同的质因数那么他们的gcd就不会为1.所以说就对他们建边就行了。然后在查找时,从1-1000000的数都遍历一遍,如果这个数在给出的数列中的话就看看他们的根,把根进行标记,看看有多少不同的根,就是联通分量的个数。

遇到的问题:一开始我不是对数建边的,而是对位置建边的,但是这样的话分解来的质因数不一定在数列中,wa了一次。然后考虑1的时候不全,注意每个1都是一个联通分量。为了重复分解,又对数列进行的排序去重。

#include <iostream>
#include <bits/stdc++.h>

using namespace std;

int a[100500];
int arr[1000500];
int h[100];
bool vis[1005000];
int pos[1005000];

int f(int x)
{
    int r,y,t;
    r=y=x;
    while(r!=arr[r])r=arr[r];
    while(y!=r)
    {
        t=arr[y];
        arr[y]=r;
        y=t;
    }
    return r;
}
int main()
{
    int t,n;
    int w=0;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        int ant=0;
        memset(vis,false,sizeof(vis));
        for(int i=0; i<=1000006; i++)arr[i]=i;
        for(int i=1; i<=n; i++)
        {
            scanf("%d",&a[i]);
            vis[a[i]]=true;
        }
        int top=1;
        sort(a+1,a+n+1);
        int pp=1;
        //cout<<a[i]<< " "<<endl;
        for(int i=1; i<=n; i++)
        {
            if(a[i]!=1)
            {
                pp=i;
                break;
            }
            ant++;
        }
        a[top++]=a[pp];
        for(int i=pp+1; i<=n; i++)
        {
            if(a[i]!=a[i-1])
            {
                a[top++]=a[i];
            }
        }
        n=top-1;
        for(int i=1; i<=n; i++)
        {
            int x=a[i];
            int ans=0;
            for(int j=2; j*j<=x; j++)
            {
                if(x%j==0)
                {
                    while(x%j==0)
                    {
                        x=x/j;
                    }
                    h[ans++]=j;
                }
            }
            if(x!=1)h[ans++]=x;
            for(int j=0; j<ans; j++)
            {
                int p=h[j];
                if(f(a[i])!=f(p))
                {
                    arr[f(a[i])]=f(p);
                }
            }
        }
        int num=0;
        memset(pos,0,sizeof(pos));
        for(int i=2; i<=1000000; i++)
        {
            if(vis[i])
            {
                int p=f(i);
                if(!pos[p])
                {
                    num++;
                    pos[p]=1;
                }
            }
        }
        //cout<<num<<" "<<ant<<endl;
        printf("Case %d: %d\n",++w,num+ant);
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值