HDU 3829 Cat VS Dog

Source: Internet
Author: User

Question:

P. Each person has a favorite and a nasty animal. Suppose the animals selected include the animals that this person prefers, but not the animals he hates at the same time. Then this person will be happy to ask a few people at most.

Ideas:

In a bipartite graph, the largest independent set 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 the actual name... 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 LL; # define N 510 struct 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", & I, & j, & n) {for (I = 1; I <= n; I ++) scanf ("% 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.