The beauty of programming 2015 Preliminary _ Prime number correlation (binary chart)

Source: Internet
Author: User

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)

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.