1199 problem B: size relationship

Source: Internet
Author: User

Floyd Warshall algorithm for finite set transitive closure (matrix implementation)

In fact, the triple loop.
Zzuoj 1199 Questions Link http://acm.zzu.edu.cn:8000/problem.php?id=1199
Problem B: Size relationshipTime limit:2 Sec Memory limit:128 MB
submit:148 solved:31
[submit][status][Web Board] Description

When we know a set of size relationships, we can tell if all relationships can be established, that is, there is no contradiction between relationships.

For example: A<b, A<c, b<c through this group of relations we can get a<b<c, all relations are established, there is no contradiction.

If a<b, B<c, c<a through the first two relations we get a<b<c, this relationship and c<a contradiction, all relations can not be set up at the same time.

Now that we know the M relationship, please determine whether the M relationship can be set up, set the output "YES", otherwise output "NO".

Input

Multiple sets of data, each set of data is as follows:

The first line has a number m. M represents the M-group relationship (1<=M<=400), and the next M-line has a relationship, denoted by two different letters and a symbol. (Enter a guaranteed letter between ' A '-' Z ', the relationship symbol is only >, <)

Output

Output "YES" or "NO" for each set of data.

Sample Input3a<ba<cb<c3a<bb<cC<aSample Output

YES

NO

/********************************************************* *************************************************** ***********/

Greater than and less than is actually a relationship, that is, the relationship between two elements, B>a can be described by a < B.

The question given is the relationship between the 26 letters, we can use 0 to 25 of the integer corresponding to A to Z, a two-dimensional array to express the relationship between elements, such as Re[0][25]==1 is 0 < 25, that is, a < Z. All that remains is the process of reasoning.

People who have studied discrete mathematics (Shanghai Science and Technology literature) know that there is a property called transitivity in the nature of the relationship in set theory, and that the relationship between < is exactly the same, and of course, less than satisfying reflexivity and symmetry is the key to our contradiction.

In the closure operation of the relationship, there is a closure operation called the transitive closure r+ of the relationship R, and this r+ is passed, that is to say, if a < b and B < C are found in r+, a < C will be found, that is to say r+ is reasoning After the relationship is obtained.

The algorithm for finding r+:

for (i=0; i<26;++i) {
for (J=0;J<26;++J) {
if (re[j] [i]) {
for (K=0;k<26;++k) {
Re[j] [K] +=re[I [K];
}
}
}
}

That's all, thanks Warshall.

Incidentally, this Floyd Warshall and graph theory inside of the classic Floyd algorithm is a person, in fact, this problem can also be completely in the form of graph theory to do

/***************************************/

AC Code:

# include <iostream>
# include <string.h>
# include <stdlib.h>
# include <math.h>
# include <stdio.h>
# include <algorithm>
# include <stack>

int m,re[27 [27];
Char a,b,c,in[4];

int main () {
using namespace Std;
int i,j,k;
while (~SCANF ("%d", &m)) {

memset (re,0,sizeof (re));

for (i=0; i<m; ++i) {
scanf ("%s", in);
a=in[0];
c=in[1];
b=in[2];

if (c== ' < ') {
re[A-' a ' [B ' a ']=1;
}else if (c== ' > ') {
re[b ' A ' [a ' a ']=1;
}
}



for (i=0; i<26;++i) {
for (J=0;J&LT;26;++J) {
if (re[j] [i]) {
for (K=0;k<26;++k) {
Re[j] [k]+=re[i] [K];
}
}
}
}

for (I=0;i<26;++i) {
for (J=0;J&LT;26;++J)
if (re[I [J]&&re[J] [i]) {
cout << "no\n";
Goto KKK;
}
}

cout << "yes\n";
KKK:;
}

return 0;
}

Reward

1199 problem B: size relationship

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.