【usaco-Liars and Truth Tellers, 2013 Jan真假奶牛】并查集
题解:
原先我看错题了,以为是任意选择k个使得它们不矛盾。
这样的话怎么做呢?我想M^2判断,把它们分成若干个集合,集合里面两两不矛盾这个集合里所有的话就不矛盾了。
但是这样是错的。为什么呢?
每一句话实质上都是说明了某两个点同真假或者不同真假。并非两两不矛盾它们就不矛盾,可能是合起来才矛盾。
后来发现题目说的是前k个。。
那么我们就可以像上图一样连边,用并查集判断,如果一个点为真或为假时它的祖先相同,则矛盾了。
代码:
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
#include<cmath>
#include<set>
using namespace std; const int N=*,M=;
int n,m,fa[N];
char s[]; int findfa(int x)
{
if(fa[x]==x) return x;
return findfa(fa[x]);
} int main()
{
// freopen("a.in","r",stdin);
freopen("truth.in","r",stdin);
freopen("truth.out","w",stdout);
scanf("%d%d",&n,&m);
for(int i=;i<=*n;i++) fa[i]=i;
int x,y,k=,bk=;
for(int i=;i<=m;i++)
{
scanf("%d%d",&x,&y);
scanf("%s",s);
if(s[]=='L')
{
fa[findfa(x)]=findfa(n+y);
fa[findfa(n+x)]=findfa(y);
}
else
{
fa[findfa(x)]=findfa(y);
fa[findfa(n+x)]=findfa(n+y);
}
if(findfa(y)==findfa(n+y)) bk=;
if(bk) k++;
}
printf("%d\n",k);
return ;
}
【usaco-Liars and Truth Tellers, 2013 Jan真假奶牛】并查集的更多相关文章
- [usaco]2013-jan Liars and Truth Tellers 真假奶牛
Description 约翰有N头奶牛,有一部分奶牛是真话奶牛,它们只说真话,而剩下的是假话奶牛,只说假话.有一天,约翰从奶牛的闲谈中陆续得到了M句话,第i句话出自第Xi头奶牛,它会告诉约翰第Yi头是 ...
- Bzoj5188/洛谷P4185 [Usaco2018 Jan]MooTube(并查集)
题面 Bzoj 洛谷 题解 最暴力的方法是直接判两个点之间的路径最小值是否\(\geq k\),用\(Dijkstra\)可以做到该算法最快效率,但是空间复杂度始终是\(O(n^2)\)的,会\(ML ...
- D. Connected Components Croc Champ 2013 - Round 1 (并查集+技巧)
292D - Connected Components D. Connected Components time limit per test 2 seconds memory limit per t ...
- USACO翻译:USACO 2013 JAN三题(1)
USACO 2013 JAN 一.题目概览 中文题目名称 镜子 栅栏油漆 奶牛排队 英文题目名称 mirrors paint lineup 可执行文件名 mirrors paint lineup 输入 ...
- 【POJ1417】【带标记并查集+DP】True Liars
Description After having drifted about in a small boat for a couple of days, Akira Crusoe Maeda was ...
- POJ 1417 - True Liars - [带权并查集+DP]
题目链接:http://poj.org/problem?id=1417 Time Limit: 1000MS Memory Limit: 10000K Description After having ...
- POJ1417:True Liars(DP+带权并查集)
True Liars Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- POJ1417 True Liars —— 并查集 + DP
题目链接:http://poj.org/problem?id=1417 True Liars Time Limit: 1000MS Memory Limit: 10000K Total Submi ...
- F - True Liars 带权并查集
After having drifted about in a small boat for a couple of days, Akira Crusoe Maeda was finally cast ...
随机推荐
- TCP 的有限状态机
TCP 有限状态机的图中每一个方框都是 TCP 可能具有的状态. 每个方框中的大写英文字符串是 TCP 标准所使用的 TCP 连接状态名. 状态之间的箭头表示可能发生的状态变迁. 箭头旁边的字,表明引 ...
- C# Excel2007 导出生成 2003兼容格式
//导出2007格式 AppWb.Saved = true; //保存工作薄 AppExcel.ActiveWorkbook.SaveCopyAs(FilePath); //导出2003格式 AppW ...
- HDU 1995 R-汉诺塔V
https://vjudge.net/contest/67836#problem/R 用1,2,...,n表示n个盘子,称为1号盘,2号盘,....号数大盘子就大.经典的汉诺塔问 题经常作为一个递归的 ...
- C# 泛型和委托
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- listBox和pictureBox的使用
重要属性:pictureBox中SizeMode可以更改图像显示的尺寸大小. using System; using System.Collections.Generic; using System. ...
- dwarf是如何处理栈帧的?
dwarf是如何处理栈帧的? DW_AT_frame_base 表明函数栈帧的起始点 95 < 1><0x000000ca> DW_TAG_subprogram 96 ...
- 【Python】python基础语法 编码
编码 默认情况下,python以UTF-8编码,所有的字符串都是Unicode字符串,可以为代码定义不同的的编码. #coding:UTF-8 #OR #-*- coding:UTF-8 -*- p ...
- monitor_guiagent
monitor_guiagent monitor_guiagent.sh #!/usr/bin/env bash #filename : monitor_guiagent.sh #Usage: /us ...
- [NOIP2012]疫情控制 贪心 二分
题面:[NOIP2012]疫情控制 题解: 大体思路很好想,但是有个细节很难想QAQ 首先要求最大时间最小,这种一般都是二分,于是我们二分一个时间,得到一个log. 然后发现一个军队,越往上走肯定可以 ...
- [Vue源码分析] v-model实现原理
最近小组有个关于vue源码分析的分享会,提前准备一下… 前言:我们都知道使用v-model可以实现数据的双向绑定,及实现数据的变化驱动dom的更新,dom的更新影响数据的变化.那么v-model是怎么 ...