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 ",ans); }