HDU1811Rank of Tetris 拓扑排序+并查集
来源:互联网 发布:福特基金会 知乎 编辑:程序博客网 时间:2024/06/10 16:24
自从Lele开发了Rating系统,他的Tetris事业更是如虎添翼,不久他遍把这个游戏推向了全球。
为了更好的符合那些爱好者的喜好,Lele又想了一个新点子:他将制作一个全球Tetris高手排行榜,定时更新,名堂要比福布斯富豪榜还响。关于如何排名,这个不用说都知道是根据Rating从高到低来排,如果两个人具有相同的Rating,那就按这几个人的RP从高到低来排。
看到题目中出现两者比较第一反应就是拓扑排序。题中存在Rating相同的情况,如果根据RP直接连相同Rating之间的点的边的话,哪些与原来点相邻的点也都要和新加入的点连边,这样会造成许多麻烦。
这里用并查集来储存相同Rating的点,不过在连边之前,先将相同Rating的点并到集合中,否则边连边边合并时会出现与上面相同的麻烦。
所有与集合中的点相连的边都连到集合的根部。
#include<iostream>#include<vector>#include<string.h>#include<queue>using namespace std;#define N 10000+7#define mem(arr,a) memset(arr,a,sizeof(arr))int par[N];vector<int>G[N];int indeg[N];bool flag = false;int n, m;int k;struct Edge{ int a, b; char c; void set(int x, int y, char z){ a = x, b = y, c = z; }};Edge edge[2*N];int find(int x){ if (par[x] == x)return x; return par[x] = find(par[x]);}bool unite(int a, int b){ a = find(a), b = find(b); if (a == b)return false; par[a] = b; return true;}void init(){ k = 0; flag = false; mem(indeg, 0); for (int i = 0; i < n; i++)G[i].clear(); for (int i = 0; i < n; i++)par[i] = i;}void topo(){ queue<int>q; for (int i = 0; i < n; i++){ if (par[i] == i&&indeg[i] == 0)q.push(i); } while (!q.empty()){ if (q.size() != 1)flag = true; int x = q.front(); q.pop(); for (int i = 0; i < G[x].size(); i++){ indeg[G[x][i]]--; if (indeg[G[x][i]] == 0){ q.push(G[x][i]); } } k++; } if (k < n)cout << "CONFLICT" << endl; else if (flag)cout << "UNCERTAIN" << endl; else cout << "OK" << endl;}int main(){ while (cin >> n >> m){ init(); for (int i = 0; i < m; i++){ int a, b; char c; cin >> a >> c >> b; edge[i].set(a, b, c); if (c == '='){ if (unite(a, b))k++; } } for (int i = 0; i < m; i++){ Edge&temp = edge[i]; if (temp.c == '=')continue; int x = find(temp.a); int y = find(temp.b); if (temp.c == '>'){ G[x].push_back(y); indeg[y]++; } else { G[y].push_back(x); indeg[x]++; } } topo(); }}
阅读全文
0 0
- HDU1811Rank of Tetris(并查集+拓扑排序)
- hdu1811Rank of Tetris(并查集+拓扑排序)
- hdu1811Rank of Tetris (拓扑排序+并查集)
- HDU1811Rank of Tetris 拓扑排序+并查集
- hdu 1811Rank of Tetris (并查集+拓扑排序)
- HDU 1811 Rank of Tetris(拓扑排序+并查集)
- HDU 1811 Rank of Tetris(拓扑排序+并查集)
- Rank of Tetris(hdu1811拓扑排序+并查集)
- hdu1811 Rank of Tetris (并查集+拓扑排序)
- hdu-1811-Rank of Tetris-并查集+拓扑排序
- hdu 1811 Rank of Tetris (并查集+拓扑排序)
- hdu 1811 Rank of Tetris (并查集 + 拓扑排序)
- HDU 1811 Rank of Tetris(并查集+拓扑排序)
- 【HDU】1811 Rank of Tetris 并查集+拓扑排序
- 【并查集+拓扑排序】HDU1811 Rank of Tetris
- HDU 1811-Rank of Tetris(拓扑排序+并查集)
- hdu Rank of Tetris 并查集 + 拓扑排序
- HDU 1811 Rank of Tetris(并查集+拓扑排序)
- Android Splash界面支持用户点击 直接进入主界面
- 171214之项目错误总结2
- mysql分批次插入VS一次性插入
- 图算法二之深度优先搜索
- C#,读取shp源文件 ,将要素生成JSON格式(面图层)
- HDU1811Rank of Tetris 拓扑排序+并查集
- 政府安全资讯精选 2017年第十七期 全国各地开展打击整治网络侵犯公民个人信息犯罪专项行动;中共中央办公厅、国务院办公厅印发计划 IPv6规模部署提上日程
- 谷歌浏览器按F12调试js代码的时候找不到代码在哪
- 解决Spring中singleton中注入prototype Bean的问题
- ubuntu 安装jupyter
- 第十六周java作业
- numpy.asarray: 转输入为数组
- android之带右侧字母(拼音)索引的列表
- 高精度单目结构光三维重建