离散题目17
来源:互联网 发布:淘宝页头背景图 编辑:程序博客网 时间:2024/06/08 09:47
离散题目17
Time Limit: 1000MS Memory Limit: 65536KB
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)]
Hint
Author
#include <bits/stdc++.h>using namespace std;struct node{ int x; int y; int num;}T[121211];int cmp(const void * a, const void * b){ struct node * c = (struct node *)a; struct node * d = (struct node *)b; if(c->x!=d->x) return (c->x-d->x); if(c->y!=d->y) return (c->y-d->y); return (c->num-d->num);}set<int> a;int main(){ string ss, buf; int t, x, y, n; while(getline(cin, ss)) { int top = 0; stringstream cs(ss); while(cs>>buf) { sscanf(buf.c_str(), "%d", &t); a.insert(t); } cin>>n; for(int i=0;i<n;i++) { cin>>x>>y; if(a.count(x)&&a.count(y)) { T[top].x = x; T[top].y = y; T[top].num = top; top++; if(T[top-1].x!=T[top-1].y) { T[top].x = T[top-1].y; T[top].y = T[top-1].x; T[top].num = top; top++; } } } qsort(&T[0], top, sizeof(T[0]), cmp); if(top>0) { cout<<"[("<<T[0].x<<", "<<T[0].y<<")"; for(int i=1;i<top;i++) { printf(", (%d, %d)", T[i].x, T[i].y); } cout<<"]"<<endl; } a.clear(); getline(cin, ss, '\n'); } return 0;}
阅读全文
0 0
- 离散题目17
- 离散题目17
- 离散题目17
- 离散题目17
- 离散题目17
- SDUT-离散题目17
- 3811-->>离散题目17
- SDUT 3811 离散题目17
- 离散题目17--求对称闭包
- 离散题目17 对称闭包
- 离散题目3
- 离散题目7
- sdut离散题目11
- sdut离散题目13
- sdut离散题目14
- 离散题目12
- 离散题目15
- 3799-->离散题目5
- 06、vue.js 之表单控件绑定
- (五) Java多线程详解之Callable和Future阻塞获取线程返回结果
- 神经网络中epoch, iteration, batchsize相关理解和说明
- 欢迎使用CSDN-markdown编辑器
- 最长公共子序列
- 离散题目17
- cocos2dx 植物大战僵尸 2 创建卡片层
- oracle查看执行计划
- Entity Framework Core 创建表之间外键的删除级联效果
- 打印图形
- RobotFramework之DateTime
- windows下解决numpy, scipy等库安装失败的方法
- 【JavaSE系列-基础篇5】——面向对象编程概念之包
- LeetCode:537. Complex Number Multiplication