HDU 3335 Divisibility(Dilworth定理+最小路径覆盖)

Divisibility

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2043    Accepted Submission(s): 823


Problem 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.
 

Author
DaiZhenyang@BUPT
 

Source
HDOJ Monthly Contest – 2010.03.06 
 

Recommend
lcy

题目大意:

    给你一些数字,让你从中选择尽可能多的数字,满足它们任意两个存在整除关系。


解题思路:

    最容易想到的就是把数字看做点,把存在整除关系的点之间连上无向边,求最大独立集。可是无向图的最大独立集是NP问题,我们还需要继续思考。可以发现整除关系满足偏序关系,这时就可以利用考虑使用Dilworth定理。

HDU 3335 Divisibility(Dilworth定理+最小路径覆盖)_第1张图片

    Dilworth定理应用到图论中就变成了:对于满足偏序关系(自反,反对称,传递)的有向图,最大独立集等于最小路径覆盖。

    所以我们就可以根据整除关系建立有向图,这个DAG的最小路径覆盖即为答案。

    有一点需要注意的是,由于有向图最小路径覆盖要求图是DAG,而这里是可以出现环的,所以我们要手动去掉环,具体操作就是对给出的序列去重,建边时不要建立自环。


AC代码:

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;
#define INF 0x3f3f3f3f
#define LL long long
#define mem(a,b) memset((a),(b),sizeof(a))

const int MAXV=1000+3;
int V;
LL a[MAXV];
int num_x;//左边顶点数
vector G[MAXV];//图的邻接表形式(左边顶点在前面,只需要建立从左边指向右边的边即可)
int match_x[MAXV],match_y[MAXV];//顶点匹配对象
int dis,dis_x[MAXV],dis_y[MAXV];//距离(用于多路增广)
bool used[MAXV];

bool searchP()//标记距离(用于多路增广)
{
    queue que;
    dis=INF;
    mem(dis_x,-1);
    mem(dis_y,-1);
    for(int i=0;idis)
            break;
        for(int i=0;i

你可能感兴趣的:(算法,图论)