hdu 3335 最大独立集
有整除关系就建边,然后求最大独立集。
AC代码:
1 #include <iostream> 2 #include <cstring> 3 #include <cstdio> 4 using namespace std; 5 6 const int N = 1001; 7 const int M = 1000000; 8 bool visit[N]; 9 __int64 a[N]; 10 int head[N]; 11 int mark[N]; 12 int n, e; 13 14 struct Edge 15 { 16 int v, next; 17 } edge[M]; 18 19 void addEdge( int u, int v ) 20 { 21 edge[e].v = v; 22 edge[e].next = head[u]; 23 head[u] = e++; 24 } 25 26 int dfs( int u ) 27 { 28 for ( int i = head[u]; i != -1; i = edge[i].next ) 29 { 30 int v = edge[i].v; 31 if ( !visit[v] ) 32 { 33 visit[v] = true; 34 if ( mark[v] == -1 || dfs( mark[v] ) ) 35 { 36 mark[v] = u; 37 return 1; 38 } 39 } 40 } 41 return 0; 42 } 43 44 int hunagry() 45 { 46 memset( mark, -1, sizeof(mark) ); 47 int res = 0; 48 for ( int i = 1; i <= n; i++ ) 49 { 50 memset( visit, 0, sizeof(visit) ); 51 res += dfs(i); 52 } 53 return res; 54 } 55 56 int main() 57 { 58 int t; 59 scanf("%d", &t); 60 while ( t-- ) 61 { 62 e = 0; 63 memset( head, -1, sizeof(head) ); 64 scanf("%d", &n); 65 for ( int i = 1; i <= n; i++ ) 66 { 67 scanf("%I64d", a + i); 68 for ( int j = i - 1; j > 0; j-- ) 69 { 70 if ( a[i] % a[j] == 0 || a[j] % a[i] == 0 ) 71 { 72 addEdge( j, i ); 73 } 74 } 75 } 76 int ans = n - hunagry(); 77 printf("%d\n", ans); 78 } 79 return 0; 80 }