Problem - 1811

  感觉这题的并查集以及拓扑排序并不难,但是做题的时候必须理解到矛盾(CONFLICT)与不确定(UNCERTAIN)直接的优先关系。

  做这题的时候,构图什么的很简单,就是没有将矛盾摆在最高优先,一旦搜到不确定的情况就立即跳转,这是不对的。

代码如下:

  1 #include <set>
  2 #include <cstdio>
  3 #include <cstring>
  4 #include <iostream>
  5 #include <algorithm>
  6
  7 using namespace std;
  8
  9 const int N = 11111;
 10 struct MFS {
 11     int fa[N];
 12     void init() { for (int i = 0; i < N; i++) fa[i] = i;}
 13     int find(int x) { return fa[x] = fa[x] == x ? x : find(fa[x]);}
 14     void merge(int x, int y) {
 15         int fx = find(x), fy = find(y);
 16         if (fx == fy) return ;
 17         fa[fx] = fy;
 18     }
 19 } mfs;
 20
 21 int x[N << 1], y[N << 1], inarc[N];
 22 bool used[N];
 23 char op[N << 1][3];
 24
 25 struct Edge {
 26     int t, nx;
 27     Edge() {}
 28     Edge(int t, int nx) : t(t), nx(nx) {}
 29 } edge[N << 1];
 30 int eh[N], ec;
 31
 32 void init() {
 33     mfs.init();
 34     memset(used, 0, sizeof(used));
 35     memset(inarc, 0, sizeof(inarc));
 36     memset(eh, -1, sizeof(eh));
 37     ec = 0;
 38 }
 39
 40 void addedge(int s, int t) {
 41     edge[ec] = Edge(t, eh[s]);
 42     eh[s] = ec++;
 43 }
 44
 45 typedef pair<int, int> PII;
 46 set<PII> nodes;
 47
 48 void input(int n, int m) {
 49     init();
 50     for (int i = 0; i < m; i++) {
 51         scanf("%d%s%d", &x[i], op[i], &y[i]);
 52         if (op[i][0] == '=') mfs.merge(x[i], y[i]);
 53         else if (op[i][0] == '<') inarc[x[i]]++;
 54         else if (op[i][0] == '>') inarc[y[i]]++;
 55     }
 56     for (int i = 0; i < n; i++) {
 57         int rt = mfs.find(i);
 58         used[rt] = true;
 59         if (rt != i) inarc[rt] += inarc[i];
 60     }
 61 //    for (int i = 0; i < n; i++) {
 62 //        if (!vis[i]) continue;
 63 //        cout << i << '~' << inarc[i] << ": ";
 64 //        for (int t = eh[i]; ~t; t = edge[t].nx) cout << edge[t].t << ' ';
 65 //        cout << endl;
 66 //    }
 67 }
 68
 69 int topo(int n, int m) {
 70     for (int i = 0; i < m; i++) {
 71         if (op[i][0] == '<') {
 72             if (mfs.fa[x[i]] == mfs.fa[y[i]]) return -2;
 73             addedge(mfs.fa[y[i]], mfs.fa[x[i]]);
 74         }
 75         if (op[i][0] == '>') {
 76             if (mfs.fa[x[i]] == mfs.fa[y[i]]) return -2;
 77             addedge(mfs.fa[x[i]], mfs.fa[y[i]]);
 78         }
 79     }
 80     nodes.clear();
 81     for (int i = 0; i < n; i++) if (used[i]) nodes.insert(PII(inarc[i], i));
 82     PII tmp;
 83     bool unc = false;
 84     while (!nodes.empty()) {
 85         tmp = *nodes.begin();
 86 //        cout << tmp.first << ' ' << tmp.second << endl;
 87         nodes.erase(nodes.begin());
 88         if (tmp.first) return -2;
 89         if (!nodes.empty() && (*nodes.begin()).first == 0) unc = true;
 90         for (int t = eh[tmp.second]; ~t; t = edge[t].nx) {
 91             int id = edge[t].t;
 92             if (inarc[id] < 0) {
 93                 cout << "shit!" << endl;
 94                 while (1) ;
 95             }
 96             nodes.erase(PII(inarc[id], id));
 97             nodes.insert(PII(--inarc[id], id));
 98         }
 99     }
100     return unc;
101 }
102
103 int main() {
104 //    freopen("in", "r" ,stdin);
105     int n, m;
106     while (~scanf("%d%d", &n, &m)) {
107         input(n, m);
108         int ans = topo(n, m);
109 //        cout << ans << endl;
110         if (ans == 0) puts("OK");
111         else if (ans == 1) puts("UNCERTAIN");
112         else puts("CONFLICT");
113     }
114     return 0;
115 }

View Code

——written by Lyon

转载于:https://www.cnblogs.com/LyonLys/p/hdu_1811_Lyon.html

hdu 1811 Rank of Tetris (并查集+拓扑排序)相关推荐

  1. HDU - 1811 Rank of Tetris 并查集 + 拓扑序 +me

    link 题意: 首先看到排名自然想到拓扑序,但是存在等于的情况,这就启发我们把等于的情况缩成一个点,让后在缩点后的图中进行拓扑即可. 对于不合法的情况当然是拓扑序没有遍历到应该遍历的点,所以只需要检 ...

  2. hdu 1811Rank of Tetris (并查集 + 拓扑排序)

    1 /* 2 题意:这些信息可能有三种情况,分别是"A > B","A = B","A < B",分别表示A的Rating高于B ...

  3. HDU 1811 Rank of Tetris(并查集按秩合并+拓扑排序)

    Rank of Tetris Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) T ...

  4. HDU 1811 Rank of Tetris

    Rank of Tetris Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...

  5. HDU——1272小希的迷宫(并查集+拓扑排序)

    小希的迷宫 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Subm ...

  6. hdu5222 Exploration【并查集+拓扑排序】

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5222 题意:有一个探险家,探险一个地方,这个地方有n个洞穴,有若干条边,有的边是有向边,有的是无向边, ...

  7. Mr. Kitayuta‘s Technology CodeForces - 505D(并查集+拓扑排序或dfs找环) 题解

    题目  Shuseki Kingdom is the world's leading nation for innovation and technology. There are n cities ...

  8. 家谱树 (并查集拓扑排序)

    目录 拓扑排序 Kahn. 最后附上Kahn的代码: 链式前向星做法: 其次是我用Kahn做的家谱树的代码(矩阵): 其次是我用Kahn做的家谱树的代码(链式前向星): [题目描述] 有个人的家族很大 ...

  9. HDOJ 1811 Rank of Tetris

    Rank of Tetris Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) T ...

最新文章

  1. html select 多选取值,下拉框多选实现 jquery-multiselect 并获取选中的值
  2. 用ABAP来实现柱状图和饼状图的输出
  3. 将用bootstrap框架的html文件转为eclipse中jsp文件
  4. 如何正确区分cssci和核心期刊
  5. 使用sql服务器发送贺卡_创建和发送免费电子贺卡的最佳网站
  6. Flash of Unstyled Content (FOUC)
  7. ArcGIS JS 学习笔记2 实现仿百度的拖拽画圆
  8. HP MSA2312 ERROR
  9. python时间序列分析——基于混沌和数据分形理论的特征构建
  10. 如何在Tomcat后台通过文件上传getshell总结(个人学习笔记思路)
  11. 读书笔记 - 《基业长青》
  12. P3975 [TJOI2015]弦论 (SAM)
  13. ONENET平台的登入与创建产品
  14. 2015-2016-1 《信息安全系统设计基础》学生博客列表
  15. 2020年最新 C# .net 面试题,月薪20K+中高级/架构师必看(五)
  16. 除了Axure,还有哪些原型设计工具
  17. CMMI3级和五级之间的区别是什么?
  18. 我的世界的服务器如何制作,《我的世界》服务器怎么做 服务器制作教程介绍...
  19. 十二月英语总结--充满热情
  20. 基于java+ssm+vue的校园卡一卡通饭卡管理系统

热门文章

  1. git merge 回退_Git项目开发必备命令
  2. java并发编程基础—生命周期与线程控制
  3. 并发编程之多线程篇之四
  4. .net版 类似火车头的网页采集
  5. .Net中简单实现发送邮件
  6. IT市场10大技术伟人 Linux之父居首(转)
  7. mysql修改最后一条记录删除第一条记录
  8. (转)对微软那棵TreeView进行试用,主要是对CheckBox进行操作
  9. 移动端placeholder不能垂直居中解决方案
  10. Python 下载依赖包环境经常失败超时解决方法