UVA 12083,uva12083

來源:互聯網
上載者:User

UVA 12083,uva12083
UVA 12083 - Guardian of Decency

題目連結

題意:給定一些男女,滿足身高差不大於40,喜歡同一種音樂,不喜歡同一種體育項目,並且性別不同,就可能發生關係,現在老師要帶一些男女出去玩,要求不能有一對發生關係,問最多能帶多少人

思路:分男女,把會發生關係的連邊,然後做最大匹配,最後n-最大匹配就是最多能帶的人

代碼:

#include <cstdio>#include <cstring>#include <string>#include <vector>#include <iostream>#include <cstdlib>using namespace std;const int N = 505;struct People {int h;string music, sport;People() {}People(int h, string music, string sport) {this->h = h;this->music = music;this->sport = sport;}}boy[N], girl[N];vector<int> g[N];int bn, gn;int T, n;bool judge(People a, People b) {if (abs(a.h - b.h) <= 40 && a.music == b.music && a.sport != b.sport) return true;return false;}int match[N], vis[N];bool dfs(int u) {for (int i = 0; i < g[u].size(); i++) {int v = g[u][i];if (vis[v]) continue;vis[v] = 1;if (match[v] == -1 || dfs(match[v])) {match[v] = u;return true;}}return false;}int hungary() {int ans = 0;memset(match, -1, sizeof(match));for (int i = 0; i < bn; i++) {memset(vis, 0, sizeof(vis));if (dfs(i)) ans++;}return ans;}int main() {cin >> T;while (T--) {cin >> n;int h; bn = gn = 0;string sex, music, sport;for (int i = 0; i < n; i++) {cin >> h >> sex >> music >> sport;if (sex == "M") boy[bn++] = People(h, music, sport);else girl[gn++] = People(h, music, sport);}for (int i = 0; i < bn; i++) {g[i].clear();for (int j = 0; j < gn; j++) {if (judge(boy[i], girl[j]))g[i].push_back(j);}}printf("%d\n", n - hungary());}return 0;}





聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.