Bzoj 2744: [HEOI2012] Friends Circle binary Map Matching

Source: Internet
Author: User

2744: [HEOI2012] Friends Circle time limit:30 Sec Memory limit:128 MB
submit:612 solved:174
[Submit] [Status] [Discuss] Description in a long, long time ago, there were two countries live in harmony, carefree life. The annual appraisal conference began, as a peace in both countries, a circle of friends is always the most worthy of the respect of others, so now is the need to ask you the largest number of friends circle.         Two countries are considered to be AB and are now described by two countries: 1.         Country A: Everyone has a friendly value, when two A's friendly values A, B, if a XOR B mod 2=1, then these two people are friends, otherwise not; 2.         B Country: Everyone has a friendly value, when two B people of friendly value A, B, if a XOR B mod 2=0 or (a or B) into the binary has an odd number of 1, then two is a friend, otherwise is not a friend; 3. A, b people between the two may also be friends, the data will give a, B "friends" situation.


4. InABThe definition of the circle of Friends: a circle of friendsSMeet

S∈a∪b , for all the I,j∈s,i and J are friends

Because of the backwardness of ancient times, without computers this has become the biggest problem of the year, and you can help them find the largest number of friends circle?

Input

The first line t<=6, which represents the total number of input data. Next T data: The first line input three integer a,b,m, indicating the number of country A, the number of B countries, AB is a friend of the logarithm; the second line a number AI, which represents the friendly value of the first person of country A; the third row b number Bi, which represents the friendly value of the person B J, 4--3+m Line, two integers per line j), which indicates that the people of the first I and the first J B are friends. Output output t lines, each line, outputs an integer that represents the maximum number of friend circles. Sample Input2 4 7
1 2
2 6 5 4
1 1
1 2
1 3
2 1
2 2
2 3
2 4
Sample OUTPUT5
"Sample description"
The largest circle of friends contains the 1th, 2 and B states 1th, 2 and 3 in country A.
HINT

"Data Range"

Two types of data

First Class: | a|<=200 | b| <= 200

Type II: | a| <= 10 | b| <= 3000

To be honest, I'm not going to be right about this data range, but this problem, enumerate a over the points (up to two), to B country run Hungary is can pass, just remind a pit: The question person looks like special card Hungary, if the tle dropped a try to change a collection call find function. I called find (i) TLE when I was b[i]%2==0, and then I switched to b[i]%2==1.

#include <iostream>#include<cstring>#include<algorithm>#include<cstdio>#include<vector>using namespacestd;#defineMAXN 3100#defineMAXV MAXN#defineMaxe 11000000structedge{intNP; Edge*Next;} E[maxe],*V[MAXV];inttope=-1;voidAddedge (intXinty) {e[++tope].np=y; E[tope].next=V[x]; V[X]=&E[tope];} Vector<int>BLST[MAXN];intA[MAXN],B[MAXN];intVis[maxn],vistime;intPTR[MAXN];BOOLFindintNow ) {        if(Vis[now]==vistime)return false; Vis[now]=Vistime; Edge*NE;  for(ne=v[now];ne;ne=ne->next) {                if(ptr[ne->np]==-1|| Find (ptr[ne->NP])) {Ptr[ne->np]=Now ; return true; }        }        return false;}intWork (vector<int>seq) {         for(intI=0; I<seq.size (); i++) ptr[seq[i]]=-1;  for(intI=0; I<seq.size (); i++)        {                 for(intj=i+1; J<seq.size (); j + +)                {                        if((B[seq[i]]^b[seq[j]) &1) &&!__builtin_parity (b[seq[i]]|B[seq[j]))                                {Addedge (seq[i],seq[j]);                        Addedge (Seq[j],seq[i]); }                }        }        intret=0;  for(intI=0; I<seq.size (); i++)        {                if((b[seq[i]]&1))                {                        ++Vistime; RET+=find (Seq[i]); }} RET=seq.size ()-ret;  for(intI=0; I<seq.size (); i++) v[seq[i]]=0; tope=-1; returnret;}intans=0;intMain () {Freopen ("Input.txt","R", stdin); Freopen ("output.txt","W", stdout); intn,m,q; intx, y; Vector<int>seq; scanf ("%d%d",&n,&m); scanf ("%d",&q);  for(intI=1; i<=n;i++) scanf ("%d", A +i);  for(intI=1; i<=m;i++) scanf ("%d", B +i);  for(intI=1; i<=n;i++) blst[i].clear ();  for(intI=1; i<=q;i++) {scanf ("%d%d",&x,&y);        Blst[x].push_back (y); }         for(intI=1; i<=n;i++) {sort (Blst[i].begin (), Blst[i].end ());        Blst[i].erase (Unique (Blst[i].begin (), Blst[i].end ()), Blst[i].end ()); }        //Nothing in A         for(intI=1; i<=m;i++) Seq.push_back (i); Ans=max (ans,work (seq));        Seq.clear ();  for(intI=1; i<=n;i++)        {                 for(intj=0; J<blst[i].size (); j + +) Seq.push_back (Blst[i][j]); if(Seq.size () +1>ans) ans=max (ans,work (seq) +1);        Seq.clear (); }         for(intI=1; i<=n;i++)        {                 for(intj=i+1; j<=n;j++)                {                        if(! ((A[i]^a[j]) &1))Continue; Vector<int>:: Iterator it1,it2; It1=Blst[i].begin (); It2=Blst[j].begin ();  while(It1!=blst[i].end () && it2!=Blst[j].end ()) {                                if(*it1==*it2) {Seq.push_back (*it1); It1++,it2++; }Else if(*it1<*it2) {it1++; }Else{it2++; }                        }                        if(Seq.size () +2>ans) ans=max (ans,work (seq) +2);                Seq.clear (); }} printf ("%d\n", ans);}

Bzoj 2744: [HEOI2012] Friends Circle binary Map Matching

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.