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 ...
随机推荐
- js排他性算法
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- MySql轻松入门系列——第二站 使用visual studio 对mysql进行源码级调试
一:背景 1. 讲故事 上一篇说了mysql的架构图,很多同学反馈说不过瘾,毕竟还是听我讲故事,那这篇就来说一说怎么利用visual studio 对 mysql进行源码级调试,毕竟源码面前,不谈隐私 ...
- 一网打尽枚举操作 .net core
本文介绍如何使用枚举以及,如何将枚举类型更好的应用于项目中,看完本文可以有序的将项目中的枚举更容易的使用到每个角落. 1,分析枚举 /// <summary> /// 性别 /// < ...
- PyQt5 FileDialog的使用例子
加载***.ui文件可以使用: loadUi('main_window.ui', self) self.btnFileChoose.clicked.connect(self.getFolderName ...
- HashMap源码和并发异常问题分析
要点源码分析 HashMap允许键值对为null:HashTable则不允许,会报空指针异常: HashMap<String, String> map= new HashMap<&g ...
- CSS中 隐藏元素的常用方法
在CSS中,使某个元素在页面中消失的方法有很多,今天为大家介绍几种我常用的方式 . 一.display:none; 让元素直接从页面消失,不占用尺寸,会改变页面布局. 代码演示: 页面演示:在页面 ...
- 001.OpenShift介绍
一 OpenShift特性 1.1 OpenShift概述 Red Hat OpenShijft Container Platform (OpenShift)是一个容器应用程序平台,它为开发人员和IT ...
- Springboot项目整合Swagger2报错
SpringBoot2.2.6整合swagger2.2.2版本的问题,启动SpringBoot报如下错: Error starting ApplicationContext. To display t ...
- Thunk函数的使用
Thunk函数的使用 编译器的求值策略通常分为传值调用以及传名调用,Thunk函数是应用于编译器的传名调用实现,往往是将参数放到一个临时函数之中,再将这个临时函数传入函数体,这个临时函数就叫做Thun ...
- 连接 mongodb 数据库 :
mongodb 数据库: 安装 mongodb 数据库: 安装 mongodb 数据库网址: https://www.mongodb.com/download-center#community 检 ...