CodeForces - 566F Clique in the Divisibility Graph
Discription
As you must know, the maximum clique problem in an arbitrary graph is NP-hard. Nevertheless, for some graphs of specific kinds it can be solved effectively.
Just in case, let us remind you that a clique in a non-directed graph is a subset of the vertices of a graph, such that any two vertices of this subset are connected by an edge. In particular, an empty set of vertexes and a set consisting of a single vertex, are cliques.
Let‘s define a divisibility graph for a set of positive integers A?=?{a1,?a2,?...,?an}as follows. The vertices of the given graph are numbers from set A, and two numbers ai and aj (i?≠?j) are connected by an edge if and only if either ai is divisible by aj, or aj is divisible by ai.
You are given a set of non-negative integers A
Input
The first line contains integer n (1?≤?n?≤?106), that sets the size of set A.
The second line contains n distinct positive integers a1,?a2,?...,?an (1?≤?ai?≤?106) — elements of subset A. The numbers in the line follow in the ascending order.
Output
Print a single number — the maximum size of a clique in a divisibility graph for set A.
Examples
Input8Output
3 4 6 8 10 18 21 24
3
Note
In the first sample test a clique of size 3 is, for example, a subset of vertexes {3,?6,?18}. A clique of a larger size doesn‘t exist in this graph.
我們不妨把邊看成有向的,當 a[i]|a[j] 時從 i 向 j 連邊(要先把重復元素縮成一個點),這樣我們發現隨便一條鏈上的元素 都可以成為一個團,並且這是一個DAG。
所以我們直接找最長鏈好了。
#include<bits/stdc++.h> #define ll long long using namespace std; const int maxn=1000000; int n,cnt[maxn+5],f[maxn+5]; inline int read(){ int x=0; char ch=getchar(); for(;!isdigit(ch);ch=getchar()); for(;isdigit(ch);ch=getchar()) x=x*10+ch-‘0‘; return x; } int main(){ n=read(); for(int i=1;i<=n;i++) cnt[read()]++; for(int i=maxn;i;i--){ for(int j=i;j<=maxn;j+=i) f[i]=max(f[i],f[j]); f[i]+=cnt[i]; } printf("%d\n",f[1]); return 0; }
CodeForces - 566F Clique in the Divisibility Graph