有理有据的结论题

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. [原创]内网渗透专用SSH连接工具sshcmd/sshshell/ssh密码破解以及Kali开启SSH

    目录 1.Kali开启SSH 2.SSH连接工具优缺点 3.渗透专用SSH连接工具 4.ssh执行cmd源码 5.批量SSH密码破解 6.相关工具下载 0x001 SSH配置 1.打开文件 etc/s ...

  2. 自然语言处理(五)——实现机器翻译Seq2Seq完整经过

    参考书 <TensorFlow:实战Google深度学习框架>(第2版) 我只能说这本书太烂了,看完这本书中关于自然语言处理的内容,代码全部敲了一遍,感觉学的很绝望,代码也运行不了. 具体 ...

  3. PHP保留小数的相关方法

    结合一下网上的例子 $num = 10.4567; //第一种:利用round()对浮点数进行四舍五入 但是这个如果没有两位小数也不会"两位精度" echo round($num, ...

  4. Babel 处理 webpack 无法处理的 ES 语法(Babel 6.x 和 Babel 7.x 有区别)(二)

    //这是main.js是我们项目的js入口文件 //1:a导入jQuery,引入jQuery包用$变量接收,此方法与node中 const $ = require('jquery') 同 //此为ES ...

  5. go系列(5)- beego自己写controller

    前边的系列文章已经讲述了如何安装环境, beego的处理逻辑都是在Controller里面完成的,下面就写一个最简单的Controller. 我们在写自己的controller的时候,一定要继承bee ...

  6. JMeter中的HTTPS套接字错误

    Apache JMeter对启用SSL的应用程序执行性能和/或负载测试时,SSL套接字错误可能是经常遇到的麻烦,严重阻碍了您的测试工作.本文重点介绍如何通过相应地配置和调优JMeter来克服这些与连接 ...

  7. 【bzoj3033】太鼓达人

    3033: 太鼓达人 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 521  Solved: 399[Submit][Status][Discuss] ...

  8. python入门之lambda表达式、内置函数

    lambda 简单的函数就可以用lambda表达式 格式: abc = lambda a1,a2:a1+a2 其中,传参是a1和a2,返回值是a1+a2 内置函数 abs(a) #取绝对值 all(a ...

  9. 执行脚本 提示 command not found

    问题现象: 初学shell,写了个脚本, 1.从windows 写好 脚本,然后部署到 linux 上. 2.chmod +x之后执行提示command not found,系统环境redhat9,用 ...

  10. C51 笔记

    一 关于宏常量的长度:C51中定义一个常数宏(默认是16位的),如果用宏表示一个32位的宏而不加'L'标志的话就会出错.如 #define BLOCK_A_BASEADDR  18*64*1024 / ...