Hdu3829_cat vs dog

Source: Internet
Author: User

The question is, given that some people like a cat or a dog, they hate a cat or a dog. How many people wish to be satisfied at most?

The question is very interesting. After modeling, it is easy.

If one cat or dog is annoying and another person prefers it, the two are connected to one side. Finally, the maximum number of independent sets is equal to the maximum number of matches ..

Orz.

 

Summon code:

 

#include <iostream>#include <cstdio>#include <cstring>#include <vector>#define maxn 505using namespace std;vector<int> likec[maxn],disc[maxn],liked[maxn],disd[maxn];vector<int> v[maxn];int n,m,p,ans,t1,t2;int f[maxn],tag[maxn];char s1[maxn],s2[maxn];void init(){ans=0;for (int i=1; i<=p; i++) v[i].clear(),f[i]=0,tag[i]=0;for (int i=1; i<=n; i++) likec[i].clear(),disc[i].clear();for (int i=1; i<=m; i++) liked[i].clear(),disd[i].clear();}int num(char S[]){int cur=0;for (int i=0; S[i]; i++) cur=cur*10+S[i]-‘0‘;return cur;}int dfs(int cur,int T){if (tag[cur]==T) return false;else tag[cur]=T;for (unsigned i=0; i<v[cur].size(); i++){if (tag[v[cur][i]]==T) continue;if (f[v[cur][i]]==0 || dfs(f[v[cur][i]],T)){f[v[cur][i]]=cur;f[cur]=v[cur][i];return true;}}return false;}int main(){while (scanf("%d%d%d",&n,&m,&p)!=EOF){init();for (int i=1; i<=p; i++){scanf("%s%s",s1,s2);t1=num(s1+1),t2=num(s2+1);if (s1[0]==‘C‘) likec[t1].push_back(i);else liked[t1].push_back(i);if (s2[0]==‘C‘) disc[t2].push_back(i);else disd[t2].push_back(i);}for (int i=1; i<=n; i++)for (unsigned x1=0; x1<likec[i].size(); x1++)for (unsigned x2=0; x2<disc[i].size(); x2++){v[likec[i][x1]].push_back(disc[i][x2]);v[disc[i][x2]].push_back(likec[i][x1]);}for (int i=1; i<=m; i++)for (unsigned x1=0; x1<liked[i].size(); x1++)for (unsigned x2=0; x2<disd[i].size(); x2++){v[liked[i][x1]].push_back(disd[i][x2]);v[disd[i][x2]].push_back(liked[i][x1]);}for (int i=1; i<=p; i++){if (f[i]!=0) continue;if (dfs(i,i)) ans++;}printf("%d\n",p-ans);}return 0;}

  

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.