HDU 3829 cat vs dog [maximum vertex independence set (Good Graph creation )]

Source: Internet
Author: User

Note:

N cat m dogs and K bear children

Every bear child has a favorite animal. If you like cat, you must hate dog. If you like Dag, you must hate cat.

If the animal you don't like is removed and the animal you like is not removed, the bear child will be happy.

Now let you remove some animals that make the bear child happy with the most people, output the number of people

 

Analysis:

If we find it impossible to build edges between cat and dog

Finally, the number of bear children is obtained.

It's nothing more than a bear child.

That is to say, there is a relationship between happy and unhappy bear children.

Take the bear child as a vertex

Create an edge if two bear-Child animals conflict with each other

Finally, we can find the maximum point to be independent.

 

Code:

 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <vector> 5 using namespace std; 6  7 const int maxn = 505; 8 const int INF = 1000000000; 9 10 int vis[maxn];11 int Link[maxn];12 int n;13 vector<int> G[maxn];14 int Find(int u) {15     for(int i = 0; i < G[u].size(); i++) {16         int v = G[u][i];17         if(!vis[v]) {18             vis[v] = 1;19             if(Link[v] == -1 || Find(Link[v]) ) {20                 Link[v] = u;21                 return true;22             }23         }24     }25     return false;26 }27 28 int solve() {29     memset(Link, -1, sizeof(Link));30     int cnt = 0;31     for(int i = 1; i <= n; i++) {32         if(G[i].size() ) {33             memset(vis, 0, sizeof(vis));34             if(Find(i) ) cnt++;35         }36     }37     return cnt;38 }39 40 char s1[maxn][5], s2[maxn][5];41 bool check(int i, int j) {42     if(strcmp(s1[i], s2[j]) == 0 || strcmp(s2[i], s1[j]) == 0 ) {43         return true;44     }45     return false;46 }47 48 int main() {49     int x, y;50     while(EOF != scanf("%d %d %d",&x, &y, &n) ) {51         for(int i = 1; i <= n; i++) {52             scanf("%s %s", s1[i], s2[i]);53         }54         for(int i = 1; i <= n; i++) {55             G[i].clear();56             for(int j = 1; j <= n; j++) {57                 if(i == j) continue;58                 if(check(i, j) ) {59                     G[i].push_back(j);60                 }61             }62         }63         printf("%d\n",(2 * n - solve()) / 2);64     }65     return 0;66 }
View code

 

HDU 3829 cat vs dog [maximum vertex independence set (Good Graph creation )]

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.