POJ 1419 (maximum independent set)

Source: Internet
Author: User

Basic knowledge:

Maximum independent set: The vertex set V takes k vertices, 22 of which are not connected to each other.

Maximal Regiment: The vertex set V takes k vertices, and its 22 have edge connections.

Maximum independent set = maximum group of complement graphs. (Fill graph = Full picture-original)

A very detailed explanation: http://www.cnblogs.com/yefeng1627/archive/2013/03/31/2991592.html

Ideas:

Said.. The maximum independent set of the original image is NP problem, then the maximum number of independent concentrated vertices can be obtained by finding the maximum number of vertices in the largest group in the map.

Attach the optimized template:

1#include <cstdio>2#include <cstring>3 #defineN 10104 BOOLFlag[n], a[n][n];5 intans, cnt[n], group[n], N, vis[n];6 //Maximum Regiment: v to take k vertices, two points connected to each other7 //Maximum independent set: K vertices in V, not connected between two points8 //Maximum number of groups = maximum number of independent sets in a complement graph9  Ten BOOLDfsintUintPOS) { One     intI, J; A      for(i = u+1; I <= N; i++){ -         if(Cnt[i]+pos <= ans)return 0; -         if(A[u][i]) { the              //compare with current group elements, take non-n (i) -              for(j =0; J < Pos; J + +)if(!a[i][Vis[j]) Break;  -             if(j = pos) {//if empty, then all are adjacent to I, then I will be added to the largest group -Vis[pos] =i; +                 if(Dfs (I, pos+1) )return 1;  -             }     +         } A     }     at     if(Pos >ans) { -              for(i =0; I < POS; i++ ) -Group[i] = vis[i];//Maximum group element -Ans =Pos; -             return 1;  -     }     in     return 0; - }  to voidMaxclique () + { -ans=-1; the      for(intI=n;i>0; i--) *     { $vis[0]=i;Panax NotoginsengDFS (I,1); -cnt[i]=ans; the     } +}
Bron–kerbosch Algorithm

Code June:

1#include <iostream>2#include <cstdio>3#include <cstdlib>4#include <cstring>5#include <string>6#include <queue>7#include <algorithm>8#include <map>9#include <iomanip>Ten#include <climits> One#include <string.h> A#include <numeric> -#include <cmath> -#include <stdlib.h> the#include <vector> -#include <stack> -#include <Set> - #defineFor (x, B, E) for (int x=b;x<= (e); x + +) + #defineREP (x, N) for (int x=0;x< (n); x + +) - #defineINF 1e7 + #defineMAXN 100010 A #defineMAXN 1000010 at #defineMod 1000007 - #defineN 1010 - using namespacestd; -typedefLong LongLL; -  -  in BOOLFlag[n], a[n][n]; - intans, cnt[n], group[n], N, M, Vis[n]; to BOOLDfsintUintPOS) { +     intI, J; -      for(i = U +1; I <= N; i++){ the         if(Cnt[i] + pos <= ans)return 0; *         if(A[u][i]) { $             //compare with current group elements, take non-n (i)Panax Notoginseng              for(j =0; J < Pos; J + +)if(!a[i][vis[j]]) Break; -             if(j = pos) {//if empty, then all are adjacent to I, then I will be added to the largest group theVis[pos] =i; +                 if(Dfs (I, POS +1))return 1; A             } the         } +     } -     if(Pos >ans) { $          for(i =0; I < POS; i++) $Group[i] = vis[i];//Maximum group element -Ans =Pos; -         return 1; the     } -     return 0;Wuyi } the  - voidMaxclique () Wu { -Ans =-1; About      for(inti = n; i >0; i--) $     { -vis[0] =i; -DFS (I,1); -Cnt[i] =ans; A     } + } the  - intMain () { $     intT; thescanf"%d", &T); the      while(t--){ thescanf"%d%d", &n, &m); the         intx, y; -Memset (A,0,sizeof(a)); in          for(inti =0; I < m; i++){ thescanf"%d%d", &x, &y); theA[x][y] = a[y][x] =1; About         } the          for(inti =1; I <= N; i++) the              for(intj =1; J <= N; J + +) the                 if(i = = j) A[i][j] =0; +                 ElseA[I][J] ^=1; - Maxclique (); the Bayi                 if(Ans <0) ans =0; theprintf"%d\n", ans); the                  for(inti =0; i < ans; i++) -printf (i = =0?"%d":"%d", Group[i]); -                 if(Ans >0) puts (""); the     } the     return 0; the}
code June

POJ 1419 (maximum independent set)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.