PKU 1087 a plug for Unix

Source: Internet
Author: User
# Include < Iostream >
# Include < String >
# Include < Map >
# Define Vmax 600
# Define Pymax 800
Using   Namespace STD;

Map < String , Int > Item;
Map < String , Int > : Iterator it;
Int N, N2, M, K;
Int Match;
Int Map1 [Pmax] [Pmax] = { 0 }; // Association Diagram
Int MAP2 [Vmax] [Vmax] = { 0 }; // Simplified chart
Int Dev [Vmax]; // Serial number of the plug corresponding to the device
Int Path [Vmax]; // Zengguang Road
Int Link [Vmax] = { 0 }; // Match

Int Addplug ( String Plug) // Add plug type
{
It = Item. Find (plug );
If (It = Item. End ()) // If not found, add
{
Item. insert (pair < String , Int > (Plug, N2 ));
N2 ++ ;
Return N2 -   1 ;
}
Else // Returns the serial number.
Return Item [plug];
}

Int Find ( Int I) // HungaryAlgorithm
{
For ( Int J =   1 ; J <= N; j ++ )
If (MAP2 [I] [J] &&   ! Path [J]) // I connected to J & J is not on the current augmented path
{
Path [J] =   1 ;
If ( ! Link [J] | Find (link [J])
{
Link [J] = I;
Return   1 ;
}
}
Return   0 ;
}

Int Main ()
{
Int A, B;
Int I, J, K;
String Tmp1, tmp2;
 
CIN > N;
N2 = N;
For (I =   0 ; I < N; I ++ )
{
CIN > Tmp1;
Item. insert (pair < String , Int > (Tmp1, I ));
}
CIN > M;
For (I =   0 ; I < M; I ++ )
{
CIN > Tmp1 > Tmp2;
Dev [I] = Addplug (tmp2 );
}
CIN > K;
For (I =   0 ; I < K; I ++ )
{
CIN > Tmp1 > Tmp2;
A = Addplug (tmp1 ); // Otherwise, map1 [item [tmp1] [item [tmp2] = 1
B = Addplug (tmp2 );
Map1 [a] [B] =   1 ;
}
For (I =   0 ; I < N2; I ++ )
For (J =   0 ; J < N2; j ++ )
{
If (I = J)
{
Map1 [I] [I] =   1 ;
Continue ;
}
If (Map1 [J] [I])
For (K =   0 ; K < N2; k ++ )
If (Map1 [I] [k])
Map1 [J] [k] =   1 ;
}
For (I =   0 ; I < M; I ++ ) // Constructor
For (J =   0 ; J < N; j ++ )
If (Map1 [Dev [I] [J])
MAP2 [I + 1 ] [J + 1 ] =   1 ;
Match =   0 ;
For (I =   1 ; I <= M; I ++ )
{
Memset (path, 0 , Sizeof (PATH )); // Clear zengguang Road
Match + = Find (I );
}
Printf ( " % D \ n " , M - Match );
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.