Zoj-1015-fishing net [String chart Judgment]

Source: Internet
Author: User

Algorithm: first re-number the graph, and then check according to the new number


[Cpp]
# Include <cstdio>
# Include <cstring>
Using namespace std;
Int n, m;
Bool g [1024] [1024], used [1024];
Int lable [1024], set [1024]; // The new number of the lable storage graph and the number calculated by the set storage string graph.
 
Void Relable (){
Memset (used, false, sizeof (used ));
Used [1] = true;
For (int num = n-1; num> 0; num --){
Memset (lable, 0, sizeof (lable ));
For (int I = 1; I <= n; I ++ ){
If (! Used [I]) {
For (int j = 1; j <= n-num; j ++ ){
If (g [I] [set [n-j + 1])
Lable [I] ++;
}
}
}
Int maxv = 0, max;
For (int I = 1; I <= n; I ++ ){
If (lable [I]> maxv ){
Maxv = lable [I];
Max = I;
}
}
Set [num] = max;
Used [max] = true;
}
 
}
Bool check (){
Int temp [1024];
For (int I = 1; I <= n; I ++ ){
Memset (temp, 0, sizeof (temp ));
Int t = 0;
For (int j = I + 1; j <= n; j ++ ){
If (g [set [I] [set [j]) {
T ++;
Temp [t] = set [j];
}
}
For (int j = 2; j <= t; j ++ ){
If (! G [temp [j] [temp [1])
Return false;
}
}
Return true;
}
Int main (){
 
While (scanf ("% d", & n, & m) = 2 ){
If (n = 0 & m = 0) break;
Memset (g, false, sizeof (g ));
 
Int a, B;
For (int I = 0; I <m; I ++ ){
Scanf ("% d", & a, & B );
G [a] [B] = g [B] [a] = true;
}
Set [n] = 1;
Relable ();
If (check () printf ("Perfect \ n ");
Else printf ("Imperfect \ n ");
Printf ("\ n ");
}
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.