HDU 1811 并查集
题意:
思路:topo+并查集
#include <cstdio>
#include <vector>
#include <algorithm>
#include <queue>
#include <iostream>
using namespace std;
vector<int > web[];
int p[],n,m,sum,f[],A[],B[];
char C[];
bool ok;
int find(int x) {return p[x] == x ? x : p[x] = find(p[x]);}
void order(){
queue<int> q;
for ( int i = ; i < n ; ++i)
if ( f[i] == && find(i) == i )
q.push(i);
while(!q.empty()){
if ( q.size() > ) ok=true;///否则说明信息不完全
int cur = q.front();
q.pop();
sum--;
for(int i = ; i < web[cur].size() ; ++i)
{
if(--f[web[cur][i]]==)
q.push(web[cur][i]);
}
}
return ;
}
int main(){
while (scanf("%d %d",&n,&m) != EOF){
ok = false ;sum = n;
for (int i = ;i <= n;++i){
p[i] = i;
web[i].clear();
f[i] = ;
}
for (int i = ;i < m; ++i){
scanf("%d %c %d",&A[i],&C[i],&B[i]);
int x = find(A[i]),y = find(B[i]);
if (C[i] == '='){
if (x != y){
p[y] = x;
sum--;
}
}
}
for (int i = ;i < m;++i){
if (C[i] == '=') continue;
int x = find(A[i]);
int y = find(B[i]);
if (C[i] == '>'){
web[x].push_back(y);
f[y]++;
}
else {
web[y].push_back(x);
f[x]++;
}
}
order();
if (sum > ) printf("CONFLICT\n");
else if (ok) printf("UNCERTAIN\n");
else printf("OK\n");
}
return ;
}
HDU 1811 并查集的更多相关文章
- HDU 1811 并查集+拓扑排序
Rank of Tetris 题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1811 Problem Description 自从Lele开发了Rati ...
- hdu 4514 并查集+树形dp
湫湫系列故事——设计风景线 Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Tot ...
- HDU 3926 并查集 图同构简单判断 STL
给出两个图,问你是不是同构的... 直接通过并查集建图,暴力用SET判断下子节点个数就行了. /** @Date : 2017-09-22 16:13:42 * @FileName: HDU 3926 ...
- HDU 4496 并查集 逆向思维
给你n个点m条边,保证已经是个连通图,问每次按顺序去掉给定的一条边,当前的连通块数量. 与其正过来思考当前这边会不会是桥,不如倒过来在n个点即n个连通块下建图,检查其连通性,就能知道个数了 /** @ ...
- HDU 1232 并查集/dfs
原题: http://acm.hdu.edu.cn/showproblem.php?pid=1232 我的第一道并查集题目,刚刚学会,我是照着<啊哈算法>这本书学会的,感觉非常通俗易懂,另 ...
- HDU 2860 并查集
http://acm.hdu.edu.cn/showproblem.php?pid=2860 n个旅,k个兵,m条指令 AP 让战斗力为x的加入y旅 MG x旅y旅合并为x旅 GT 报告x旅的战斗力 ...
- hdu 1198 (并查集 or dfs) Farm Irrigation
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1198 有题目图11种土地块,块中的绿色线条为土地块中修好的水渠,现在一片土地由上述的各种土地块组成,需要浇 ...
- hdu 1598 (并查集加贪心) 速度与激情
题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1598 一道带有贪心思想的并查集 所以说像二分,贪心这类基础的要掌握的很扎实才行. 用结构体数组储存公 ...
- hdu 4496(并查集)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4496. 思路:简单并查集应用,从后往前算就可以了. #include<iostream> ...
随机推荐
- len字符串的长度
#!/usr/bin/env python def fun4(x) : if len(x) > 2 : return print(x[0],x[1]) else: return 0 d = (' ...
- Cheatsheet: 2015 10.01 ~ 10.31
.NET Publishing your ASP.NET App to Linux in 5 minutes with Docker Integrating AngularJS with ASP.NE ...
- centos jenkins
wget -O /etc/yum.repos.d/jenkins.repo http://pkg.jenkins-ci.org/redhat/jenkins.repo rpm --import htt ...
- 从ASP了解Http Buffer
he Buffer property specifies whether to buffer the output or not. When the output is buffered, the s ...
- 根据多年经验整理的《互联网MySQL开发规范》
一.基础规范 使用 INNODB 存储引擎 表字符集使用 UTF8 所有表都需要添加注释 单表数据量建议控制在 5000W 以内 不在数据库中存储图⽚.文件等大数据 禁止在线上做数据库压力测试 禁⽌ ...
- mysql局域网连接
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%'IDENTIFIED BY '123456' WITH GRANT OPTION; //从任何主机连接到mysql服务 ...
- java高薪之路__004_泛型
参考地址: 1. http://www.cnblogs.com/lwbqqyumidi/p/3837629.html2. http://www.cnblogs.com/abcwt112/p/47350 ...
- $.ajax()常用方法详解(推荐)
AJAX 是一种与服务器交换数据的技术,可以在补充在整个页面的情况下更新网页的一部分.接下来通过本文给大家介绍ajax一些常用方法,大家有需要可以一起学习. 1.url: 要求为String类型的参数 ...
- nginx location配置
nginx location配置 location在nginx中起着重要作用,对nginx接收到的请求字符串进行处理,如地址定向.数据缓存.应答控制.代理转发等location语法location ...
- zabbix3.0.4 部署之四 (LNAP > PHP安装)
1.安装依赖 安装epel-release源 安装 libiconv-1.14.tar.gz (这个还有个devl包) libmcrypt-2.5.8.tar.gz mhash-0.9.9.9. ...