并查集
题意:找出给定的这些话中是否有冲突。若没有则最多有多少句是对的。


/*
思路:如果第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的更多相关文章

  1. HDU 1232 并查集/dfs

    原题: http://acm.hdu.edu.cn/showproblem.php?pid=1232 我的第一道并查集题目,刚刚学会,我是照着<啊哈算法>这本书学会的,感觉非常通俗易懂,另 ...

  2. 1021.Deepest Root (并查集+DFS树的深度)

    A graph which is connected and acyclic can be considered a tree. The height of the tree depends on t ...

  3. F2 - Spanning Tree with One Fixed Degree - 并查集+DFS

    这道题还是非常有意思的,题意很简单,就是给定一个图,和图上的双向边,要求1号节点的度(连接边的条数)等于K,求这棵树的生成树. 我们首先要解决,如何让1号节点的度时为k的呢???而且求的是生成树,意思 ...

  4. UVA208-Firetruck(并查集+dfs)

    Problem UVA208-Firetruck Accept:1733  Submit:14538 Time Limit: 3000 mSec  Problem Description The Ce ...

  5. 2018 计蒜之道复赛 贝壳找房魔法师顾问(并查集+dfs判环)

    贝壳找房在遥远的传奇境外,找到了一个强大的魔法师顾问.他有 22 串数量相同的法力水晶,每个法力水晶可能有不同的颜色.为了方便起见,可以将每串法力水晶视为一个长度不大于 10^5105,字符集不大于  ...

  6. Codeforces 455C Civilization(并查集+dfs)

    题目链接:Codeforces 455C Civilization 题目大意:给定N.M和Q,N表示有N个城市,M条已经修好的路,修好的路是不能改变的.然后是Q次操作.操作分为两种.一种是查询城市x所 ...

  7. hdu6370 并查集+dfs

    Werewolf Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total ...

  8. POJ 3728 The merchant(并查集+DFS)

    [题目链接] http://poj.org/problem?id=3728 [题目大意] 给出一棵树,每个点上都可以交易货物,现在给出某货物在不同点的价格, 问从u到v的路程中,只允许做一次买入和一次 ...

  9. 牛客练习赛16 C 任意点【并查集/DFS/建图模型】

    链接:https://www.nowcoder.com/acm/contest/84/C 来源:牛客网 题目描述 平面上有若干个点,从每个点出发,你可以往东南西北任意方向走,直到碰到另一个点,然后才可 ...

随机推荐

  1. Mac OSX的开机启动配置

    Login Items Mac OSX的当前用户成功登录后启动的程序,该类别的启动项配置文件存放在~/Library/Preferences/com.apple.loginitems.plist,所以 ...

  2. c#基础练习之if结构

    using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace if语句 ...

  3. Dynamic Pivot table wizard SQL Server

    原文 http://www.gyurcit.hu/pivot.html Dynamic Pivot table wizard This stored procedure generate dynami ...

  4. adb logcat 命令行用法

    作者 :万境绝尘  转载请著名出处 eclipse 自带的 LogCat 工具太垃圾了, 开始用 adb logcat 在终端查看日志; 1. 解析 adb logcat 的帮助信息 在命令行中输入  ...

  5. EasyUI - Tree 树组件

    效果: 数据库设计: 使用的数据: 其中的字段,是跟据要生成的树节点的属性定义的. text:代表要显示的字段名称. state:是否是目录节点. iconCls:节点的图标是什么. url:跳转的链 ...

  6. Qt for Android 部署流程分析

    原地址:http://blog.csdn.net/foruok/article/details/17796017 今天为了测试使用 Qt Creator 3.0.0 开发的纯 C 工程,利用了 Win ...

  7. Axure RP中线条的设置

    文章来源与网络 来自:非原型不设计

  8. js显示时间

    function nowTime(){ var data= new Date(); var y=data.getFullYear(); var m=parseInt(data.getMonth())+ ...

  9. Linux 静态库&动态库调用

    1.什么是库在windows平台和linux平台下都大量存在着库.本质上来说库是一种可执行代码的二进制形式,可以被操作系统载入内存执行.由于windows和linux的本质不同,因此二者库的二进制是不 ...

  10. 【Demo 0009】Android 组件(BroadcastReceiver)

    本章学习要点:        1.  了解Broadcast的作用;        2.  掌握自定义广播和系统广播的接收:        3.  掌握广播的发送: