POJ1291-并查集/dfs
并查集
题意:找出给定的这些话中是否有冲突。若没有则最多有多少句是对的。
/*
思路:如果第x句说y是对的,则x,y必定是一起的,x+n,y+n是一起的;反之x,y+n//y,x+n是一起的。
利用并查集判断 x 和 x+n 是否在同一集合。
至于查找最多正确的话,对这些 “小树” 进行dfs即可。
*/
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<algorithm>
#include<iostream>
#include<queue>
#include<map>
#include<stack>
#include<set>
#include<math.h>
using namespace std;
typedef long long int64;
//typedef __int64 int64;
typedef pair<int64,int64> PII;
#define MP(a,b) make_pair((a),(b))
const int maxn = 2005;
const int inf = 0x7fffffff;
const double pi=acos(-1.0);
const double eps = 1e-8; int fa[ maxn ];
int rank[ maxn ];
struct Edge{
int u,v,next;
}edge[ maxn<<1 ];
int cnt,head[ maxn ];
int vis[ maxn ];
int Cnt_true,Cnt_false; void init( int n ){
cnt = 0;
//Cnt_true = Cnt_false = 0;
memset( head,-1,sizeof( head ) ) ;
for( int i=1;i<=n;i++ ){
fa[ i ] = i;
rank[ i ] = 1;
vis[ i ] = 0;
}
} void addedge( int a,int b ){
edge[ cnt ].u = a;
edge[ cnt ].v = b;
edge[ cnt ].next = head[ a ];
head[ a ] = cnt ++ ; edge[ cnt ].u = b;
edge[ cnt ].v = a;
edge[ cnt ].next = head[ b ];
head[ b ] = cnt ++ ;
} int find( int x ){
if( x==fa[x] )
return x;
return fa[x] = find( fa[x] );
} void union_ab( int x,int y ){
int fax = find( x );
int fay = find( y );
if( rank[ fax ]>rank[ fay ] ){
fa[ fay ] = fax;
rank[ fax ] += rank[ fay ];
}
else {
fa[ fax ] = fay;
rank[ fay ] += rank[ fax ];
}
} void dfs( int x,int n ){
vis[ x ] = 1;
if( x<=n ){
vis[ x+n ] = 1;
Cnt_true ++ ;
//若x是正确的,则x+n则是错误的,同时也不用再去访问。
}
else {
vis[ x-n ] = 1;
Cnt_false ++ ;
}
for( int i=head[x];i!=-1;i=edge[i].next ){
int y = edge[i].v;
if( !vis[y] ){
dfs( y,n );
}
}
} int main(){
int n;
while( scanf("%d",&n)==1,n ){
init( n*2 );
bool f = true;
char s1[ 24 ],s2[ 24 ],s3[ 24 ];
int x1,y1,x2,y2;
int fax,fay;
for( int i=1;i<=n;i++ ){
scanf("%s%d%s%s",s1,&y1,s2,s3);
x1 = i,x2 = i+n;
y1 = y1,y2 = y1+n;
//x1表示true,x2表示false
if( f==false ) continue;
if( s3[0]=='f' ){
fax = find( x1 );
fay = find( y1 );
if( fax==fay ){
f = false;
continue;
}
fax = find( x2 );
fay = find( y2 );
if( fax==fay ){
f = false;
continue;
}
union_ab( x1,y2 );
union_ab( x2,y1 );
addedge( x1,y2 );
addedge( x2,y1 );
}
else { fax = find( x1 );
fay = find( y2 );
if( fax==fay ){
f = false;
continue;
}
fax = find( x2 );
fay = find( y1 );
if( fax==fay ){
f = false;
continue;
} union_ab( x1,y1 );
union_ab( x2,y2 );
addedge( x1,y1 );
addedge( x2,y2 );
}
}
if( f==false ) {
puts("Inconsistent");
continue;
}//specail judge
for( int i=1;i<=n;i++ ){
if( find(i)==find(i+n) ){
f = false;
break;
}
}
if( f==false ) {
puts("Inconsistent");
continue;
}
int ans = 0;
for( int i=1;i<=2*n;i++ ){
if( !vis[i] ){
Cnt_false = Cnt_true = 0;
dfs( i,n );
ans += max( Cnt_true,Cnt_false );
}
}//find the max
printf("%d\n",ans );
}
return 0;
}
POJ1291-并查集/dfs的更多相关文章
- HDU 1232 并查集/dfs
原题: http://acm.hdu.edu.cn/showproblem.php?pid=1232 我的第一道并查集题目,刚刚学会,我是照着<啊哈算法>这本书学会的,感觉非常通俗易懂,另 ...
- 1021.Deepest Root (并查集+DFS树的深度)
A graph which is connected and acyclic can be considered a tree. The height of the tree depends on t ...
- F2 - Spanning Tree with One Fixed Degree - 并查集+DFS
这道题还是非常有意思的,题意很简单,就是给定一个图,和图上的双向边,要求1号节点的度(连接边的条数)等于K,求这棵树的生成树. 我们首先要解决,如何让1号节点的度时为k的呢???而且求的是生成树,意思 ...
- UVA208-Firetruck(并查集+dfs)
Problem UVA208-Firetruck Accept:1733 Submit:14538 Time Limit: 3000 mSec Problem Description The Ce ...
- 2018 计蒜之道复赛 贝壳找房魔法师顾问(并查集+dfs判环)
贝壳找房在遥远的传奇境外,找到了一个强大的魔法师顾问.他有 22 串数量相同的法力水晶,每个法力水晶可能有不同的颜色.为了方便起见,可以将每串法力水晶视为一个长度不大于 10^5105,字符集不大于 ...
- Codeforces 455C Civilization(并查集+dfs)
题目链接:Codeforces 455C Civilization 题目大意:给定N.M和Q,N表示有N个城市,M条已经修好的路,修好的路是不能改变的.然后是Q次操作.操作分为两种.一种是查询城市x所 ...
- hdu6370 并查集+dfs
Werewolf Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total ...
- POJ 3728 The merchant(并查集+DFS)
[题目链接] http://poj.org/problem?id=3728 [题目大意] 给出一棵树,每个点上都可以交易货物,现在给出某货物在不同点的价格, 问从u到v的路程中,只允许做一次买入和一次 ...
- 牛客练习赛16 C 任意点【并查集/DFS/建图模型】
链接:https://www.nowcoder.com/acm/contest/84/C 来源:牛客网 题目描述 平面上有若干个点,从每个点出发,你可以往东南西北任意方向走,直到碰到另一个点,然后才可 ...
随机推荐
- 转:c语言EOF是什么?(及getchar()和putchar用法)
我学习C语言的时候,遇到的一个问题就是EOF. 它是end of file的缩写,表示"文字流"(stream)的结尾.这里的"文字流",可以是文件(file) ...
- boost:regex分割字符串(带有'\'字符) - zzusimon的专栏 - 博客频道 - CSDN.NET
boost:regex分割字符串(带有'\'字符) - zzusimon的专栏 - 博客频道 - CSDN.NET boost:regex分割字符串(带有'\'字符) 分类: C++ 2011-08- ...
- Swift学习之UI开发初探
Swift是供iOS和OS X应用编程的新编程语言.相信很多开发者都在学习这门新语言.废话不多说,下面我就来学习使用Swift创建一个简单的UI应用程序. AD: 概述 Apple近日发布了Swift ...
- 终于懂了:WM_PAINT中应该用BeginPaint与EndPaint这两个api,它们的功能正是使无效区域恢复(所以WM_PAINT里即使什么都不做,也必须写上BeginPaint与EndPaint)——Delphi里WM_PAINT消息的三个走向都做到了这一点 good
程序本来是想实现鼠标单击改变背景颜色.可是,程序运行时,为什么没有任何消息触发,背景颜色就一直不断的改变了?WM_PAINT怎么被触发的 #include <windows.h> #inc ...
- <context-param>与<init-param>的区别与作用(转)
<context-param>的作用:web.xml的配置中<context-param>配置作用1. 启动一个WEB项目的时候,容器(如:Tomcat)会去读它的配置文件we ...
- [Oracle]TRIGGER
题外话: Oracle 的使用. 以前客户方用的是SQL SERVER,一直在用SQL SERVER,感觉SQL SERVER的用户体验非常好. 不管是开发环境的界面布局到SQL 的写法上,感觉写起来 ...
- 【虚拟化实战】容灾设计之一VR vs SRM
作者:范军 (Frank Fan) 新浪微博:@frankfan7 从本文开始,我们将介绍一系列的关于容灾的解决方案.先探讨应用的场景,然后再深入介绍技术架构. 情景一: 某小型公司的虚拟化环境中,在 ...
- 在C++工程中main函数之前跑代码的廉价方法(使用全局变量和全局函数)
// test.cpp : Defines the entry point for the console application. // #include "stdafx.h" ...
- jssdk微信图片上传功能
/*wx.config({ debug: false, appId: data.appid, timestamp: data.timestamp, nonceStr: data.nonceStr, s ...
- jqueryui datepicker refresh
http://stackoverflow.com/questions/6056287/jquery-ui-datepicker-prevent-refresh-onselect 给选中的TD加背景色