LightOJ1356 maximum independent set HK algorithm prime factorization

Source: Internet
Author: User
Tags cas

Analytical

When A and B meet \ (A = b \times prime\) , we say that a B has a conflict relationship, all numbers are considered to be vertices in the graph, then A and b have a conflict, and there is an edge between a A and a. The title is: given some numbers, choose some number from these numbers to make up a set, so that there is no conflict between each of the two numbers in this set, that is, there is no edge between each of the two vertices, and ask the maximum number of this set.

Any integer can be decomposed into the product of multiple prime factors, and only after decomposition, the number of prime factors is odd and even two number is only possible conflict, because a number of prime factor is odd number a only multiply two primes to become a new prime factor of the odd number B, it is obvious that A and B does not have a conflict relationship, Two numbers with an even number of prime factors are the same. So we can divide all the numbers into two sets based on the odd number of prime factors, the elements in the X and Y,x, and the elements in Y may have edges, and the elements in the same set have no edges, obviously this is a bipartite graph.

To look at this problem, in fact, is to find the maximum number of independent sets of elements. The largest independent set is the selection of the most points to form a set, so that any selected two points are not connected.

There is a formula: number of elements of the maximum independent set = number of vertices-maximum number of matches. The maximum matching number here is the maximum match of the binary graph. So we just need to find out the maximum number of matches for the two graph above, and the number of vertices minus the maximum match is the answer. Note that the number of vertices of the two graphs up to 4e4, with the Hungarian algorithm will be severely timed out, we use an optimized algorithm--hopcroft-krap algorithm, adjacency table map, time complexity is \ (O (V\sqrt{e}). The HK algorithm can refer to my other article, "Finding the maximum matching--hopcroft-krap algorithm for binary graphs".

To find the maximum independent set there is another way to find the largest group of the graph, but this method of time complexity is very high, we do not use this method to solve the problem

AC Code
#include <bits/stdc++.h>using namespace Std;const int maxn = 4e4 + 5;const int maxm = 5e5 + 5;const int inf = 0x3f3f 3f3f;struct edge{int to, next;}; BOOL Isnp[maxm];int prime_num[maxm];int prm[maxm];int n;int MX[MAXN], MY[MAXN], Nx, Ny;int DX[MAXN], DY[MAXN], Dis;bool vs T[maxn];int AX[MAXN], ay[maxn];int top;int HEAD[MAXN];    Edge ns[maxm];int ext[maxm];int cur;void cal_prime_number () {int res;    PRIME_NUM[1] = 0;        for (int i = 2; i < MAXM; ++i) {int t = i;        res = 0;                for (int j = 2, J * J <= t; ++j) {while (t% J = 0) {++res;            T/= J;        }} if (t! = 1) ++res;    Prime_num[i] = res;    }}void get_prime (int u) {top = 0;            for (int i = 2; I * i <= u; ++i) {if (u% i = = 0) {prm[top++] = i;        while (u% i = = 0) u/= i; }} if (U! = 1) prm[top++] = u;}  BOOL Searchp () {queue<int> Q;  dis = INF;    memset (DX,-1, sizeof DX);    memset (Dy,-1, sizeof dy);            for (int i = 1; I <= Nx; ++i) {if (mx[i] = = 1) {Q.push (i);        Dx[i] = 0; }} while (!        Q.empty ()) {int u = q.front ();        Q.pop ();        if (Dx[u] > dis) break;            for (int i = head[u]; i =-1; i = ns[i].next) {int v = ns[i].to;                if (dy[v] = = 1) {Dy[v] = Dx[u] + 1;                if (my[v] = =-1) dis = dy[v];                    else {Dx[my[v]] = Dy[v] + 1;                Q.push (My[v]); }}}}} return dis! = INF;}        BOOL DFS (int u) {for (int i = head[u]; I! =-1; i = ns[i].next) {int v = ns[i].to;            if (!vst[v] && dy[v] = = Dx[u] + 1) {vst[v] = 1;            if (my[v]! =-1 && dy[v] = = dis) continue; if (my[v] = =-1 | |      DFS (My[v])) {          MY[V] = u;                Mx[u] = v;            return true; }}} return false;}    int Maxmatch () {int res = 0;    memset (MX,-1, sizeof MX);    Memset (My,-1, sizeof my);        while (true) {memset (VST, 0, sizeof VST);        Queue<int> Q;        dis = INF;        memset (DX,-1, sizeof DX);        memset (Dy,-1, sizeof dy);                for (int i = 1; I <= Nx; ++i) {if (mx[i] = = 1) {Q.push (i);            Dx[i] = 0; }} while (!            Q.empty ()) {int u = q.front ();            Q.pop ();            if (Dx[u] > dis) break;                for (int i = head[u]; i =-1; i = ns[i].next) {int v = ns[i].to;                    if (dy[v] = = 1) {Dy[v] = Dx[u] + 1;                    if (my[v] = =-1) dis = dy[v];  else {Dx[my[v]] = Dy[v] + 1;                      Q.push (My[v]);        }}}} if (dis = = inf) break;        for (int i = 1; I <= Nx; ++i) {if (mx[i] = = 1 && DFS (i)) ++res; }} return res;}    void Add_edge (int u, int v) {ns[cur].next = Head[u];    Ns[cur].to = v;    Head[u] = cur; ++cur;}    int main () {cal_prime_number ();    int t;    scanf ("%d", &t);    int INP;        for (int cas = 1; CAs <= t; ++cas) {cur = 0;        Memset (Head,-1, sizeof head);        memset (ext, 0, sizeof ext);        Nx = Ny = 0;        scanf ("%d", &n);            for (int i = 1; I <= n; ++i) {scanf ("%d", &AMP;INP);                if (PRIME_NUM[INP] & 1) {Ax[++nx] = INP;            EXT[INP] = Nx;                } else {Ay[++ny] = INP;            EXT[INP] = Ny; }} for (int i = 1; I <= Nx; ++i)       {get_prime (ax[i]);                for (int j = 0; j < top; ++j) {int goal = Ax[i]/prm[j];                int index = Ext[goal];                if (index = = 0) continue;            Add_edge (i, index);            }} for (int i = 1; I <= Ny; ++i) {get_prime (ay[i]);                for (int j = 0; j < top; ++j) {int goal = Ay[i]/prm[j];                int index = Ext[goal];                if (index = = 0) continue;            Add_edge (index, i);    }} printf ("Case%d:%d\n", CAs, N-maxmatch ()); } return 0;} /*352 4 8 16 3252 3 4 6 931 2 3*/

LightOJ1356 maximum independent set HK algorithm prime number decomposition

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.