题意:有这么一群人,一群好人,和一群坏人,好人永远会说实话,坏人永远说假话,现在给你一组对话和好人与坏人的数目P1, P2。
数据里面的no是A说B是坏人, yes代表A说B是好人,就是这样,问题能不能从这些话里面得出来唯一的解,就是可以确定谁是好人谁是坏人,如果不能输出no,如果可以输出所有的好人。
分析,可以把这些人依据他们中的关系分成几个小集合,集合里面保存两类人的数目,然后DP求出来好人或者坏人是不是唯一的即可,‘
注意并查集时候可以这么认为凡是yes都是同类,no都是异类
///////////////////////////////////////////////////////////////

#include<iostream>

#include<algorithm>
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<queue>
using namespace std; const int maxn = ; struct people
{
    int father, relation;
    int same, other;//同类数目,和异类数目
    int True;//是说谎者还是诚实者
}p[maxn]; int dp[maxn][maxn]; int Find(int x)
{
    int k = p[x].father;
    if( p[x].father != x )
    {
        p[x].father = Find(k);
        p[x].relation = (p[x].relation+p[k].relation)%;
    }     return p[x].father;
} int main()
{
    int M, T, L;     while(scanf("%d%d%d", &M, &T, &L), M+T+L)
    {
        int i, j, u, v, ru, rv, ok=, d, N=T+L, k=;
        char s[];int f[maxn];//f记录最后小集体的头结点         for(i=; i<=N; i++)
        {
            p[i].father = i;
            p[i].other = ;
            p[i].same = ;//自己和自己是同类,所以最少也有一个
            p[i].relation = ;
            p[i].True = ;
        }         while(M--)
        {
            scanf("%d%d%s", &u, &v, s);             if(ok)continue;
            ru = Find(u), rv = Find(v);             if(s[] == 'y')
                d = ;//0表示同类,1表示异类
            else d = ;             if(ru == rv && (p[v].relation+p[u].relation)% != d)
                ok = ;
            else if(ru != rv)
            {
                p[ru].father = rv;
                p[ru].relation = (p[u].relation+p[v].relation+d)%;
            }
        }         if(!ok)//有可能说的话有矛盾
        {
            for(i=; i<=N; i++)
            {
                u = Find(i);
                if(u == i)
                    f[k++] = i;
                else
                {
                    p[u].other += p[i].relation;
                    p[u].same += -p[i].relation;
                }
            }             memset(dp, , sizeof(dp));             dp[][ p[ f[] ].same ] += ;
            dp[][ p[ f[] ].other ] += ;             for(i=; i<k; i++)
            {
                u = f[i];
                for(j=; j<=N; j++)
                {
                    if(dp[i-][j])
                    {
                        dp[i][ p[u].same+j ] += dp[i-][j];
                        dp[i][ p[u].other+j ] += dp[i-][j];
                    }
                }
            }
        }         if(dp[k-][L] !=  || ok)
            printf("no\n");
        else
        {
            for(i=k-; i>; i--)
            {
                u = f[i];
                v = p[u].same;
                if( (i!= && dp[i-][T-v] != ) || (i== && T==v) )
                {
                    p[u].True = ;
                    T -= v;
                }
                else
                    T -= p[u].other;
            }             for(i=; i<=N; i++)
            {
                u = p[i].father;
                if(p[u].True && !p[i].relation || p[u].True== && p[i].relation)
                    printf("%d\n", i);
            }
            printf("end\n");
        }
    }     return ;
}
/*
1 1 1
1 2 yes

*/

F - True Liars - poj1417(背包+并查集)的更多相关文章

  1. F - True Liars 带权并查集

    After having drifted about in a small boat for a couple of days, Akira Crusoe Maeda was finally cast ...

  2. POJ 1417 - True Liars - [带权并查集+DP]

    题目链接:http://poj.org/problem?id=1417 Time Limit: 1000MS Memory Limit: 10000K Description After having ...

  3. #383 Div1 Problem B Arpa's weak amphitheater.... (分组背包 && 并查集)

    题意 : 有n个人,每个人都有颜值bi与体重wi.剧场的容量为W.有m条关系,xi与yi表示xi和yi是好朋友,在一个小组. 每个小组要么全部参加舞会,要么参加人数不能超过1人. 问保证总重量不超过W ...

  4. 【春训团队赛第四场】补题 | MST上倍增 | LCA | DAG上最长路 | 思维 | 素数筛 | 找规律 | 计几 | 背包 | 并查集

    春训团队赛第四场 ID A B C D E F G H I J K L M AC O O O O O O O O O 补题 ? ? O O 传送门 题目链接(CF Gym102021) 题解链接(pd ...

  5. Codeforces 741B:Arpa's weak amphitheater and Mehrdad's valuable Hoses(01背包+并查集)

    http://codeforces.com/contest/741/problem/B 题意:有 n 个人,每个人有一个花费 w[i] 和价值 b[i],给出 m 条边,代表第 i 和 j 个人是一个 ...

  6. Codeforces #541 (Div2) - F. Asya And Kittens(并查集+链表)

    Problem   Codeforces #541 (Div2) - F. Asya And Kittens Time Limit: 2000 mSec Problem Description Inp ...

  7. GYM 101173 F.Free Figurines(贪心||并查集)

    原题链接 题意:俄罗斯套娃,给出一个初始状态和终止状态,问至少需要多少步操作才能实现状态转化 贪心做法如果完全拆掉再重装,答案是p[i]和q[i]中不为0的值的个数.现在要求寻找最小步数,显然要减去一 ...

  8. vijos 1250 最勇敢的机器人 分组背包+并查集

    P1250最勇敢的机器人 背景 Wind设计了很多机器人.但是它们都认为自己是最强的,于是,一场比赛开始了~ 描述 机器人们都想知道谁是最勇敢的,于是它们比赛搬运一些物品. 它们到了一个仓库,里面有n ...

  9. Codeforces Round #383 (Div. 2)D. Arpa's weak amphitheater and Mehrdad's valuable Hoses(dp背包+并查集)

    题目链接 :http://codeforces.com/contest/742/problem/D 题意:给你n个女人的信息重量w和美丽度b,再给你m个关系,要求邀请的女人总重量不超过w 而且如果邀请 ...

随机推荐

  1. 简单说明Python中的装饰器的用法

    简单说明Python中的装饰器的用法 这篇文章主要简单说明了Python中的装饰器的用法,装饰器在Python的进阶学习中非常重要,示例代码基于Python2.x,需要的朋友可以参考下   装饰器对与 ...

  2. Android的GridView和Gallery结合Demo

    Android的GridView和Gallery结合Demo Demo介绍:首页是一个GridView加载图片,竖屏时显示3列图片,横屏时显示4列图片;并且对图片进行大小限制和加灰色边框处理. 点击某 ...

  3. (转)iFrame高度自适应

    第一种方法:代码简单,兼容性还可以,大家可以先测试下: function SetWinHeight(obj) { var win=obj; if (document.getElementById) { ...

  4. DEDECMS批量修改默认文章和列表命名规则的方法

    很多人因为添加分类而苦恼,尤其是批量添加的时候,必须要重新修改一下文章命名规则和列表命名规则,都是为了做SEO.如果进行默认值的修改,就会事半功倍.不多说. 一.DEDE5.5修改默认文章命名规则. ...

  5. Access数据库数据转换Table.Json

    使用WPF组件 xaml <Window x:Class="JsonConvert.MainWindow" xmlns="http://schemas.micros ...

  6. 合理计划 dictionary cache 大小

    [数据字典缓冲区(Data Dictionary Cache)  ] 用于存放Oracle系统管理自身所需要的所有信息,包括登录的用户名.用户对象.权限等. 查看 data dictionary ca ...

  7. Swift - 20 - 字典的基础操作

    //: Playground - noun: a place where people can play import UIKit var dict = [1:"one", 2:& ...

  8. 重拾C++ 基础知识总结(一)

    1.使用gcc编译c++文件报错 proc1.cc:(.text+0x14): undefined reference to `std::cout' C++程序使用gcc命令只能编译,不能链接库文件 ...

  9. [LeetCode OJ] Single Number之一 ——Given an array of integers, every element appears twice except for one. Find that single one.

    class Solution { public: int singleNumber(int A[], int n) { int i,j; ; i<n; i++) { ; j<n; j++) ...

  10. c#中使用ABCpdf处理PDF

    using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI ...