Prime number Correlation
Test instructions: The maximum independent set of binary graphs (a set of vertices that are not connected to any two vertices in the graph)
Idea: Alternating dyeing method, two colors to dye the picture. The most color is the maximum independent set.
#include <iostream> #include <cstdio> #include <cstring> #include <cstdlib> #include < algorithm> #include <queue> #include <set> #include <map> #include <vector> #include < Cmath> #define LL __int64#define INF 0x3fffffffusing namespace std;vector<vector<int> >g (1005); int a[ 1005];int vis[1005];int n;int tm[1005];int max=0;int is_prime (int m) {int k=sqrt (m); if (m==2| | m==3) return 1; else for (int i=2;i<=k;i++) if (m%i==0) return 0; return 1;} void dfs (int x,int color) {if (x==n-1) return; Vis[x]=1; for (int i=0;i<g[x].size (); i++) {int e=g[x][i]; if (!vis[e]) {if (tm[e]==0) {tm[e]=-color; Color=-color; DFS (E,color); }}}}int Main () {//freopen ("D:\\test.txt", "R", stdin); int T; int t=1; cin>>t; while (t--) {scanf ("%d", &n); for (int i=0;i<n;i++) {scanf ("%d", &Amp;a[i]); } for (int i=0;i<1005;i++) g[i].clear (); Sort (a,a+n); int m=0; for (int i=0;i<n;i++) {for (int j=i+1;j<n;j++) {if (a[j]%a[i]==0) {if (is_p Rime (A[j]/a[i]) {g[i].push_back (j); m++; }}}} int ans=0; memset (Tm,0,sizeof (tm)); memset (vis,0,sizeof (VIS)); for (int i=0;i<n;i++) {if (Tm[i]==0&&!vis[i]) {tm[i]=-1; DFS (I,-1); }} int t1=0,t2=0; for (int i=0;i<n;i++) {if (tm[i]==-1) t1++; if (tm[i]==1) t2++; }//cout<<t1<< "" <<t2<<endl; Ans=max (T1,T2); cout<< "Case #" <<t++<< ":" <<ans<<endl; } return 0;}
The beauty of programming 2015 Preliminary _ Prime number correlation (binary chart)