離散題目17
Time Limit: 1000MS Memory Limit: 65536KB
Submit Statistic
Problem Description
給出集合X和X上的關係R,求關係R在X上的對稱閉包s(R)。
例如:
X={1,2,3,4,5} , R={<1,1>,<2,1>,<3,3>,<2,3>,<3,2>,<4,5>}
s(R)= {<1,1>,<1,2>,<2,1>,<2,3>,<3,2>,<3,3>,<4,5>,<5,4>}
Input
多組輸入,每組輸入第一行爲集合X的元素;第二行爲一個整數n ( n > 0 ),代表X上的關係R中序偶的個數;接下來n行用來描述X上的關係R,每行兩個數字,表示關係R中的一個序偶。細節參考示例輸入。
非空集合X的元素個數不大於500,每個元素的絕對值不大於2^32 - 1。
Output
每組輸入對應一行輸出,爲X上關係R的對稱閉包s(R),s(R)中的序偶根據序偶中的第一個值升序排列,如果第一個值相同則根據第二個值升序排列;具體輸出格式見樣例(注意:樣例每個逗號後有一個空格)。
Example Input
1 2 3 4 5
6
1 1
2 1
3 3
2 3
3 2
4 5
Example Output
[(1, 1), (1, 2), (2, 1), (2, 3), (3, 2), (3, 2), (3, 3), (4, 5), (5, 4)]
此代碼是抄的*
include<bits/stdc++.h>
include<algorithm>
using namespace std;
struct node
{
int b;
int h;
}k[1100];
char p[1234500];
int cmp(struct node x, struct node y)
{
if(x.b==y.b)
return x.h<y.h;
else
return x.b<y.b;
}
int main()
{
int i, t, n;
while(gets(p))
{
scanf("%d", &n);
t=n;
for(i=0; i<n; i++)
{
scanf("%d %d", &k[i].b, &k[i].h);
if(k[i].b!=k[i].h)
{
k[t].b=k[i].h;
k[t].h=k[i].b;
t++;
}
}
sort(k, k+t, cmp);
printf("[");
for(i=0; i<t; i++)
{
printf("(%d, %d)", k[i].b, k[i].h);
if(i<t-1)
printf(", ");
}
printf("]\n");
gets(p);//第二次輸入開始
}
return 0;
}