[USACO | UPC] Liars and Truth Tellers | 拓展域并查集

简介: 题目描述After spending so much time around his cows, Farmer John has started to understand their language. Moreover, he notices that among his N cows (2 ≤ N ≤ 1000 ), some always tell the truth while others always lie.

网址链接或者是链接


题目描述


After spending so much time around his cows, Farmer John has started to  understand their language. Moreover, he notices that among his N cows (2 ≤ N ≤ 1000 ), some always tell the truth while others always lie.


FJ carefully listens to M statements (1 <= M <= 10,000) from his cows, each of the form “x y T”, meaning that “cow x claims cow y always tells the truth” or “x y L”, meaning that “cow x claims cow y always tells lies”.

Each statement involves a pair of different cows, and the same pair of cows may appear in multiple statements.


Unfortunately, FJ believes he might have written down some entries in his list incorrectly, so there may not be a valid way to designate each cow as a truth teller or a liar that is consistent with all the M statements on FJ’s list. To help FJ salvage as much of his list as possible, please compute the largest value of A such that there exists a valid way to designate each cow as a truth teller or a liar in a manner that is consistent with the first A entries in FJ’s list.


输入


  • Line 1: Two space-separated integers, N and M.
  • Lines 2…1+M: Each line is of the form “x y L” or “x y T”, describing a statement made by cow x about cow y.


输出


  • Line 1: The maximum value of A such that the first A entries in FJ’s list can be consistent with some assignment of “truth teller” or “liar” to the N cows.


样例输入


4 3
1 4 L
2 3 T
4 1 T


样例输出


2


提示


There are 4 cows and 3 statements. Cow 1 says that cow 4 lies, cow 2 says that cow 3 tells the truth, and cow 4 says that cow 1 tells the truth.Statements 1 and 3 cannot both be satisfied at the same time, but

statements 1 and 2 can be, if we let cows 1…3 tell the truth and cow 4 be a liar.


在思考这个题的时候,想到过之前做过的一道类似的题目,当时也做了总结:链接,这一类叫做拓展域 并查集


对于这个题目:

首先我们可以知道,每个点都有两个属性,真或者是假

对于a b L的情况,我们可以知道{

这句话表达的意思是a说b假

结果会有两种,a真b假或者是a假b真

}

对于a b T的情况,我们可以知道{

这句话表达的意思是a说b真

结果会有两种,a真b真,a假b假

}

所以说这里我们就可以用并查集来维护每个点的真假,如果说同一个点出现了两个真假状态,那就是矛盾的情况


int n,m;
int fa[maxn];
void init() {
  for(int i=1; i<=2*n; i++) fa[i] = i;
}
int find(int x) {
  if(x == fa[x]) return x;
  else return fa[x] = find(fa[x]);
}
int main() {
  n = read,m = read;
  init();
  int x,y;
  char c;
  int flag = 1,ans = 0,pos = 0;
  for(int i=1; i<=m; i++) {
    scanf("%d %d %c",&x,&y,&c);
    if(c == 'L') {
      fa[find(n + x)] = find(y);
      fa[find(n + y)] = find(x);
    } else {
      fa[find(x)] = find(y);
      fa[find(n + x)] = find(n + y);
    }
    if(find(y) == find(n + y)) {
      flag = 0;
      pos = i-1;
    }
    if(flag) ans ++;
  }
//  assert(pos == flag);
  cout << ans << endl;
  return 0;
}



目录
相关文章
【CCCC】L3-018 森森美图 (30分),计算几何+判断三点共线+bfs最短路
【CCCC】L3-018 森森美图 (30分),计算几何+判断三点共线+bfs最短路
147 0
|
安全
【CCCC】L3-009 长城 (30分),计算几何+凸包,极角排序
【CCCC】L3-009 长城 (30分),计算几何+凸包,极角排序
142 0
2019ICPC上海K-Color Graph(二分图 状压枚举)
2019ICPC上海K-Color Graph(二分图 状压枚举)
76 0
|
人工智能 BI
upc-2021个人训练赛第27场 D: Values(思维+并查集)
upc-2021个人训练赛第27场 D: Values(思维+并查集)
81 0
UPC-星区划分(并查集+枚举)
UPC-星区划分(并查集+枚举)
82 0
|
Java Shell
Codeforces Round #746 (Div. 2) D - Hemose in ICPC ?(交互 二分 欧拉序)
Codeforces Round #746 (Div. 2) D - Hemose in ICPC ?(交互 二分 欧拉序)
153 0
|
人工智能 BI
UPC Decayed Bridges(并查集+思维)
UPC Decayed Bridges(并查集+思维)
101 0
[UVA1364 | POJ | NC]Knights of the Round Table | Tarjan 求点双 | 二分图 | 综合图论
我们可以很轻松地发现,被提出的都是在点双连通分量之外的,比如该图中的1 和 5 ,那么怎么判断哪些点不在环中呢? 此时我们还可以逆向思考,不 在 环 中 的 = = 总 的 − 在 环 中 的,所以说现在问题就转换成了满足条件的环内的点的个数
126 0
[UVA1364 | POJ | NC]Knights of the Round Table | Tarjan 求点双 | 二分图 | 综合图论
All in the Family_upc_模拟 or lca + 并查集
The scientists working at the Family Genetics Institute are tracing the spread of a hereditary disease through family trees. They have started by listing the family members that have the disease,
116 0
All in the Family_upc_模拟 or lca + 并查集
|
Go
[CCPC 2019] 厦门 | Zayin and Obstacles | 三维差分前缀 + bfs
Description Recently, Zayin became obsessed with a tower defense game called Arknights. The most special level is the 5th level of chapter 4: Don’t panic. The most special thing about this level is that an enemy will continue taking radioactive damage after passing through the Active Originiums.
144 0