SDUT 3811 离散题目17
2017-05-24 09:44
211 查看
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)]
代码: 求对称闭包,然而这个输出样例是有问题的
正确的应该是:[(1, 1), (1, 2), (2, 1), (2, 3), (2, 3), (3, 2), (3, 2), (3, 3), (4, 5), (5, 4)]
给出集合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)]
代码: 求对称闭包,然而这个输出样例是有问题的
正确的应该是:[(1, 1), (1, 2), (2, 1), (2, 3), (2, 3), (3, 2), (3, 2), (3, 3), (4, 5), (5, 4)]
#include<bits/stdc++.h> using namespace std; struct node { int x, y; bool operator < (const node &b) const { if(x == b.x) return y < b.y; else return x < b.x; } }; vector<node> a; int main() { string s; int n, i, u, v; while(getline(cin, s)) { scanf("%d", &n); for(i = 0; i < n; i++) { scanf("%d %d", &u, &v); a.push_back((node){u, v});//存起来 } //getchar(); sort(a.begin(), a.end());//排序 n = a.size(); for(i = 0; i < n; i++) { if(a[i].x != a[i].y)//如果不相等,交换一下存入 { a.push_back((node){a[i].y, a[i].x}); } } sort(a.begin(), a.end());//排序,然后输出 printf("[(%d, %d)", a[0].x, a[0].y); for(i = 1; i < a.size(); i++) { printf(", (%d, %d)", a[i].x, a[i].y); } printf("]\n"); a.clear(); getline(cin, s);//坑点,数据后面有多余的空格 } return 0; }