首页 > 其他分享 >B. Gardener and the Array

B. Gardener and the Array

时间:2023-01-15 18:33:52浏览次数:63  
标签:right bar cup ab test Array Gardener left

B. Gardener and the Array

The gardener Kazimir Kazimirovich has an array of $n$ integers $c_1, c_2, \dots, c_n$.

He wants to check if there are two different subsequences $a$ and $b$ of the original array, for which $f(a) = f(b)$, where $f(x)$ is the bitwise OR of all of the numbers in the sequence $x$.

A sequence $q$ is a subsequence of $p$ if $q$ can be obtained from $p$ by deleting several (possibly none or all) elements.

Two subsequences are considered different if the sets of indexes of their elements in the original sequence are different, that is, the values of the elements are not considered when comparing the subsequences.

Input

Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^5$). The description of the test cases follows.

The first line of each test case contains one integer $n$ ($1 \le n \le 10^5$) — the size of the array $c$.

The description of the array $c$ in this problem is given implicitly to speed up input.

The $(i + 1)$-st of the following $n$ lines of the test case begins with an integer $k_i$ ($1 \le k_i \le 10^5$) — the number of set bits in the number $c_i$. Next follow $k_i$ distinct integers $p_{i, 1}, p_{i, 2}, \dots, p_{i, k_i}$ ($1 \le p_i \le 2 \cdot 10^5$) —the numbers of bits that are set to one in number $c_i$. In other words, $c_i = 2^{p_{i, 1}} + 2^{p_{i, 2}} + \ldots + 2^{p_{i, k_i}}$.

It is guaranteed that the total sum of $k_i$ in all tests does not exceed $10^5$.

Output

For each set of input, print "Yes" if there exist two different subsequences for which $f(a) = f(b)$, and "No" otherwise.

You can output the answer in any case (upper or lower). For example, the strings "$\text{yEs}$", "$\text{yes}$", "$\text{Yes}$", and "$\text{YES}$" will be recognized as positive responses.

Example

input

5
3
2 1 5
2 2 4
2 2 3
2
2 1 2
1 2
4
3 1 2 4
2 2 4
4 1 2 5 6
2 2 5
5
3 3 1 2
3 2 5 3
5 7 2 3 1 4
5 1 2 6 3 5
3 2 6 3
2
1 1
1 2

output

No
Yes
Yes
Yes
No

Note

It can be proven that in the first test case there are no two different subsequences $a$ and $b$ for which $f(a) = f(b)$.

In the second test case, one of the possible answers are following subsequences: the subsequence $a$ formed by the element at position $1$, and the subsequence $b$ formed by the elements at positions $1$ and $2$.

In the third test case, one of the possible answers are following subsequences: the subsequence $a$ formed by elements at positions $1$, $2$, $3$ and $4$, and the subsequence $b$ formed by elements at positions $2$, $3$ and $4$.

 

解题思路

  感觉官方给出的题解写得不是很好,给出的证明跟没证一样。

  首先容易想到,如果存在两个数$x,y$各自在二进制下为$1$的位所构成的集合属于包含关系,例如$x = 2^1 + 2^3 + 3^4$在二进制下为$1$的位所构成的集合为$\{ 1,3,4 \}$,$y = 2^1 + 2^4$对应的集合为$\{ 1,4 \}$,那么存在包含关系$\{ 1,4 \} \subseteq \{ 1,3,4 \}$,为了方便这里标记为$x \subseteq y$。那么我们就可以构造出一组解,其中子数组$a$取全集$U$(即全部的数),子数组$b$取$U - \{ x \}$(这里假设$x \subseteq y$),很明显$a \ne b$且$f(a) = f(b)$(这里的$a,b$指的是数的集合)。

  这就证明了如果存在两个数的二进制位属于包含关系,那么有解。上面证明了充分性,题解只通过充分性就推出“如果不存在两个数的二进制位属于包含关系,那么无解”,很明显逻辑有问题,因此还要证明这个命题的必要性,即“如果有解,那么存在两个数的二进制位属于包含关系”,从而根据逆否命题推出等价命题“如果不存在两个数的二进制位属于包含关系,那么无解”。

  如果有解,那么存在子数组$a$和$b$满足$a \ne b$且$f(a) = f(b)$。那么有

\begin{align*}
f(U) &= f \left( b \cup (U-b) \right) \\
&= f(b) \mid f(U - b) \\
&= f(a) \mid f(U - b) \\
&= f \left( a \cup (U - b) \right) \\
&= f \left( a \cup \bar{b} \right) \\
&= f \left( (a \cup \bar{b}) \cap U \right) \\
&= f \left( (a \cup \bar{b}) \cap (b \cup \bar{b}) \right) \\
&= f \left( ab \cup a \bar{b} \cup \bar{b} \right) \\
&= f \left( ab \cup \bar{b} (a \cup U) \right) \\
&= f \left( ab \cup (U - b) \right)
\end{align*}

  其中保证$a \ne \phi$和$b \ne \phi$。

  通过上面的推导可以得到$f(U) = f \left( ab \cup (U - b) \right)$,并且由于$a \ne b$,因此$ab \cup (U - b) \subset U$(反证法,如果$ab \cup (U - b) = U$,由于$ab \cup (U - b) =  ab \cup (U \cap \bar{b}) = ab \cup \bar{b} = U$,而$b \cup \bar{b} = U$,因此$ab = b$,即$a = b$,矛盾),意味着至少存在两个数的二进制位属于包含关系。这是因为将被包含的数从全集$U$中删除,得到真子集$ab \cup (U - b)$,同时还满足$f(U) = f \left( ab \cup (U - b) \right)$。必要性得到证明。

  因此得到证明:如果存在两个数的二进制位属于包含关系 $\Leftrightarrow$ 有解,从而可以推出逆否命题:如果不存在两个数的二进制位属于包含关系,那么无解。

  因此接下来只需要开个哈希表枚举统计每个数在二进制下为$1$的位出现的次数,然后再枚举每个数,看看该数能否被其他数包含(等价于看看该数在二进制下所有是$1$的位在哈希表中出现次数是否都大于等于$2$),如果是那么就存在解。如果全部数都无法被其他数包含那么就无解。

  AC代码如下:

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 
 4 const int N = 1e5 + 10;
 5 
 6 vector<int> p[N];
 7 
 8 void solve() {
 9     int n;
10     scanf("%d", &n);
11     unordered_map<int, int> cnt;
12     for (int i = 0; i < n; i++) {
13         int m;
14         scanf("%d", &m);
15         p[i].clear();
16         while (m--) {
17             int x;
18             scanf("%d", &x);
19             cnt[x]++;
20             p[i].push_back(x);
21         }
22     }
23     for (int i = 0; i < n; i++) {
24         bool flag = true;
25         for (auto &x : p[i]) {
26             if (cnt[x] < 2) {
27                 flag = false;
28                 break;
29             }
30         }
31         if (flag) {
32             printf("Yes\n");
33             return;
34         }
35     }
36     printf("No\n");
37 }
38 
39 int main() {
40     int t;
41     scanf("%d", &t);
42     while (t--) {
43         solve();
44     }
45     
46     return 0;
47 }

 

参考资料

  Codeforces Round #843 (Div. 2) Editorial:https://codeforces.com/blog/entry/111286

标签:right,bar,cup,ab,test,Array,Gardener,left
From: https://www.cnblogs.com/onlyblues/p/17053892.html

相关文章