离散题目18
Time Limit: 1000MS
Memory Limit: 65536KB
Problem Description
给出一个集合A和A上的关系R,求关系R的传递闭包。
例如:
A={0,1,2} , R={<0,0>,<1,0>,<2,2>,<1,2>,<2,1>}
t(R) = {<0,0>,<1,0>,<2,2>,<2,1>,<1,2>,<1,1>,<2,0>};
Input
多组输入,输入n、m,集合A={0, 1, …, n-1 };m代表关系的数量,n、m不超过20.
Output
每组输入输出t(R),根据t(R)中序偶的第一个数字升序排序,如果第一个数字相同,根据第二个升序排序。
Example Input
3 5 0 0 1 0 2 2 1 2 2 1
Example Output
0 0 1 0 1 1 1 2 2 0 2 1 2 2
Hint
Author
1、掌握传递闭包的矩阵运算(详情参照 左孝凌版离散数学教材P124)
2、数据量小,for即可解决
3、注意各个数组的初始化
以下为AC代码
#include<bits/stdc++.h>
using namespace std;
int main()
{
int m, n;
int a[100][100];
while(cin>>m>>n)
{
int x,y;
for(int i = 0; i < n; i++)
{
cin>>x>>y;
a[x][y] = 1;
}
for(int k = 0; k < m; k++)
for(int i = 0; i < m; i++)
{
if(a[i][k] != 0)
{
for(int j = 0; j < m; j++)
{
a[i][j]+=a[k][j];
}
}
}
for(int i = 0; i < m; i++)
{
for(int j = 0; j < m; j++)
{
if(a[i][j] != 0)
cout<<i<<" "<<j<<endl;
}
}
memset(a,0,sizeof(a));
}
return 0;
}
/***************************************************
Result: Accepted
Take time: 0ms
****************************************************/