알고리즘 관련/BOJ
BOJ)15271 친구 팰린드롬2
자손9319
2017. 12. 5. 20:21
문제: icpc.me/15271
남자와 여자에 대한 이분 그래프가 형성되므로 이분 매칭으로 최대로 세울 수 있는 친구 수를 구해줄 수 있다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | #include <cstdio> #include <algorithm> #include <vector> #include <cstring> using namespace std; int n, m, visited[222], b[222]; vector<vector<int>> vt; int dfs(int here) { if (visited[here])return false; visited[here] = true; for (auto next : vt[here]) { if (!b[next] || dfs(b[next])) { b[next] = here; return true; } } return false; } int bmatch() { int ret = 0; for (int i = 1; i <= n; i++) { memset(visited, 0, sizeof(visited)); if (dfs(i))ret += 2; } return ret; } int main() { scanf("%d%d", &n, &m); vt.resize(n + 1); for (int i = 0; i < m; i++) { int x, y; scanf("%d%d", &x, &y); if ((x + y) % 2) { if (x % 2) vt[x].push_back(y); else vt[y].push_back(x); } } int res = bmatch(); if (res != n)res++; printf("%d\n", res); return 0; } | cs |