码迷,mamicode.com
首页 > 编程语言 > 详细

【二分图】【最大匹配】【匈牙利算法】洛谷 P2071 座位安排 seat.cpp/c/pas

时间:2014-11-04 19:32:32      阅读:305      评论:0      收藏:0      [点我收藏+]

标签:style   blog   io   color   for   sp   div   log   bs   

 ∵每个座位可以坐俩人,所以拆点最大匹配。

 1 #include<cstdio>
 2 #include<vector>
 3 #include<cstring>
 4 using namespace std;
 5 #define N 2001
 6 vector<int>G[N<<2];
 7 typedef vector<int>::iterator ITER;
 8 int mat[N<<2];
 9 bool vis[N<<2];
10 int n,x,y;
11 bool dfs(int U)
12 {
13     for(ITER it=G[U].begin();it!=G[U].end();it++)
14       if(!vis[*it])
15         {
16           vis[*it]=1;
17           if(mat[*it]==-1||dfs(mat[*it]))
18             {
19               mat[*it]=U;
20               return 1;
21             }
22         }
23       return 0;
24 }
25 int max_match()
26 {
27     memset(mat,-1,sizeof(mat)); int res=0;
28     for(int i=1;i<=(n<<1);i++)
29       {
30         memset(vis,0,sizeof(vis));
31         if(dfs(i)) res++;
32       }
33     return res;
34 }
35 int main()
36 {
37     scanf("%d",&n);
38     for(int i=1;i<=(n<<1);i++)
39       {
40         scanf("%d%d",&x,&y);
41         G[i+(n<<1)].push_back(x);
42         G[i+(n<<1)].push_back(x+n);
43         G[i+(n<<1)].push_back(y);
44         G[i+(n<<1)].push_back(y+n);
45         G[x].push_back(i+(n<<1));
46         G[x+n].push_back(i+(n<<1));
47         G[y].push_back(i+(n<<1));
48         G[y+n].push_back(i+(n<<1));
49       }
50     printf("%d\n",max_match());
51     for(;;);
52     return 0;
53 }

【二分图】【最大匹配】【匈牙利算法】洛谷 P2071 座位安排 seat.cpp/c/pas

标签:style   blog   io   color   for   sp   div   log   bs   

原文地址:http://www.cnblogs.com/autsky-jadek/p/4074184.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!