COUNT函数之前不能调用取值
作者:
Kue
,
2020-08-29 22:28:09
,
所有人可见
,
阅读 598
若对pos先取值,则count函数会失效
cin >> a >> b;
// int x = pos[a], y = pos[b]; 这若用pos先取值,cont就没有效果了
if (pos.count(a) == 0 && pos.count(b) == 0) printf("ERROR: %d and %d are not found.\n", a, b);
else if (pos.count(a) == 0) printf("ERROR: %d is not found.\n", a);
else if (pos.count(b) == 0) printf("ERROR: %d is not found.\n", b);
else{
// 为啥count之前不能加 取值呢?
int x = pos[a], y = pos[b];
while (d[x] != d[y])
{
if (d[x] > d[y]) x = p[x];
else y = p[y];
}
// 判断是否相同
while (x != y)
{
x = p[x];
y = p[y];
}
if (in[x] == b) printf("%d is an ancestor of %d.\n", b, a);
else if (in[x] == a) printf("%d is an ancestor of %d.\n", a, b);
else printf("LCA of %d and %d is %d.\n", a, b, in[x]);
}
你这个是map?你开始用了一次pos[a],就相当于在红黑树(map)里进行了一次查询,或者说赋值,就在map里建立了a的映射,此时数字型的map,默认是0,pos[a]=0。但是count变为1.。你自己试一下,只要出现map[x],不管赋不赋值,count就变为1
是的