Lightoj 1356-prime Independence factorization parity composition + HK optimized __ maximum matching (weight matching), independent set, group

Source: Internet
Author: User

1356-prime Independence

PDF (中文版) Statistics Forum
Time Limit:3 second (s) Memory limit:32 MB

A set of integers is called prime independent if none of it is a prime multiple of another. An integer a are said to be a prime multiple of b if,

A = b x k (where K is a prime [1])

So 6 is a prime multiple of 2, but 8 are not. And for example, {2, 8, the is prime independent but {2, 8,} or {3, 6} are not.

Now, given a set of distinct positive integers calculate the largest prime. Input

Input starts with an integer T (≤20), denoting the number of test cases.

Each case starts with a integer N (1≤n≤40000) denoting the size of the set. Next line contains N integers separated by a. Each of these Nintegers are distinct and between 1 and 500000 inclusive. Output

For each case, print the case number and the size of the largest prime independent subset.

Sample Input Output for Sample Input

3

5

2 4 8 16 32

5

2 3 4 6 9

3

1 2 3

Case 1:3

Case 2:3

Case 3:2

Note

1 An integer are said to be a prime if it's divisible by exactly two distinct. Few prime numbers are 2, 3, 5, 7, one, ...

2. The Dataset is huge, use faster I/O methods. PROBLEM Setter:abdullah AL MAHMUD SPECIAL thanks:jane ALAM


Tle to Death, O (╯-╰) o O (n*n) of the time complexity of the construction of the damage ...


Restrictions: If a% b = = 0 && A/b = k where k is prime, then A and B cannot exist at the same time in a set.

To give you the number of n, so you find the maximum set to meet the limit, the number of output elements.



Train of thought: according to each number of factors of the parity of the creation of a bipartite graph, structure a good picture. Run HK, Hungary did not dare to write, even I did not dare to use vector.


Build diagram: Can not be two-layer for loop traversal build, O (n*n) of the complexity of the map cannot afford. You can find all the a[i of each number of p[], determine whether A[i]/p[exists, and then build the graph according to the parity. The time complexity of this mapping is the worst is O (n*10), then add HK, total time complexity

O (sqrt (n) * m + n * 10).


Note: It is not possible to directly preprocess all the mass factors of the number, MLE O (╯-╰) o


AC Code:


#include <cstdio> #include <cstring> #include <algorithm> #include <vector> #include <queue&
Gt
#define MAXN 40000+10 #define MAXM 1000000+10 #define INF 0x3f3f3f3f #define DEBUG printf ("1\n");
using namespace Std;
struct edge{int to, next;};
Edge EDGE[MAXM];
int HEAD[MAXN], edgenum;
int p[500010], num[500010];//records whether each number is prime numbers record the number of qualitative factors in each number int A[MAXN];
int ID[MAXN];
int Oddnum, evennum;
    void Getp () {memset (p, 0, sizeof (p));
        for (int i = 2; I <= 500000 i++) {if (p[i)) continue;
    for (int j = 2*i J <= 500000 j+=i) p[j] = 1;
} p[1] = 1;
}//vector<int> p[500000+10];
        void Getpsum () {for (int j = 1; J <= 500000; j +) {int cnt = 0;
        int n = j;
                for (int i = 2; I * I <= n; i++) {if (n% i = = 0) {while (n% i = = 0)
                    {cnt++;
                n/= i;
   }
            }     } if (n > 1) cnt++;
    NUM[J] = cnt;
    } void Init () {edgenum = 0;
Memset (Head,-1, sizeof (head));
    } void Addedge (int u, int v) {Edge E1 = {V, head[u]};
    Edge[edgenum] = E1;
Head[u] = edgenum++;
} int n;
int vis[500010];
int p[30], top;
    void Getprime (int n) {top = 0;
            for (int i = 2; I * I <= n; i++) {if (n% i = = 0) {p[top++] = i;
        while (n% i = = 0) n/= i;
} if (n > 1) p[top++] = n;
    } void Getmap () {scanf ("%d", &n);
    Oddnum = Evennum = 0;
    memset (Vis, 0, sizeof (VIS));
        for (int i = 1; I <= n; i++) {scanf ("%d", &a[i]);
        Vis[a[i]] = i;//mark the element has already appeared if (Num[a[i]] & 1) id[i] = ++oddnum;
    else id[i] = ++evennum;
    Init ();
   for (int i = 1; I <= n; i++) {getprime (a[i]);//processing factor for (int j = 0; J < top; J +) {         int goal = A[i]/p[j];
            int index = Vis[goal]; if (index)//exists {if (Num[a[i]] & 1 && num[a[index]% 2 = 0) ad
                Dedge (Id[i], id[index]);
            else if (Num[a[i]]% 2 = 0 && Num[a[index]] & 1) Addedge (Id[index), id[i]);
{}}} bool USED[MAXN];
int DX[MAXN], DY[MAXN];
int MX[MAXN], MY[MAXN];
        int DFS (int u) {for (int i = head[u]; I!=-1; i = edge[i].next) {int v = edge[i].to;
            if (!used[v] && dy[v] = = Dx[u] + 1) {Used[v] = true; if (my[v] = = 1 | |
                DFS (My[v])) {My[v] = u; mx[u] = v;
            return 1;
}} return 0;
int kcase = 1;
    void HK () {memset (MX,-1, sizeof (MX));
    memset (I,-1, sizeof (my));
    int ans = 0;
        while (1) {bool flag = false;
     memset (DX, 0, sizeof (DX));   memset (dy, 0, sizeof (DY));
        Queue<int> Q;
        for (int i = 1; I <= oddnum i++) if (mx[i] = = 1) q.push (i); while (!
            Q.empty ()) {int u = q.front ();
            Q.pop ();
                for (int i = head[u]; I!=-1; i = edge[i].next) {int v = edge[i].to;
                    if (!dy[v]) {Dy[v] = Dx[u] + 1;
                    if (my[v] = = 1) flag = true;
                        else {Dx[my[v]] = Dx[u] + 1;
                    Q.push (My[v]);
        }} if (!flag) break;
        Memset (used, false, sizeof (used));
    for (int i = 1; I <= oddnum i++) if (mx[i] = = 1) ans = DFS (i);
printf ("Case%d:%d\n", kcase++, N-ans);
    int main () {GETP ();
    Getpsum ();
    int t; scanf ("%d", &t);
        while (t--) {getmap ();
    HK ();
return 0;
 }


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.