Clique in the Divisibility Graph
来源:互联网 发布:软件工程项目实践报告 编辑:程序博客网 时间:2024/04/29 22:02
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. Determine the size of a maximum clique in a divisibility graph for set A.
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.
Print a single number — the maximum size of a clique in a divisibility graph for set A.
83 4 6 8 10 18 21 24
3
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.
这题不难,就是优化时间上有点难度。可以写出完成任务的代码,但是不符合条件,所以都是白费功夫。之后再去看看别人写的代码,然后再自己重写了一遍就AC了。
#include<iostream>#include<algorithm>#include<cstring>#include<string>#include<cstdio>using namespace std;#define T 1000100int n,dp[T],i,j,ma=0,k;int main(){//freopen("input.txt","r",stdin);scanf("%d",&n);for(i=0;i<n;++i){scanf("%d",&k);ma = max(ma,++dp[k]);for(j=2*k;j<T;j+=k){dp[j] = max(dp[j],dp[k]);}}printf("%d\n",ma);return 0;}
- Clique in the Divisibility Graph
- 周赛-Clique in the Divisibility Graph
- VK Cup 2015 - Finals F. Clique in the Divisibility Graph
- codeforces 566F F. Clique in the Divisibility Graph(dp)
- 【CF 566F】 Clique in the Divisibility Graph
- Codeforces 566 F. Clique in the Divisibility Graph
- CF Clique in the Divisibility Graph (DP)
- codeforces 566F. Clique in the Divisibility Graph dp
- VK Cup 2015 - Finals, online mirror F. Clique in the Divisibility Graph 数论
- VK Cup 2015 - Finals, online mirror F - Clique in the Divisibility Graph dp
- Clique in the Divisibility Graph(dp递推的应用) (上升子序列)
- [CodeForces 566F] Clique in the Divisibility Graph (记忆化搜索)
- UVA11324 The Largest Clique
- The Largest Clique
- hdu4725 The Shortest Path in Nya Graph
- hdu4725 The Shortest Path in Nya Graph
- The Shortest Path in Nya Graph
- HDU4725-The Shortest Path in Nya Graph
- Foundation-NSString类
- Shiro学习(6)Realm整合
- 如何搭建一个独立博客——简明Github Pages与Hexo教程
- 禁止滑动的ViewPager
- Install R-base 3.1.0 in Ubuntu 14.04
- Clique in the Divisibility Graph
- 修改MySQL数据库字符编码为UTF-8解决中文乱码
- ZTD 简单做十个好习惯总结
- Shiro学习(7)与Web整合
- 二叉树算法总结
- 将博客搬至CSDN
- Delete Node in a Linked List Java LeetCode
- CLRS 4.4用递归树方法求解递归式
- java子类构造器调用父类构造器初始化顺序