【luogu P2065 [TJOI2011]卡片】 假题解

题目链接:https://www.luogu.org/problemnew/show/P2065

辣鸡匈牙利,没有优化贼鸡儿慢

 1 // luogu-judger-enable-o2
 2 #include <iostream>
 3 #include <cstdio>
 4 #include <algorithm>
 5 #include <cstring>
 6 #define ri register
 7 #define fo(a,b) for(ri int a = 1; a <= b; a++)
 8 using namespace std;
 9 const int maxn = 2000;
10 int T, n, m, tot, b[maxn], r[maxn], a[maxn][maxn], link[maxn], ans;
11 bool vis[maxn];
12 inline int read()
13 {
14     int ret=0;
15     char c=getchar();
16     while (c<'0' || c>'9') c=getchar();
17     while (c>='0' && c<='9'){
18         ret=((ret<<3)+(ret<<1))+c-'0';
19         c=getchar();
20     }
21     return ret;
22 }
23 int gcd(int x, int y)
24 {
25     if(x%y == 0) return y;
26     else return gcd(y,x%y);
27 }
28 
29 bool dfs(int x)
30 {
31     fo(i,n)
32     {
33         if(a[x][i] == 1 && !vis[i])
34         {
35             vis[i] = 1;
36             if(!link[i] || dfs(link[i]))
37             {
38                 link[i] = x;
39                 return 1;
40             }
41         }
42     }
43     return 0;
44 }
45 int main()
46 {
47     scanf("%d",&T);
48     fo(ii,T)
49     {
50         memset(b,0,sizeof(b));
51         memset(r,0,sizeof(r));
52         memset(a,0,sizeof(a));
53         memset(link,0,sizeof(link));
54         ans = 0;
55         m = read();
56         n = read();
57         
58         
59         fo(i,m)
60         b[i] = read();
61 
62         fo(i,n)
63         r[i] = read();
64         
65         fo(i,m)
66         fo(j,n)
67         {
68             if(gcd(b[i],r[j]) > 1)
69             {
70                 a[i][j] = 1;
71             }
72         }
73         
74         fo(i,m)
75         {
76             memset(vis,0,sizeof(vis));
77             if(dfs(i)) ans++;
78         }
79         printf("%d\n",ans);
80     }
81     return 0;
82 }

 

posted @ 2018-04-24 21:39  Misaka_Azusa  阅读(166)  评论(0编辑  收藏  举报
Live2D