F - True Liars - poj1417(背包+并查集)
#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(背包+并查集)的更多相关文章
- F - True Liars 带权并查集
After having drifted about in a small boat for a couple of days, Akira Crusoe Maeda was finally cast ...
- POJ 1417 - True Liars - [带权并查集+DP]
题目链接:http://poj.org/problem?id=1417 Time Limit: 1000MS Memory Limit: 10000K Description After having ...
- #383 Div1 Problem B Arpa's weak amphitheater.... (分组背包 && 并查集)
题意 : 有n个人,每个人都有颜值bi与体重wi.剧场的容量为W.有m条关系,xi与yi表示xi和yi是好朋友,在一个小组. 每个小组要么全部参加舞会,要么参加人数不能超过1人. 问保证总重量不超过W ...
- 【春训团队赛第四场】补题 | 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 ...
- 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 个人是一个 ...
- Codeforces #541 (Div2) - F. Asya And Kittens(并查集+链表)
Problem Codeforces #541 (Div2) - F. Asya And Kittens Time Limit: 2000 mSec Problem Description Inp ...
- GYM 101173 F.Free Figurines(贪心||并查集)
原题链接 题意:俄罗斯套娃,给出一个初始状态和终止状态,问至少需要多少步操作才能实现状态转化 贪心做法如果完全拆掉再重装,答案是p[i]和q[i]中不为0的值的个数.现在要求寻找最小步数,显然要减去一 ...
- vijos 1250 最勇敢的机器人 分组背包+并查集
P1250最勇敢的机器人 背景 Wind设计了很多机器人.但是它们都认为自己是最强的,于是,一场比赛开始了~ 描述 机器人们都想知道谁是最勇敢的,于是它们比赛搬运一些物品. 它们到了一个仓库,里面有n ...
- 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 而且如果邀请 ...
随机推荐
- Python进阶:函数式编程实例(附代码)
Python进阶:函数式编程实例(附代码) 上篇文章"几个小例子告诉你, 一行Python代码能干哪些事 -- 知乎专栏"中用到了一些列表解析.生成器.map.filter.lam ...
- C#解leetcode:119. Pascal's Triangle II
题目是: Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return ...
- ASP.NET面试
1.net中读写数据库需要用到那些类?他们的作用都是什么?答:DataSet:数据存储器.DataCommand:执行语句命令.DataAdapter:数据的集合,用语填充.2.介绍一下什么是Code ...
- 用js生成二维码
<!doctype html> <html> <meta charset="utf-8"/> <head> <script s ...
- PHP解析xml
<?xml version="1.0" encoding="UTF-8"?> <ZIP_result> <result name= ...
- SQL利用临时表实现动态列、动态添加列
--方法一--------------------------------------------------------------------- declare @sql as varchar(1 ...
- UITabBarController自定义一
UITabBarController自定义一 首先在Appdelegate.m文件中将UITabBarController的子类设置为rootViewController,并设置其viewContro ...
- Linux网络配置命令ifconfig输出信息解析
eth0 Link encap:Ethernet HWaddr 00:1e:4f:e9:c2:84 inet addr:128.224.163.153 Bcast:128.224.163 ...
- Linux 0.11下信号量的实现和应用
Linux 011下信号量的实现和应用 生产者-消费者问题 实现信号量 信号量的代码实现 关于sem_wait和sem_post sem_wait和sem_post函数的代码实现 信号量的完整代码 实 ...
- 深入Java虚拟机读书笔记第二章平台无关性
Java的体系结构对平台无关的支持 Java平台 Java的体系结构通过几种途径支持Java程序的平台无关性,其中主要是通过Java平台自己.Java平台扮演一个运行时Java程序与其下的硬件和操作系 ...