【USACO】2013 Jan Liars and Truth Tellers 真假奶牛

该博客介绍了USACO竞赛中关于真假奶牛的问题,约翰需要确定哪些奶牛说的是真话哪些是假话。给定奶牛的对话,博客分析了判断记录是否矛盾的方法,并提供了一个求解最大不矛盾话数K的思路,通过二分图染色解决冲突。

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

Liars and Truth Tellers 真假奶牛


  • Description

约翰有N头奶牛,有一部分奶牛是真话奶牛,它们只说真话,而剩下的是假话奶牛,只说假话。有一天,约翰从奶牛的闲谈中陆续得到了M句话,第i句话出自第Xi头奶牛,它会告诉约翰第Yi头是一头真话奶牛还是假话奶牛。然而,约翰记性不好,他可能把这些话的内容记错了。请检查一下 约翰的记录是否会有矛盾,帮助他找到一个尽量大的K使得约翰记下的前K句话不矛盾。

  • Input Format

第一行:两个整数 N 和 M ,1 ≤ N ≤ 1000; 1 ≤ M ≤ 10000
• 第二行到 M + 1 行:第 i + 1 行有两个整数:Xi 和 Yi,1 ≤ Xi, Yi ≤ N ,接下来有一个字符:
– 如果是 T ,表示 Xi 说 Yi 是真话奶牛;
– 如果是 L,表示 Xi 说 Yi 是假话奶牛;

  • Output Format

单个整数,即表示题目描述中的K

  • Sample Input

4 3
1 4 L
2 3 T
4 1 T

  • Sample Output

2

  • Hint

解释
前两句没有矛盾, 但第一句和第三句存在矛盾


  • 分析

若x说y是假,则x真y假或x假y真;
若x说y是真,则x真y真或x假y假。
然后我们二分一个答案Lim,对于边的编号在2*Lim以内的做二分图染色。对于状况一染不同的颜色,状况二染相同的颜色。如果能够顺利跑完,说明到第Lim句话之前都是没有矛盾的。


#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int N=1001,M=10001;
int n,m,u,v,Ans,tot,last[N],Colored[N];
char ch;
struct Edge{int to,next,c;}E[2*M];
void Addline(int u,int v,int w){
    E[++tot].to=v; E[tot].next=last[u]; E[tot].c=w; last[u]=tot;
    E[++tot].to=u; E[tot].next=last[v]; E[tot].c=w; last[v]=tot;
}
bool Color(int u,int Lim){
    for (int i=last[u];i;i=E[i].next){
        if (i>Lim) continue;
        int v=E[i].to;
        if (E[i].c==0){
            if (Colored[v]==-1){
                Colored[v]=1-Colored[u];
                if (!Color(v,Lim)) return false;
            }
            else if (Colored[v]!=1-Colored[u]) return false;
        }
        else{
            if (Colored[v]==-1){
                Colored[v]=Colored[u];
                if (!Color(v,Lim)) return false;
            }
            else if (Colored[v]!=Colored[u]) return false;
        }
    }
    return true;
}
bool Check(int Lim){
    memset(Colored,-1,sizeof(Colored));
    for (int i=1;i<=n;i++)
        if (Colored[i]==-1){
            Colored[i]=0;
            if (!Color(i,Lim)) return false;
        }
    return true;
}
int main(){
    freopen("in.txt","r",stdin);
    freopen("out.txt","w",stdout);
    scanf("%d%d\n",&n,&m);
    for (int i=1;i<=m;i++){
        scanf("%d%d %c\n",&u,&v,&ch);
        Addline(u,v,ch=='T');
    }
    for (int L=0,R=m,Mid=(L+R)>>1;L<=R;Mid=(L+R)>>1)
        if (Check(2*Mid)) Ans=Mid,L=Mid+1;
        else R=Mid-1;
    printf("%d",Ans);
    fclose(stdin); fclose(stdout);
    return 0;
}
### 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'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、付费专栏及课程。

余额充值