vjudge7.21 I POJ 1291题解

本题主要应用并查集*

题目
Description
The court of King Xeon 2.4 is plagued with intrigue and conspiracy. A document recently discovered by the King’s Secret Service is thought to be part
of some mischievous scheme. The document contains simply a set of sentences which state the truth or falsehood of each other. Sentences have the form “Sentence X is true/false” where X identifies one sentence in the set. The King’s Secret Service suspects
the sentences in fact refer to another, yet uncovered, document.

While they try to establish the origin and purpose of the document, the King ordered you to find whether the set of sentences it contains is consistent, that is, if there is a valid truth assignment for the sentences. If the set is consistent, the King wants
you to determine the maximum number of sentences which can be made true in a valid truth assignment for the document.
Input
The input contains several instances of documents. Each document starts with a line containing a single integer,

N, which indicates the number of sentences in the document (1 <= N <= 1000). The following N lines contain each a sentence. Sentences are numbered sequentially, in the order they appear in the input (the first is sentence 1, the second is sentence 2, and so
on). Each sentence has the form “Sentence X is true.” or “Sentence X is false.”, where 1 <= X <= N. The value N = 0 indicates the end of input.
Output
For each document in the input your program should output one line. If the document is consistent,your program should print the maximum number of sentences
in a valid truth assignment for the document.Otherwise your program should print the word ‘Inconsistent’.
Sample Input
1
Sentence 1 is false.
1
Sentence 1 is true.
5
Sentence 2 is false.
Sentence 1 is false.
Sentence 3 is true.
Sentence 3 is true.
Sentence 4 is false.
0
Sample Output
Inconsistent
1
3

题解:
题目的意思为按照第几句话说第几句话是真的或者假的格式,给出指定数目的几句话,并求其中真话数目最多的个数,如果其中有相悖的地方,则单独输出一行Inconsistent
此题可以通过并查集的方式求解,通过每个集合中的代表元(也就是根节点)把两个不同的集合进行合并,得出其中每个节点的相对关系,最后统计每个集合最大的真话(或假话数目,因为真话和假话是相对的,换句话说如果假话为真了那真话就为假了)数目,就可以得出本题中的最大真话数。
代码参考引用自网络搜索https://blog.csdn.net/churehill123/article/details/9853637
下面是关系本题并结合参考代码的一些细节
本题用了并查集的模板套路,查找,合并等函数模块,但在查找中,新加了一句*status[k]=status[k]?status[parent[k]]:!status[parent[k]];*这句话的用意是为了更新集合中的相对关系,并且以根节点为标准进行更新,从而保证了集合中相对关心的正确性,而这里的status数组,就是用来保存子节点与父节点之间关系的数组,因为在一个并查集中并不是所有元素都是你真我真这种情形的,有可能真假都会在一个集合中出现,所以status数组就是为了记录集合中的这种关系而诞生的,让集合中真假之间的相对关系得以体现,在引用的参考代码中1为一样,0不同,然后就是后面的压缩路径等操作,至此查找模块告一段落。
之后是合并模块,合并模块是根据每个集合中的代表元去合并的,所以第一步就要确定这两个元素的代表元是谁,它们相同还是不同,如果相同,就可以根据status数组中记录的它们与根节点的关系进行判断它们是否相悖,比如两个元素的代表元是一样的,并且它们与代表元的真假情况也相同(即代表元真就真,代表元假就假)那么就可以根据句子中的真假关系去判断它们是否相悖,很显然,如果这这两个元素一个说另一个元素时假的,那么这个元素的真假关系肯定不相同,所以相悖,那么同理,如果这两个元素真假关系不同,且在同一个集合中,如果一个元素说另一个元素是真的,那么他们的真假情况应当相同,所以相悖,那么其实这也就是两元素集合相同时的两种情况,那么下面来说当两元素不在同一集合时,而且这两个元素又产生的关系,比如谁说谁真假,那么为了判断两个集合中所有元素的真假关系,我们必须要把两个集合合并,否则我们只能得到集合中两个元素之间的真假关系,而得不到两个集合所有元素的真假关系,合并时要判断两个代表元之间的真假关系,同样可以根据两个元素与各自代表元的真假关系来判断,如果两元素的真假关系是相同的,那么其代表元之间的真假关系肯定是相同的,那么直接把两元素之间的真假关系赋给其中一个代表元即可,反之如果两元素之间的真假关系不同,那么其代表元的关系比然相反,把两元素之间的关系的非赋予其中一个代表元即可,并把另一个代表元当做新集合的代表元,让其成为赋值代表元的父节点,便完成了合并,之后只要以代表元为标准,更新一下集合中的真假关系就可以让此集合完结。
最后是统计部分,统计部分就是把题目中所有集合的最大真话数相加,变是本题的最大真话数。
至此,本题完结。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值