HDU 3829 cat vs dog

Source: Internet
Author: User

Question:

P. Each person has a favorite and a nasty animal. If the selected animal contains a person's favorite animal and does not contain the animal he hates, the person will be happy to ask at most a few people to be happy.

Ideas:

The maximum independence set in the bipartite graph uses the conflict between people to build the edge and find the maximum matching.

Note:

The example in the question shows that the animal names such as D1 and C1 may be longer than this one... So the array Length

Code:

#include<cstdio>#include<iostream>#include<cstring>#include<string>#include<algorithm>#include<map>#include<set>#include<vector>#include<queue>#include<cstdlib>#include<ctime>#include<cmath>using namespace std;typedef unsigned long long LL;#define N 510struct edge {int v, next;} ed[N * N];int vis[N], match[N], head[N];int n, tot;char like[N][10], dislike[N][10];void add(int u, int v) {ed[tot].v = v;ed[tot].next = head[u];head[u] = tot++;}bool dfs(int u) {int i, v;for (i = head[u]; ~i; i = ed[i].next) {v = ed[i].v;if (!vis[v]) {vis[v] = 1;if (!match[v] || dfs(match[v])) {match[v] = u;return true;}}}return false;}int bimatch() {int i, sol = 0;memset(match, 0, sizeof(match));for (i = 1; i <= n; i++) {memset(vis, 0, sizeof(vis));if (dfs(i))sol++;}return sol;}int main() {int i, j, ans;while (~scanf("%d%d%d", &i, &j, &n)) {for (i = 1; i <= n; i++)scanf("%s%s", like[i], dislike[i]);memset(head, -1, sizeof(head));tot = 0;for (i = 1; i <= n; i++) {for (j = i + 1; j <= n; j++) {if (!strcmp(like[i], dislike[j])|| !strcmp(like[j], dislike[i])) {add(i, j);add(j, i);}}}ans = n - bimatch() / 2;printf("%d\n", ans);}return 0;}


HDU 3829 cat vs dog

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.