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 而且如果邀请 ...
随机推荐
- Centos6.3 配置yum 163源
1. 下载repo文件 下载地址:http://mirrors.163.com/.help/CentOS6-Base-163.repo 2. 备份并替换系统的repo文件[root@localh ...
- Pyhton开发【第五篇】:Python基础之杂货铺
Python开发[第五篇]:Python基础之杂货铺 字符串格式化 Python的字符串格式化有两种方式: 百分号方式.format方式 百分号的方式相对来说比较老,而format方式则是比较先进 ...
- 用angular来思考问题How do I “think in AngularJS” if I have a jQuery background?
[翻译]How do I “think in AngularJS” if I have a jQuery background? 1. 不要先设计页面,然后再使用DOM操作来改变它的展现 在jQuer ...
- (转)JavaWeb学习总结(十三)——使用Session防止表单重复提交
如何防止表单重复提交 在平时开发中,如果网速比较慢的情况下,用户提交表单后,发现服务器半天都没有响应,那么用户可能会以为是自己没有提交表单,就会再点击提交按钮重复提交表单,我们在开发中必须防止表单重复 ...
- C#比较dynamic和Dictionary性能
开发中需要传递变参,考虑使用 dynamic 还是 Dictionary(准确地说是Dictionary<string,object>).dynamic 的编码体验显著优于 Diction ...
- Android开发手记(16) 数据存储一 SharedPreferences
Android为数据存储提供了五种方式: 1.SharedPreferences 2.文件存储 3.SQLite数据库 4.ContentProvider 5.网络存储 SharedPreferenc ...
- Oracle11G 数据库 expdp、impdp使用示例
expdp 备份数据库.expdp/impdp简单测试 操作系统层面创建目录 [root@Oracle11g ~]# mkdir -p /home/oracle/db_back/ 修改目录的所属用户. ...
- centos 6.X 安装输入法
1.打开终端 su 输入 密码 yum install "@Chinese Support" 2.接下来是启用中文输入法的操作 系统 ->首选项 ->输入法 3.在弹出 ...
- 服务器之间socket传输单链接和多连接测试结果
今天做了一下测试,目的是看看局域网内服务器a,通过一个连接往服务器b传输数据,和通过多个连接传输的不同. 结果发现和多少个连接没关系,一个进程一个连接就能跑满网卡,只要write的时候够快,read的 ...
- 09_linux下安装Nvidia显卡驱动
下载驱动 去官网找去,哈哈o(^▽^)o 安装kernel source [root@localhost ~]# yum install kernel-devel 如果还不行,试试下面的 [root@ ...