True Liars POJ - 1417
In order to prevent the worst-case scenario, Akira should distinguish the devilish from the divine. But how? They looked exactly alike and he could not distinguish one from the other solely by their appearances. He still had his last hope, however. The members of the divine tribe are truth-tellers, that is, they always tell the truth and those of the devilish tribe are liars, that is, they always tell a lie.
He asked some of them whether or not some are divine. They knew one another very much and always responded to him "faithfully" according to their individual natures (i.e., they always tell the truth or always a lie). He did not dare to ask any other forms of questions, since the legend says that a devilish member would curse a person forever when he did not like the question. He had another piece of useful informationf the legend tells the populations of both tribes. These numbers in the legend are trustworthy since everyone living on this island is immortal and none have ever been born at least these millennia.
You are a good computer programmer and so requested to help Akira by writing a program that classifies the inhabitants according to their answers to his inquiries.
Input
n p1 p2
xl yl a1
x2 y2 a2
...
xi yi ai
...
xn yn an
The first line has three non-negative integers n, p1, and p2. n is the number of questions Akira asked. pl and p2 are the populations of the divine and devilish tribes, respectively, in the legend. Each of the following n lines has two integers xi, yi and one word ai. xi and yi are the identification numbers of inhabitants, each of which is between 1 and p1 + p2, inclusive. ai is either yes, if the inhabitant xi said that the inhabitant yi was a member of the divine tribe, or no, otherwise. Note that xi and yi can be the same number since "are you a member of the divine tribe?" is a valid question. Note also that two lines may have the same x's and y's since Akira was very upset and might have asked the same question to the same one more than once.
You may assume that n is less than 1000 and that p1 and p2 are less than 300. A line with three zeros, i.e., 0 0 0, represents the end of the input. You can assume that each data set is consistent and no contradictory answers are included.
Output
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <vector>
#include <cmath> using namespace std; #define ll long long
#define pb push_back
#define fi first
#define se second const int N = ;
struct node
{
int rt, v;
}fa[N];
int dp[N][N];
int a[N][];//不同树上两种人的人数
vector<int > p[N][]; //记录编号
bool vis[N];
int n, p1, p2; int Find(int x)
{
if(fa[x].rt == x) return x;
else{
int tmp = fa[x].rt;
fa[x].rt = Find(fa[x].rt);
fa[x].v = (fa[x].v + fa[tmp].v) % ;
return fa[x].rt;
}
} void Union(int x, int y, int d)
{
int fax = Find(x);
int fay = Find(y);
if(fax != fay){
fa[fay].rt = fax;
fa[fay].v = (fa[x].v + d - fa[y].v + ) % ;
}
} void solve()
{
while(~scanf("%d%d%d", &n, &p1, &p2) && (n + p1 + p2)){ int m = p1 + p2;
for(int i = ; i <= m; ++i){
fa[i].rt = i;
fa[i].v = ;
a[i][] = ;
a[i][] = ;
p[i][].clear();
p[i][].clear();
vis[i] = ; for(int j = ; j <= m; ++j) dp[i][j] = ;
} int x, y;
char op[];
for(int i = ; i <= n; ++i){
scanf("%d%d%s", &x, &y, op);
Union(x, y, op[] == 'y' ? : );
} //所有叶子完整接收信息
for(int i = ; i <= m; ++i) Find(i); int cnt = ;
for(int i = ; i <= m; ++i){
if(!vis[i]){
for(int j = ; j <= m; ++j){
if(fa[j].rt == fa[i].rt){
vis[j] = true;
a[cnt][fa[j].v]++;
p[cnt][fa[j].v].pb(j);
}
}
cnt++;
}
} dp[][] = ;
for(int i = ; i < cnt; ++i){
for(int j = ; j <= m; ++j){
if(j - a[i][] >= && dp[i - ][j - a[i][]]){
dp[i][j] += dp[i - ][j - a[i][]];
}
if(j - a[i][] >= && dp[i - ][j - a[i][]]){
dp[i][j] += dp[i - ][j - a[i][]];
}
}
}
//cout << "ans \\\\\\" << endl;
if(dp[cnt - ][p1] != ) printf("no\n");
else{
vector<int > info;
int remains = p1;
int x;
for(int i = cnt - ; i >= ; --i){
x = remains - a[i][];
if(dp[i - ][x] == ){
for(int o = ; o < a[i][]; ++o){
info.pb(p[i][][o]);
}
remains = x;
continue;
}
x = remains - a[i][];
if(dp[i - ][x] == ){
for(int o = ; o < a[i][]; ++o){
info.pb(p[i][][o]);
} remains = x;
}
} sort(info.begin(), info.end());
int sum = (int)info.size();
for(int i = ; i < sum; ++i) printf("%d\n", info[i]);
printf("end\n");
//printf("end\n\n\n");
}
}
} int main()
{ solve(); return ;
}
True Liars POJ - 1417的更多相关文章
- POJ1417 True Liars —— 并查集 + DP
题目链接:http://poj.org/problem?id=1417 True Liars Time Limit: 1000MS Memory Limit: 10000K Total Submi ...
- poj 1417(并查集+简单dp)
True Liars Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 2087 Accepted: 640 Descrip ...
- POJ1417 True Liars
题意 Language:Default True Liars Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 6392 Accep ...
- POJ1417:True Liars(DP+带权并查集)
True Liars Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- POJ 1417 - True Liars - [带权并查集+DP]
题目链接:http://poj.org/problem?id=1417 Time Limit: 1000MS Memory Limit: 10000K Description After having ...
- poj 1417 True Liars(并查集+背包dp)
题目链接:http://poj.org/problem?id=1417 题意:就是给出n个问题有p1个好人,p2个坏人,问x,y是否是同类人,坏人只会说谎话,好人只会说实话. 最后问能否得出全部的好人 ...
- POJ 1417 True Liars(种类并查集+dp背包问题)
题目大意: 一共有p1+p2个人,分成两组,一组p1,一组p2.给出N个条件,格式如下: x y yes表示x和y分到同一组,即同是好人或者同是坏人. x y no表示x和y分到不同组,一个为好人,一 ...
- POJ 1417 True Liars
题意:有两种人,一种人只会说真话,另一种人只会说假话.只会说真话的人有p1个,另一种人有p2个.给出m个指令,每个指令为a b yes/no,意思是,如果为yes,a说b是只说真话的人,如果为no,a ...
- POJ 1417 并查集 dp
After having drifted about in a small boat for a couple of days, Akira Crusoe Maeda was finally cast ...
随机推荐
- 变分(图)自编码器不能直接应用于下游任务(GAE, VGAE, AE, VAE and SAE)
自编码器是无监督学习领域中一个非常重要的工具.最近由于图神经网络的兴起,图自编码器得到了广泛的关注.笔者最近在做相关的工作,对科研工作中经常遇到的:自编码器(AE),变分自编码器(VAE),图 ...
- 实现saas多租户方案比较
看到一篇比较多租户数据隔离方案的文章,总结挺不错.其实大部分内容在我前几年写的文章都有. 文章翻译自: https://blog.arkency.com/comparison-of-approache ...
- [Google Guava] 强大的集合工具类:java.util.Collections中未包含的集合工具
转载的,有问题请联系我 原文链接 译文链接 译者:沈义扬,校对:丁一 尚未完成: Queues, Tables工具类 任何对JDK集合框架有经验的程序员都熟悉和喜欢java.util.Collecti ...
- 解决Celery 在Windows中搭建和使用的版本
官网:http://docs.celeryproject.org/en/latest/faq.html#does-celery-support-windows 描述如下:表示Celery 4.0版本以 ...
- [Cadence] 10个Cadence AD PADS经典案例 2-12层板设计
[Cadence] 10个Cadence Allegro经典案例 2-12层板设计 自己保存的PCB例程资料分享 Allegro AD PADS看下面截图需要的拿去 下载链接 链接: https:// ...
- drf之框架基础
(一)drf基础 全称:django-rest framework 接口:什么是接口.restful接口规范(协议) CBV(基于FBV的基础上形成).CBV生命周期源码----基于restful规范 ...
- SQL去掉重复数据
SELECT vc_your_email,vc_our_ref_or_code INTO #tmp FROM( SELECT vc_your_email,vc_our_ref_or_code,ROW_ ...
- winxp系统连接服务器丢包解决方法
winxp系统连接服务器丢包解决方法 MFC编写一个打开网页的程序,发生异常没有获取到数据. 分析步骤: 1. 用getLastError()获取到的信息,(2)- 系统找不到指定的文件. 2. 用浏 ...
- TCP实战一(三握四挥、流量控制)
上一篇博文已经介绍了tcpdump的一些基本操作与命令,今天这篇博文将带你解密如何利用wireshark对tcpdump抓到的数据包进行可视化分析! 参考文献:https://zhuanlan.zhi ...
- Spring中的JDBC API
1 JdbcTemplate的诞生 JDBC作为Java平台访问关系数据库的标准API,其成功是有目共睹的.为了避免在JDBC API在使用中的种种尴尬局面(几乎程式一样的代码,繁琐的异常处理),Sp ...