zoukankan      html  css  js  c++  java
  • nyoj 月老的难题 (稳定婚姻问题)

    百度了一下稳定婚姻问题。。 还有什么GS算法。。

    以为什么高端的东西。。

    尼玛结果代码跟上一题一模一样的好吗。。

    醉了。。

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstdlib>
     4 #include<cstring>
     5 #include<string>
     6 #include<queue>
     7 #include<algorithm>
     8 #include<map>
     9 #include<iomanip>
    10 #include<climits>
    11 #include<string.h>
    12 #include<cmath>
    13 #include<stdlib.h>
    14 #include<vector>
    15 #include<set>
    16 #define INF 1e7
    17 #define MAXN 100010
    18 #define maxn 50
    19 #define maxm 1000
    20 #define Mod 1000007
    21 using namespace std;
    22 typedef long long LL;
    23 
    24 
    25 int T;
    26 int u, v;
    27 int n, m;
    28 int ans;
    29 vector<int> G[555];
    30 int vis[10010];
    31 int match[10010];
    32 
    33 bool path(int u)
    34 {
    35     for (int i = 0; i < G[u].size(); ++i) {
    36         int v = G[u][i];
    37         if (true == vis[v]) continue;
    38         vis[v] = true;
    39         if (match[v] == -1 || path(match[v])) {
    40             match[v] = u;
    41             return true;
    42         }
    43     }
    44     return false;
    45 }
    46 
    47 void hungarian()
    48 {
    49     ans = 0;
    50     memset(match,-1,sizeof(match));
    51     for (int i = 1; i <= n; ++i) {
    52         memset(vis,0,sizeof(vis));
    53         if (path(i)) ans++;
    54     }
    55 }
    56 
    57 void run()
    58 {
    59     scanf("%d%d",&n,&m);
    60     for (int i = 0; i <= n; ++i) {
    61         G[i].clear();
    62     }
    63     for (int i = 0; i < m; ++i) {
    64         scanf("%d%d",&u,&v);
    65         G[u].push_back(v);
    66     }
    67     hungarian();
    68     printf("%d
    ",ans);
    69     
    70 }
    71 int main()
    72 {
    73     scanf("%d", &T);
    74     while (T--)
    75         run();
    76     return 0;
    77 }
  • 相关阅读:
    VM CentOS建立共享文件夹实现VS Code在windows环境下go开发,在centos环境里编译
    Centos7安装protobuf3.6.1
    C++继承汇总(单继承、多继承、虚继承、菱形继承)
    用 Python 发邮件
    C# Task任务教程
    C#获取IP和默认网关
    GO语言从入门到放弃
    Go结构体标签
    解除百度云浏览器端对下载大文件的限制
    移动浏览器横屏
  • 原文地址:https://www.cnblogs.com/usedrosee/p/4338486.html
Copyright © 2011-2022 走看看