ZOJ-1589 Professor John
2013-05-12 09:54
274 查看
Professor John
Time Limit: 2 Seconds Memory Limit: 65536 KB
Sunny Cup 2003 - Preliminary Round
April 20th, 12:00 - 17:00
Problem H: Professor John
Professor John is investigating a math problem. He has obtained some relations among several variables. Now he would like to know if there are any other relations that can be deduced from these obtained ones. Since he has been working for too long, Professor John decides to grant himself a vacation while assigning you to do the job. Are you ready?
Input
The first line of input contains an integer N, which is the number of test cases. Then N test cases follow.
For each test case:
the 1st line contains a positive integer m (<= 100) which is the number of given relations;
the following m lines each contains a given relation, in the format
Variable1<Variable2
or
Variable1>Variable2
A "Variable" is represented by a capital character. There will not be conflicting relations given.
Output
For each test case, first print in one line "Case d:" where d is the number of the test case, start counting from 1.
Then output all the relations which can be deduced from the given relations in alphabetical order, in the format Variable1<Variable2. Each relation occupies one line. No extra space shall be printed. The given relations must NOT be included.
If no new relation is found, output "NONE" in one line.
Sample Input
2
3
A<B
C>B
C<D
2
A<B
C<D
Sample Output
Case 1:
A<C
A<D
B<D
Case 2:
NONE
通过Floyd算法求闭包,再判断
#include <stdio.h>
#include <string.h>
int main()
{
int N, m, map[26][26];
int i, j, k, count = 0, flag;
char str[5];
scanf("%d", &N);
while(N--) {
count++;
scanf("%d", &m);
for(i = 0; i < 26; i++) {
for(j = 0; j < 26; j++) {
map[i][j] = 0;
}
}
for(i = 0; i < m; i++) {
scanf("%s", str);
if(str[1] == '>') {
map[str[0]-'A'][str[2]-'A'] = 1;
} else {
map[str[2]-'A'][str[0]-'A'] = 1;
}
}
for(k = 0; k < 26; k++) {
for(i = 0; i < 26; i++) {
for(j = 0; j < 26; j++) {
if(map[i][j] == 0) {
if(map[i][k] && map[k][j]) {
map[i][j] = 2;
}
}
}
}
}
printf("Case %d:\n", count);
flag = 1;
for(i = 0; i < 26; i++) {
for(j = 0; j < 26; j++) {
if(map[j][i] == 2) {
printf("%c<%c\n", i+'A', j+'A');
flag = 0;
}
}
}
if(flag) {
printf("NONE\n");
}
}
return 0;
}
Time Limit: 2 Seconds Memory Limit: 65536 KB
Sunny Cup 2003 - Preliminary Round
April 20th, 12:00 - 17:00
Problem H: Professor John
Professor John is investigating a math problem. He has obtained some relations among several variables. Now he would like to know if there are any other relations that can be deduced from these obtained ones. Since he has been working for too long, Professor John decides to grant himself a vacation while assigning you to do the job. Are you ready?
Input
The first line of input contains an integer N, which is the number of test cases. Then N test cases follow.
For each test case:
the 1st line contains a positive integer m (<= 100) which is the number of given relations;
the following m lines each contains a given relation, in the format
Variable1<Variable2
or
Variable1>Variable2
A "Variable" is represented by a capital character. There will not be conflicting relations given.
Output
For each test case, first print in one line "Case d:" where d is the number of the test case, start counting from 1.
Then output all the relations which can be deduced from the given relations in alphabetical order, in the format Variable1<Variable2. Each relation occupies one line. No extra space shall be printed. The given relations must NOT be included.
If no new relation is found, output "NONE" in one line.
Sample Input
2
3
A<B
C>B
C<D
2
A<B
C<D
Sample Output
Case 1:
A<C
A<D
B<D
Case 2:
NONE
通过Floyd算法求闭包,再判断
#include <stdio.h>
#include <string.h>
int main()
{
int N, m, map[26][26];
int i, j, k, count = 0, flag;
char str[5];
scanf("%d", &N);
while(N--) {
count++;
scanf("%d", &m);
for(i = 0; i < 26; i++) {
for(j = 0; j < 26; j++) {
map[i][j] = 0;
}
}
for(i = 0; i < m; i++) {
scanf("%s", str);
if(str[1] == '>') {
map[str[0]-'A'][str[2]-'A'] = 1;
} else {
map[str[2]-'A'][str[0]-'A'] = 1;
}
}
for(k = 0; k < 26; k++) {
for(i = 0; i < 26; i++) {
for(j = 0; j < 26; j++) {
if(map[i][j] == 0) {
if(map[i][k] && map[k][j]) {
map[i][j] = 2;
}
}
}
}
}
printf("Case %d:\n", count);
flag = 1;
for(i = 0; i < 26; i++) {
for(j = 0; j < 26; j++) {
if(map[j][i] == 2) {
printf("%c<%c\n", i+'A', j+'A');
flag = 0;
}
}
}
if(flag) {
printf("NONE\n");
}
}
return 0;
}
相关文章推荐
- zoj 1589 Professor John(Dijkstra~)
- zoj 1589 Professor John
- ZOJ 1589 Professor John (图的传递闭包)
- ZOJ 1589 Professor John(传递闭包)
- ZOJ 1589 Professor John
- ZOJ-1589
- ZOJ 1589 Professor John【暴力解决】【待改进】
- ZJU 1589 Professor John
- zoj 1589
- zoj 1589 Professor John 尚有点小疑问
- zoj 1589 传递闭包
- TOJ 2933 ZOJ 1589 Professor John / floyd
- zoj1081判断点是否在多边形内
- ZOJ-1174
- UVA 1589 Xiangqi(仔细的模拟)
- zoj 3826 Hierarchical Notation(hash+dfs)
- zoj 2626 Polygon Game
- Treasure Hunt I - ZOJ 3626 树形dp
- zoj 1654 Place the Robots 二分图匹配DFS实现
- ZOJ Problem Set - 1649 - Rescue