有理有据的结论题

Problem Description

"The Werewolves" is a popular card game among young people.In the basic game, there are 2 different groups: the werewolves and the villagers.
Each player will debate a player they think is a werewolf or not. 
Their words are like "Player x is a werewolf." or "Player x is a villager.".
What we know is :
1. Villager won't lie.
2. Werewolf may lie. 
Of cause we only consider those situations which obey the two rules above. 
It is guaranteed that input data exist at least one situation which obey the two rules above.
Now we can judge every player into 3 types :
1. A player which can only be villager among all situations, 
2. A player which can only be werewolf among all situations.
3. A player which can be villager among some situations, while can be werewolf in others situations.
You just need to print out the number of type-1 players and the number of type-2 players.
No player will talk about himself.

Input

The first line of the input gives the number of test cases T.Then T test cases follow.
The first line of each test case contains an integer N,indicating the number of players.
Then follows N lines,i-th line contains an integer x and a string S,indicating the i-th players tell you,"Player x is a S."
limits:
1≤T≤10
1≤N≤100,000
1≤x≤N
S∈ {"villager"."werewolf"}

Output

For each test case,print the number of type-1 players and the number of type-2 players in one line, separated by white space.

Sample Input

1
2
2 werewolf
1 werewolf

Sample Output

0 0

题目大意

现在有N个人,每个人要么是第一类:说的全是真话;要么是第二类:说的可能是假话。

每一个人x都指认另一个人y是第一类或第二类人。

请问在所有合法的情况下,有多少人必定是第一类人、有多少人必定是第二类人?

题目分析

为什么好多人第一眼看去2-sat?为什么有些人(MikuKnight)以为这真的是把狼人杀???

以上吐槽结束。题意已经大概写了一下了。

暴力想法

暴力做法的第一反应肯定是爆搜。

时间复杂度O(2^n)

推点性质

再一眼看去:诶这题意有点意思啊,为什么问的是“所有合法”情况下?

转念一想,那么如果每个人都说假话不也合法吗?

于是得到了这题的第一个性质:每一个人都可能是第二类人。

既然如此,只需要判断一个人能不能够成为第一类人就行了。

方法很简单,对于每一个人,假定他说真话合法,于是dfs下去检查是否矛盾。细节在于铁定说假话的人的话是没有后继效用的,意即他指认的第一类人并不一定合法。

时间复杂度O(n^2)

再推点性质

打多校时候同队的ZXB机智地猜了一条结论(以下简称“一定是第二类人的人”为“铁狼”):

结论的充分性是显然的,考虑其必要性。

首先一个人是铁狼的充要条件是其不可能成为第一类人。由上面的性质可以得到,问题的补集就变成是:哪些人不能成为第二类人。

由于每一个人会且只会指认一个人,最基础的判定方法自然是上一条中的dfs。

考虑指认关系形成的图中环的部分。若环中有超过一条“werewolf”边,在合法情况下,被指认人的判定可以是被信任的也可以是不被信任的;再者,由于问题询问的是“所有合法情况”,被指认人的后继因此也可以被信任或不被信任。换句话说,一旦被指认,他之后的所有后继都形同虚设、模棱两可。

再考虑图中非环部分。由于每个人都可能是第二类人,于是只需考虑第一类边。自然,非法情况当且仅当(直接或间接)指认铁狼为村民。

这样,就证明了性质的充要性。

可能会犯的错误1

把边(u,v,werewolf)的v的后继全部删去。

这个情况在环上成立然而链上不成立。

可能会犯的错误2

某些方法会出现的找环时候的错误。

挂张图,这个就不细说了我错在这里没发现好久,属于做题的经验,还是多积累吧。

后记

话说在这题上和法老拉平还是有点小激动啊

 #include<cstdio>
#include<cctype>
#include<cstring>
const int maxn = ; struct Edge
{
int y,opt;
}edges[maxn];
int T,n,ans;
char ch[];
int vis[maxn],deg[maxn],sv[maxn],cnt,tag;
int head[maxn],nxt[maxn];
int qead[maxn],qxt[maxn],qdges[maxn],edgeTot;
bool done[maxn]; int read()
{
char ch = getchar();
int num = ;
bool fl = ;
for (; !isdigit(ch); ch = getchar())
if (ch=='-') fl = ;
for (; isdigit(ch); ch = getchar())
num = (num<<)+(num<<)+ch-;
if (fl) num = -num;
return num;
}
void addedge(int u, int v)
{
qdges[++edgeTot] = v, qxt[edgeTot] = qead[u], qead[u] = edgeTot;
}
void dfs(int x)
{
vis[x] = ;
for (int i=head[x]; i!=-; i=nxt[i])
{
int v = edges[i].y;
if (vis[v]){
if (vis[v]==) vis[v] = , sv[++sv[]] = v;
vis[x] = ;
return;
}
dfs(v);
if (vis[v]==) vis[x] = ;
}
}
void search(int x, int top, int tot)
{
for (int i=head[x]; i!=-; i=nxt[i])
{
int v = edges[i].y, cnt = tot;
if (!edges[i].opt){
if (cnt){
tag = -;
return;
}
cnt = , tag = v;
}
if (v==top) return;
search(v, top, cnt);
}
}
void wit(int x)
{
if (done[x]) return;
done[x] = ;
for (int i=qead[x]; i!=-; i=qxt[i])
wit(qdges[i];);
}
int main()
{
// freopen("1009.in","r",stdin);
// freopen("1009.out","w",stdout);
T = read();
while (T--)
{
memset(head, -, sizeof head);
memset(qead, -, sizeof qead);
memset(done, , sizeof done);
memset(vis, , sizeof vis);
memset(deg, , sizeof deg);
memset(vv, , sizeof vv);
n = read(), edgeTot = sv[] = ans = ;
for (int i=; i<=n; i++)
{
edges[i].y = read(), scanf("%s",ch);
edges[i].opt = ch[]=='v';
nxt[i] = head[i], head[i] = i;
deg[edges[i].y]++;
if (edges[i].opt) addedge(edges[i].y, i);
}
for (int i=; i<=n; i++)
if (!vis[i]&&!deg[i]) dfs(i);
for (int i=; i<=n; i++)
if (!vis[i]) dfs(i);
for (int i=; i<=sv[]; i++)
{
cnt = , tag = -;
search(sv[i], sv[i], );
if (tag==-) continue;
wit(tag);
}
for (int i=; i<=n; i++)
if (done[i]) ans++;
printf("0 %d\n",ans);
}
return ;
}

END

【图论】hdu6370Werewolf的更多相关文章

  1. [leetcode] 题型整理之图论

    图论的常见题目有两类,一类是求两点间最短距离,另一类是拓扑排序,两种写起来都很烦. 求最短路径: 127. Word Ladder Given two words (beginWord and end ...

  2. 并查集(图论) LA 3644 X-Plosives

    题目传送门 题意:训练指南P191 分析:本题特殊,n个物品,n种元素则会爆炸,可以转移到图论里的n个点,连一条边表示u,v元素放在一起,如果不出现环,一定是n点,n-1条边,所以如果两个元素在同一个 ...

  3. NOIp 2014 #2 联合权值 Label:图论 !!!未AC

    题目描述 无向连通图G 有n 个点,n - 1 条边.点从1 到n 依次编号,编号为 i 的点的权值为W i ,每条边的长度均为1 .图上两点( u , v ) 的距离定义为u 点到v 点的最短距离. ...

  4. HDU 5521 [图论][最短路][建图灵感]

    /* 思前想后 还是决定坚持写博客吧... 题意: n个点,m个集合.每个集合里边的点是联通的且任意两点之间有一条dis[i]的边(每个集合一个dis[i]) 求同时从第1个点和第n个点出发的两个人相 ...

  5. SDUT 2141 【TEST】数据结构实验图论一:基于邻接矩阵的广度优先搜索遍历

    数据结构实验图论一:基于邻接矩阵的广度优先搜索遍历 Time Limit: 1000MS Memory Limit: 65536KB Submit Statistic Discuss Problem ...

  6. [转] POJ图论入门

    最短路问题此类问题类型不多,变形较少 POJ 2449 Remmarguts' Date(中等)http://acm.pku.edu.cn/JudgeOnline/problem?id=2449题意: ...

  7. HDU 5934 Bomb 【图论缩点】(2016年中国大学生程序设计竞赛(杭州))

    Bomb Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submis ...

  8. Codeforces 553C Love Triangles(图论)

    Solution: 比较好的图论的题. 要做这一题,首先要分析love关系和hate关系中,love关系具有传递性.更关键的一点,hate关系是不能成奇环的. 看到没有奇环很自然想到二分图的特性. 那 ...

  9. 图论(floyd算法):NOI2007 社交网络

    [NOI2007] 社交网络 ★★   输入文件:network1.in   输出文件:network1.out   简单对比 时间限制:1 s   内存限制:128 MB [问题描述] 在社交网络( ...

随机推荐

  1. 多线程 NSThread 了解

    用NSThread创建子线程的3种方法   //  DYFViewController.m //  623-02-pthread // //  Created by dyf on 14-6-23. / ...

  2. StringUtils中常用方法leftPad(),rightPad(),center()

    org.apache.commons.lang3的StringUtils 方法如下: public static String leftPadTime(Integer time){    return ...

  3. 在centos7下搭建nginx环境,并配置负载均衡,最终能达到通过域名直接访问的目的

    1.关于nginx:个人理解的nginx它的主要用途就是负载均衡,当然可能还有其他一些功能可能我们不长用到,我们通过nginx可以干什么呢?为什么要引入它呢?原因是当有高并发访问服务器时,服务器可能会 ...

  4. BZOJ1102(搜索)

    随便写一下的搜索,别的OJ深搜就过了,强大的BZOJ成功栈溢出RE了我并使我屈服地用广搜过掉,第一行手动开栈惨遭无视. 广搜: #pragma comment(linker, "/STACK ...

  5. datagridview连接数据库的简单实现

    对于需要从数据库获取数据的列,在DataPropertyName填写对应的列名字. 在Load函数中添加如下代码,先关闭自动生成列选项,然后得到一个查询的datatable,并将其绑定到datasou ...

  6. arcgis mdb 数据中的shp 如何合并一起

    如上操作  一直往下就可以啦 选择数据源 就可以了,然后就可以load  其他数据啦   ,坐标系要一直

  7. if __FILE__ == $0 end

    if __FILE__ == $0 end __FILE__是一个“具有魔力”的变量,它代表了当前文件名.$0是用于启动程序的文件名.那么代码“if __FILE__ == $0”便意味着检查此文件是 ...

  8. 搭建高可用mongodb集群(一)——mongodb配置主从模式

    转载自:LANCEYAN.COM 在大数据的时代,传统的关系型数据库要能更高的服务必须要解决高并发读写.海量数据高效存储.高可扩展性和高可用性这些难题.不过就是因为这些问题Nosql诞生了. NOSQ ...

  9. spring boot图片上传至远程服务器并返回新的图片路径

    界面上传图片时考虑到可能会有用户的图片名称一致,使用UUID来对图片名称进行重新生成. //UUIDUtils public class UUIDUtils { public static Strin ...

  10. Windows 10下mysql 64位 安装(mysql-5.7.11-winx64安装)

    Windows下mysql 64位 安装(mysql-5.7.11-winx64安装) 系统Windows10 安装包mysql-5.7.11-winx64.zip 安装过程中遇到的问题,请留意4.0 ...