当前位置: 代码迷 >> 综合 >> UVALive 3415 Guardian of Decency(最大独立集)
  详细解决方案

UVALive 3415 Guardian of Decency(最大独立集)

热度:26   发布时间:2023-12-08 10:51:13.0

题目链接:
UVALive 3415 Guardian of Decency
题意:
刘汝佳的大白书P356页上的题目.
最大独立集.


#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <climits>
#include <cmath>
#include <ctime>
#include <cassert>
#define IOS ios_base::sync_with_stdio(0); cin.tie(0);
using namespace std;
typedef long long ll;
const int MAX_N = 510;int T, n, m, K, total;
int head[MAX_N * MAX_N], vis[MAX_N], match[MAX_N];struct Student{int height;string sex, music, game;
}boy[MAX_N], girl[MAX_N];struct Edge{int to,next;
}edge[MAX_N * MAX_N];inline bool check(int i, int j)
{if(abs(boy[i].height - girl[j].height) > 40) return true;if(boy[i].music != girl[j].music) return true;if(boy[i].game == girl[j].game) return true;return false;
}inline void AddEdge(int from, int to)
{edge[total].to = to;edge[total].next = head[from];head[from] = total++;
}inline bool dfs(int u)
{for(int i = head[u]; i != -1; i = edge[i].next){int to = edge[i].to;if(vis[to]) continue;vis[to] = 1;if(match[to] == -1 || dfs(match[to])){match[to] = u;return true;}}return false;
}inline int Hungary()
{int res = 0;memset(match, -1, sizeof(match));for(int i = 0; i < n; i++){memset(vis, 0, sizeof(vis));if(dfs(i)) res++;}return res;
}int main()
{IOS;cin >> T;while(T--){cin >> K;n = m = 0;for(int i = 0; i < K; i++){int h;string tmp1, tmp2, tmp3;cin >> h >> tmp1 >> tmp2 >> tmp3;if(tmp1[0] == 'M') {boy[n].height = h, boy[n].music = tmp2;boy[n].game = tmp3, n++;}else {girl[m].height = h, girl[m].music = tmp2;girl[m].game = tmp3, m++;}}total = 0;memset(head, -1, sizeof(head));for(int i = 0; i < n; i++){for(int j = 0; j < m; j++){if(check(i, j) == false ) AddEdge(i, j);}}int ans = Hungary();cout << K - ans << endl;}return 0;
}
  相关解决方案