码迷,mamicode.com
首页 > 其他好文 > 详细

Codeforces 566F Clique in the Divisibility Graph

时间:2016-07-13 23:02:52      阅读:150      评论:0      收藏:0      [点我收藏+]

标签:

http://codeforces.com/problemset/problem/566/F

题目大意

有n个点,点上有值a[i], 任意两点(i, j)有无向边相连当且仅当 

(a[i] mod a[j])==0||(a[j] mod a[i])==0


问这幅图中的最大连通子图是多少。

思路:直接转移,时间复杂度O(n^1.5)

#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<iostream>
int n,a[2000005],f[2000005];
int read(){
    int t=0,f=1;char ch=getchar();
    while (ch<0||ch>9){if (ch==-) f=-1;ch=getchar();}
    while (0<=ch&&ch<=9){t=t*10+ch-0;ch=getchar();}
    return t*f;
}
int main(){
    n=read();int mx=0;
    for (int i=1;i<=n;i++){
        a[i]=read();
        mx=std::max(mx,a[i]);
    }
    std::sort(a+1,a+1+n);
    int ans=0;
    for (int i=1;i<=n;i++){
        f[a[i]]++;
        for (int j=a[i]+a[i];j<=mx;j+=a[i])
         f[j]=std::max(f[j],f[a[i]]);
        ans=std::max(ans,f[a[i]]);
    }
    printf("%d\n",ans);
}

 

Codeforces 566F Clique in the Divisibility Graph

标签:

原文地址:http://www.cnblogs.com/qzqzgfy/p/5667934.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!