ACM1811拓扑排序和并查集
/*
ACM1811 可以利用拓扑排序和并查集解决,主要方式是利用并查集在输入数据的时候将所有相等的点合并
然后将处理完的数据统一按照一个符号方向连接成有向线段,利用的是邻接矩阵;接下来把每条边都进行履历
如果出现conflict,那么就会在去除所有入度的时候无法找到新的零入度点,那么就会似的point无法减到零,
表示有conflict,如果出现uncertain,那么就会在一次寻找新的零入度点时能找到多个点,导致在队列中保存
多个数据 ;
*/
#include<iostream>
#include<queue>
using namespace std;
class EDGE
{
public:
int from,to;
char sign;
};
class WAY
{
public:
int from,to,next;
};
class DEAL
{
public:
DEAL(int n,int m)
{
edge=new EDGE[m];
way=new WAY[m];
father=new int[n];
head=new int[n];
into=new int[n];
for(int i=;i<n;i++)
{
father[i]=i;head[i]=-;
into[i]=;
}
number=n;
point=n;
side=m;
count=;
certain=true;
conflict=false;
}
~DEAL()
{
delete []edge;
delete []father;
delete []way;
delete []head;
delete []into;
}
void Input()
{
for(int i=;i<side;i++)
{
cin>>edge[i].from>>edge[i].sign>>edge[i].to;
if(edge[i].sign=='=')
{
int fa=FindFather(edge[i].from);
int fb=FindFather(edge[i].to);
Union(fa,fb);
point--;
}
}
}
void Preprocess()
{
for(int i=;i<side;i++)
{
if(edge[i].sign=='=')continue;
int fa=FindFather(edge[i].from);
int fb=FindFather(edge[i].to);
if(fa==fb)conflict=true;
if(edge[i].sign=='<')
Merge(fb,fa);
else if(edge[i].sign=='>')
Merge(fa,fb);
}
}
void Toposort()
{
for(int i=;i<number;i++)
{
if(into[i]==&&father[i]==i)//是头节点
q.push(i);
}
while(!q.empty())
{
int l=q.front();
q.pop();
point--;
if(!q.empty())certain=false;
int v;
for(v=head[l];v!=-;v=way[v].next)
{
int u=way[v].to;
into[u]--;
if(!into[u])q.push(u);
}
}
}
void Output()
{
if(point>||conflict)
cout<<"CONFLICT"<<endl;
else if(!certain)cout<<"UNCERTAIN"<<endl;
else cout<<"OK"<<endl;
}
int FindFather(int x)
{
if(x==father[x])return x;
return father[x]=FindFather(father[x]);
}
void Union(int x,int y)
{
if(x!=y)father[y]=x;//后面跟着前面
}
void Merge(int x,int y)
{
way[count].from=x;
way[count].to=y;
way[count].next=head[x];
into[y]++;
head[x]=count++;
}
private:
queue<int>q;
int number,side,point,count;
EDGE *edge;
WAY *way;
int *father,*head,*into;
bool certain,conflict;
};
int n,m;
int main()
{
while(cin>>n>>m)
{
DEAL ob(n,m);
ob.Input();
ob.Preprocess();
ob.Toposort();
ob.Output();
}
return ;
}
ACM1811拓扑排序和并查集的更多相关文章
- Rank of Tetris 杭电 拓扑排序加并查集
自从Lele开发了Rating系统,他的Tetris事业更是如虎添翼,不久他遍把这个游戏推向了全球. 为了更好的符合那些爱好者的喜好,Lele又想了一个新点子:他将制作一个全球Tetris高手排行榜, ...
- ZOJ 3521 Fairy Wars oj错误题目,计算几何,尺取法,排序二叉树,并查集 难度:2
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3521 ATTENTION:如果用long long 减小误差,这道题只能用 ...
- Luogu1155 NOIP2008双栈排序(并查集)
两个位置i和j上的元素不能被放进同一个栈的充要条件显然是存在k使i<j<k且ak<ai<aj.由此在保证合法的情况下贪心地放就是正确的了. 至于如何判断,可以记一下后缀最小值, ...
- 并查集+拓扑排序 赛码 1009 Exploration
题目传送门 /* 题意:无向图和有向图的混合图判环: 官方题解:首先对于所有的无向边,我们使用并查集将两边的点并起来,若一条边未合并之前, 两端的点已经处于同一个集合了,那么说明必定存在可行的环(因为 ...
- hdu1811 Rank of Tetris 拓扑排序+并查集
这道题是拓扑排序和并查集的综合运用. 由于排行榜是一种从高到低的排序.所以在拓扑排序的时候,如果有一次加入的入度为零的点数大于1,就有变得不确定了(UNCERTAIN). 由于只有一棵树,当树的数量大 ...
- Codeforces 336D Dima and Trap Graph 并查集
Dima and Trap Graph 枚举区间的左端点, 然后那些左端点比枚举的左端点小的都按右端点排序然后并查集去check #include<bits/stdc++.h> #defi ...
- BZOJ.1576.[Usaco2009 Jan]安全路经Travel(树形DP 并查集)
题目链接 BZOJ 洛谷 先求最短路树.考虑每一条非树边(u,v,len),设w=LCA(u,v),这条边会对w->v上的点x(x!=w)有dis[u]+dis[v]-dis[x]+len的距离 ...
- HDU1598 并查集+贪心
find the most comfortable road Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ...
- [NOI2018]归程(可持久化并查集,Kruskal重构树)
解法一: 1.首先想到离线做法:将边和询问从大到小排序,并查集维护连通块以及每个连通块中所有点到1号点的最短距离.$O(n\log n)$ 配合暴力等可以拿到75分. 2.很容易想到在线做法,使用可持 ...
随机推荐
- 洪水!(Flooded! ACM/ICPC World Final 1999,UVa815)
题目描述:竞赛入门经典的习题4-10 解题思路:1.把各个网格想象成一个数组 2.排序 3.雨水总体积去铺满 //太懒了只写了求海拔 #include <stdio.h> #define ...
- selenium常用操作方法
Webdriver中比较常用的操作元素的方法: clear() 清除输入框的默认内容 send_keys("xxx") 在一个输入框里输入xx内容 ——如果输入中文,则需要在脚本开 ...
- 使用flume抓取tomcat的日志文件下沉到kafka消费
Tomcat生产日志 Flume抓取日志下沉到kafka中 将写好的web项目打包成war包,eclise直接导出export,IDEA 在artifact中添加新的artifact-achieve项 ...
- centos7安装zabbix3.2详解
服务器端安装 1.安装仓库 rpm -ivh http://repo.zabbix.com/zabbix/3.2/rhel/7/x86_64/zabbix-release-3.2-1.el7.noar ...
- CVPR-2018 那些有趣的新想法
Taylor Guo @ Shanghai - 2018.10.18 缘起 还有什么比顶级会议更适合寻找有趣新想法的地方吗?我们从CVPR 2018 计算机视觉和模式识别的顶级会议中发现了很多有趣的东 ...
- spring 整合hibernate注解时候,出现“Unknown entity: com.ssh.entry.Admin; nested exception is org.hibernate.MappingException: Unknown entity: com.ssh.entry.Admin”异常的问题
今天学习使用ssh框架的时候,出现一个异常,弄了好久才找到,在这记录一下,我的sb错误1.spring整合hibernate,取代*.hbm.xml配置文件 在applicationContext ...
- WebApi中利用Razor模板引擎来生成html
在服务器端基于Razor来生成html的一个思路 using System.Web.Mvc; using System.IO; using System.Web.Routing; using Syst ...
- 2.hbase原理(未完待续)
hbase简介相关概念hmsterhregionserver表regionhstorememstorestorefilehfileblockcacheWALminorcompactmajorcompa ...
- ACM 第三天
A - Arpa’s hard exam and Mehrdad’s naive cheat CodeForces - 742A There exists an island called Arpa’ ...
- IT启示录
引用电影<夏洛特烦恼>中夏洛的一句话:"一直以来,我根本就不知道自己想要什么".可以说在写这篇博客之前我仍然没有考虑清楚之后的道路,即使早已明确了走游戏开发的道理,却不 ...