[USACO2013 Jan]Liars and Truth Tellers真假奶牛

本文介绍了一种使用并查集解决逻辑关系问题的方法。针对特定的真假陈述问题,通过建立‘同盟’与‘敌对’关系进行节点合并,有效地找出逻辑上不一致的陈述。文章提供了完整的代码实现及解析。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目链接:..并没有找到

题目大意:


题解:

并查集

看清题意!说的是找最多的前K句话,而不是找最多的K句话qwq(不然我就不会做了..orz

句型有两种:

1、x说y是真的,那么x和y同真同假。

2、x说y是假的,那么x和y一真一假。

所以就有两种关系,可近似看成——“同盟”&"敌对"。

于是就是经典的并查集啦。

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#define maxn 1100

int fa[maxn],em[maxn];//fa-同盟的 em-敌对
int ffind(int x)
{
	if (fa[x]!=x) fa[x]=ffind(fa[x]);
	return fa[x];
}
int main()
{
	//freopen("truth.in","r",stdin);
	//freopen("truth.out","w",stdout);
	int n,m,x,y,i,bk=-1;char c;
	scanf("%d%d",&n,&m);
	for (i=1;i<=n;i++) {fa[i]=i;em[i]=-1;}
	// memset(pd,-1,sizeof(pd));
	for (i=1;i<=m;i++)
	{
		scanf("%d%d %c",&x,&y,&c);
		if (bk!=-1) continue;
		if (c=='T')
		{
			int fx=ffind(fa[x]),fy=ffind(fa[y]);
			if (em[fx]==-1 || em[fy]==-1)//朋友的敌人是敌人
			{
				fa[fy]=fx;
				if (em[fx]!=-1) em[fy]=em[fx];
				else if (em[fy]!=-1) em[fx]=em[fy];
			}else
			{
				int ex=ffind(em[fx]),ey=ffind(em[fy]);
				if (fx==ey || fy==ex) {bk=i;continue;}//有矛盾啦
				fa[fy]=fx;fa[ex]=ey;				
			}
		}else if (c=='L')
		{
			int fx=ffind(fa[x]),fy=ffind(fa[y]);
			if (fx==fy) {bk=i;continue;}//有矛盾啦
			if (em[fx]==-1 || em[fy]==-1)
			{
				if (em[fx]!=-1) {em[fy]=fx;fa[fy]=em[fx];}//敌人的敌人是朋友
				else if (em[fy]!=-1) {em[fx]=fy;fa[fx]=em[fy];}
				else em[fx]=fy,em[fy]=fx;
			}else
			{
				int ex=ffind(em[fx]),ey=ffind(em[fy]);
				fa[fx]=ey;fa[fy]=ex;
			}
		}
	}
	if (bk==-1) bk=m;else bk--;
	printf("%d\n",bk);
	return 0;
}


转载于:https://www.cnblogs.com/Euryale-Rose/p/6527840.html

### USACO 2016 January Contest Subsequences Summing to Sevens Problem Solution and Explanation In this problem from the USACO contest, one is tasked with finding the size of the largest contiguous subsequence where the sum of elements (IDs) within that subsequence is divisible by seven. The input consists of an array representing cow IDs, and the goal is to determine how many cows are part of the longest sequence meeting these criteria; if no valid sequences exist, zero should be returned. To solve this challenge efficiently without checking all possible subsequences explicitly—which would lead to poor performance—a more sophisticated approach using prefix sums modulo 7 can be applied[^1]. By maintaining a record of seen remainders when dividing cumulative totals up until each point in the list by 7 along with their earliest occurrence index, it becomes feasible to identify qualifying segments quickly whenever another instance of any remainder reappears later on during iteration through the dataset[^2]. For implementation purposes: - Initialize variables `max_length` set initially at 0 for tracking maximum length found so far. - Use dictionary or similar structure named `remainder_positions`, starting off only knowing position `-1` maps to remainder `0`. - Iterate over given numbers while updating current_sum % 7 as you go. - Check whether updated value already exists inside your tracker (`remainder_positions`). If yes, compare distance between now versus stored location against max_length variable&#39;s content—update accordingly if greater than previous best result noted down previously. - Finally add entry into mapping table linking latest encountered modulus outcome back towards its corresponding spot within enumeration process just completed successfully after loop ends normally. Below shows Python code implementing described logic effectively handling edge cases gracefully too: ```python def find_largest_subsequence_divisible_by_seven(cow_ids): max_length = 0 remainder_positions = {0: -1} current_sum = 0 for i, id_value in enumerate(cow_ids): current_sum += id_value mod_result = current_sum % 7 if mod_result not in remainder_positions: remainder_positions[mod_result] = i else: start_index = remainder_positions[mod_result] segment_size = i - start_index if segment_size > max_length: max_length = segment_size return max_length ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值