最小路徑覆蓋:Divisibility

DivisibilityTime Limit:1000MS    Memory Limit:32768KB    64bit IO Format:%I64d & %I64u

Description

As we know,the fzu AekdyCoin is famous of math,especially in the field of number theory.So,many people call him "the descendant of Chen Jingrun",which brings him a good reputation.
AekdyCoin also plays an important role in the ACM_DIY group,many people always ask him questions about number theory.One day,all members urged him to conduct a lesson in the group.The rookie daizhenyang is extremely weak at math,so he is delighted.
However,when AekdyCoin tells us "As we know, some numbers have interesting property. For example, any even number has the property that could be divided by 2.",daizhenyang got confused,for he don't have the concept of divisibility.He asks other people for help,first,he randomizely writes some positive integer numbers,then you have to pick some numbers from the group,the only constraint is that if you choose number a,you can't choose a number divides a or a number divided by a.(to illustrate the concept of divisibility),and you have to choose as many numbers as you can.
Poor daizhenyang does well in neither math nor programming.The responsibility comes to you!
 

Input

An integer t,indicating the number of testcases,
For every case, first a number n indicating daizhenyang has writen n numbers(n<=1000),then n numbers,all in the range of (1...2^63-1).
 

Output

The most number you can choose.
 

Sample Input

1 3 1 2 3
 

Sample Output

2 Hint: If we choose 2 and 3,one is not divisible by the other,which is the most number you can choose.
 
一開始想錯了,想把不能除盡的連一塊兒,然後找最長的路徑
後來發現這樣一來可能有問題,另一方面要找路徑的長度貌似不太可行
正確做法是把能除盡的連一塊兒,然後找最小覆蓋路徑的數量

#include <iostream>
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>

using namespace std;

const int MAXN=1010;
int uN,vN;  //u,v數目
int g[MAXN][MAXN];//編號是0~n-1的
int linker[MAXN];//是否有父親,如果有,父親是誰
bool used[MAXN]; //在本次dfs中是否在路徑中被指過
bool dfs(int u){
    int v;
    for(v=0;v<vN;v++)
        if(g[u][v]&&!used[v]){
            used[v]=true;
            if(linker[v]==-1||dfs(linker[v])){//v沒有父親或v的父親有其他兒子
                linker[v]=u;
                return true;
            }
        }
    return false;
}

int hungary(){
    int res=0;
    int u;
    memset(linker,-1,sizeof(linker));
    for(u=0;u<uN;u++){
        memset(used,0,sizeof(used));
        if(dfs(u))  res++;
    }
    return res;
}

bool cmp(long long a, long long b){
    return a > b;
}

long long num[1010];

int main(){
    int t;
    int n;
    int i, j;
    scanf("%d", &t);
    while(t --){
        scanf("%d", &n);
        for(i = 0; i < n; i ++){
            scanf("%I64d", &num[i]);
        }
        memset(g, 0, sizeof(g));
        sort(num, num + n, cmp);
//        for(i = 0; i < n; i ++)
//            printf("%I64d ", num[i]);
        uN = n;
        vN = n;
        for(i = 0; i < n ; i ++){
            for(j = i + 1; j < n; j ++){
                if(num[i] % num[j] == 0){
                    g[i][j] = 1;
                }
            }
        }
        int ans = n - hungary();
        printf("%d\n", ans);
    }
    return 0;
}





發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章