poj 1417 True Liars (并查集+dp)
2014-03-08 21:21
555 查看
True Liars
After having drifted about in a small boat for a couple of days, Akira Crusoe Maeda was finally cast ashore on a foggy island. Though he was exhausted and despaired, he was still fortunate to remember a legend of the foggy island, which he had heard from patriarchs in his childhood. This must be the island in the legend. In the legend, two tribes have inhabited the island, one is divine and the other is devilish, once members of the divine tribe bless you, your future is bright and promising, and your soul will eventually go to Heaven, in contrast, once members of the devilish tribe curse you, your future is bleak and hopeless, and your soul will eventually fall down to Hell. 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 The input consists of multiple data sets, each in the following format : 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 For each data set, if it includes sufficient information to classify all the inhabitants, print the identification numbers of all the divine ones in ascending order, one in a line. In addition, following the output numbers, print end in a line. Otherwise, i.e., if a given data set does not include sufficient information to identify all the divine members, print no in a line. Sample Input 2 1 1 1 2 no 2 1 no 3 2 1 1 1 yes 2 2 yes 3 3 yes 2 2 1 1 2 yes 2 3 no 5 4 3 1 2 yes 1 3 no 4 5 yes 5 6 yes 6 7 no 0 0 0 Sample Output no no 1 2 end 3 4 5 6 end Source Japan 2002 Kanazawa |
给出p1+p2个人,其中p1个是好人,p2个是坏人。好人只说真话,坏人只说假话,有一些关系 ,a说b是好人(坏人).其中没有矛盾的,判断是否有唯一解判断哪些人是好人,哪些人是坏人。
思路来源于:cxlove博客
思路:
a说b是好人,那么a、b是同种人的,a说b是坏人,那么a、b不是同种人,用并查集维护每个有关系的集合,集合存和root关系相同的有多少人、不同的有多少人,然后dp。
dp[i][j] 前i个集合有j个好人的种数。
用一个数组纪录路径,不过要输出方案貌似还要一点处理,我是通过建图处理的,代码写的好长,衰~
代码:
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <string> #include <map> #include <stack> #include <vector> #include <set> #include <queue> #pragma comment (linker,"/STACK:102400000,102400000") #define maxn 1005 #define MAXN 2005 #define mod 1000000009 #define INF 0x3f3f3f3f #define pi acos(-1.0) #define eps 1e-6 typedef long long ll; using namespace std; int n,m,ans,cnt,tot,flag; int p1,p2; int num[maxn][2],pre[maxn],dp[maxn][maxn],vis[maxn],pp[maxn]; int path[maxn][maxn],mp[maxn],ok[maxn][maxn],tmp[maxn],vv[maxn]; int X[maxn],Y[maxn]; char s[maxn][10]; struct Node { int v,w,next; } edge[10005]; vector<int>res; void addedge(int u,int v,int w) { tot++; edge[tot].v=v; edge[tot].w=w; edge[tot].next=pp[u]; pp[u]=tot; } int Find(int x) { if(x==pre[x]) return x; pre[x]=Find(pre[x]); return pre[x]; } void Merge(int x,int y) { int u=Find(x),v=Find(y); if(u!=v) { if(ok[u][v]) { pre[u]=v; num[v][1]+=num[u][1]; num[v][0]+=num[u][0]; } else { pre[u]=v; num[v][1]+=num[u][0]; num[v][0]+=num[u][1]; } } } void dfs(int u,int k,int r) // k-是否与根相同 r-要找的点 { if(k==r) res.push_back(u); int i,j,t,v; for(i=pp[u]; i; i=edge[i].next) { v=edge[i].v; if(!vis[v]) { vis[v]=1; dfs(v,k^(!edge[i].w),r); } } } void dfs1(int u,int k) { tmp[u]=k; int i,j,t,v; for(i=pp[u];i;i=edge[i].next) { v=edge[i].v; if(!vis[v]) { vis[v]=vv[v]=1; dfs1(v,k^(!edge[i].w)); } } } void presolve() // 预处理ok[i][j] i和j的属性是否相同 { int i,j,t; memset(vis,0,sizeof(vis)); for(i=1; i<=p1+p2; i++) { if(!vis[i]) { memset(tmp,0,sizeof(tmp)); memset(vv,0,sizeof(vv)); vis[i]=vv[i]=1; dfs1(i,1); vector<int>tt,tx; for(j=1;j<=p1+p2;j++) { if(vv[j]) tt.push_back(j),tx.push_back(tmp[j]); } for(j=0;j<tt.size();j++) { for(int k=j;k<tt.size();k++) { int u=tt[j],v=tt[k]; if(tx[j]==tx[k]) ok[u][v]=ok[v][u]=1; else ok[u][v]=ok[v][u]=0; } } } } } void solve() // dp并输出答案 { int i,j,t; memset(dp,0,sizeof(dp)); memset(vis,0,sizeof(vis)); dp[0][0]=1; cnt=0; for(i=1; i<=p1+p2; i++) { int x=Find(i); if(!vis[x]) { cnt++; mp[cnt]=x; vis[x]=1; for(j=0; j<=p1; j++) { if(j>=num[x][0]) { dp[cnt][j]+=dp[cnt-1][j-num[x][0]]; if(dp[cnt][j]) { path[cnt][j]=j-num[x][0]; } } if(j>=num[x][1]) { int last=dp[cnt][j]; dp[cnt][j]+=dp[cnt-1][j-num[x][1]]; if(dp[cnt][j]!=last) { path[cnt][j]=j-num[x][1]; } } } } } res.clear(); if(dp[cnt][p1]==1) { int now=p1; for(i=cnt; i>=1; i--) { int cxx=path[i][now],k; if(now-cxx==num[mp[i]][1]) k=1; else k=0; if(now==cxx) continue ; memset(vis,0,sizeof(vis)); vis[mp[i]]=1; dfs(mp[i],1,k); now=cxx; } sort(res.begin(),res.end()); for(i=0; i<res.size(); i++) { printf("%d\n",res[i]); } printf("end\n"); } else printf("no\n"); } int main() { int i,j,t; while(scanf("%d%d%d",&n,&p1,&p2),n|p1|p2) { int u,v; tot=0; memset(pp,0,sizeof(pp)); for(i=1; i<=p1+p2; i++) { pre[i]=i; num[i][1]=1; num[i][0]=0; } for(i=1; i<=n; i++) { scanf("%d%d%s",&X[i],&Y[i],s[i]); addedge(X[i],Y[i],s[i][0]=='y'); addedge(Y[i],X[i],s[i][0]=='y'); } presolve(); for(i=1;i<=n;i++) { Merge(X[i],Y[i]); } solve(); } return 0; } /* 7 4 2 2 3 no 3 4 no 4 3 no 5 5 yes 5 3 no 5 5 yes 1 5 yes */
相关文章推荐
- 本地通知UILocalNotification
- Longest Consecutive Sequence
- Longest Consecutive Sequence
- Query上传插件Uploadify 3.2使用
- ARCH UEFI 想说爱你不容易
- 推荐两款在线文本编辑器
- 怪异模式(Quirks Mode)对 HTML 页面的影响w
- 简单登陆UI设计
- 使用heapq标准库计算访问次数最多的query
- gtk ui block
- "quit-alert"
- Flash Builder 开发视频播放器客户端(myhslider)
- Flash Builder 开发视频播放器客户端(FlvPlayer.mxml)
- 【Ext JS 4】组件查找_ComponentQuery类
- Qt学习总结-ui篇
- javaweb之request获取referer请求头实现防盗链
- visual studio 生成后事件 Post-Build Event
- javaweb之request使用RequestDispatcher的include方法实现页面包含
- javaweb之request请求转发forward方法的细节
- NGUI -- UIPanel