有理有据的结论题

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. 51Nod 1272 最大距离 (栈或贪心)

    #include <cstdio> #include <queue> #include <cstring> #include <iostream> #i ...

  2. mongodb javaapi网页版链接

    http://www.open-open.com/doc/view/abe58dc8d0114ef2bd34d0bbccd3691e

  3. UILabel和UIbutton(富文本)封装方法

    /** 方法说明:设置label的富文本属性 参数说明:contentStr富文本内容 textColor字体颜色 rangeSet设置字体颜色及大小的位置 */ - (UILabel *)backf ...

  4. c/c++学习系列之内存对齐

    1.C++内存对齐规则 每个特定平台上的编译器都有自己的默认“对齐系数”(也叫对齐模数).程序员可以通过预编译命令#pragma pack(n),n=1,2,4,8,16来改变这一系数,其中的n就是你 ...

  5. c/c++学习系列之putchar、getchar、puts、gets的运用

    如果您只想取得使用者输入的字元,則可以使用getchar(),它直接取得使用者輸入的字元并传回,如果只想要输出一個字元,則也可以直接使用putchar(),以下是个简单的例子: #include &l ...

  6. [在读]web前端黑客技术揭秘

  7. Java微信公众平台开发(九)--微信自定义菜单的创建实现

    自定义菜单这个功能在我们普通的编辑模式下是可以直接在后台编辑的,但是一旦我们进入开发模式之后我们的自定义菜单就需要自己用代码实现,所以对于刚开始接触的人来说可能存在一定的疑惑,这里我说下平时我们在开发 ...

  8. File "<stdin>" , line 1

    写了一个hello.py,仅有一句,print 'hello world', 运行 Python hello.py 出错,提示: File "<stdin>" , li ...

  9. Y2165终极分班考试题。

    第一题答案:D 2.下面关于SQLServer中视图的说法错误的是:C 答案:视图是数据中存储的数据值得集合. 3.在JAVA中,关于日志记录工具log4j的描述错误的是:D 答案:log4j个输出级 ...

  10. React 实践记录 01 组件开发入门

    Introduction 本文组成: Ryan Clark文章Getting started with React的翻译. 博主的实践心得. React由Facebook的程序员创建,是一个非常强大的 ...