As we all know, Aryan is a funny guy. He decides t...
創建於:2025年6月21日
使用 O4 Mini High 回答 Chat01
創建於:2025年6月21日
使用 O4 Mini High 回答 Chat01
As we all know, Aryan is a funny guy. He decides to create fun graphs. For a graph , he defines fun graph of as follows:
As we all know again, Harshith is a superb guy. He decides to use fun graphs to create his own superb graphs. For a graph , a fun graph G' ' is called a superb graph of if G' ' has the minimum number of vertices among all possible fun graphs of .
Aryan gives Harshith simple undirected graphs , all on the same vertex set . Harshith then wonders if there exist other graphs , all on some other vertex set such that:
Help Harshith solve his wonder.
For a graph , a subset of vertices is called an independent set if no two vertices of are connected with an edge.
For a graph , a subset of vertices is called a clique if every vertex of is connected to every other vertex of with an edge.
A graph is a simple undirected graph if its edges are undirected and there are no self-loops or multiple edges between the same pair of vertices.
Input
Each test contains multiple test cases. The first line contains the number of test cases (). The description of the test cases follows.
The first line of each test case contains two integers and ().
Then, there are graphs described. The first line of each graph description contains a single integer ( ).
Next lines each contain two space-separated integers and (), denoting that an edge connects vertices and .
It is guaranteed that the sum of over all graphs over all test cases does not exceed , and the sum of over all test cases does not exceed .
Output
For each testcase, print "Yes" if there exists graphs satisfying the conditions; otherwise, "No".
You can output the answer in any case (upper or lower). For example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as positive responses.
Example
InputCopy
3
5 2
3
3 4
5 3
5 1
6
3 5
3 4
1 4
1 2
2 3
4 2
4 3
0
3
3 1
1 4
1 2
4
4 2
4 3
1 2
2 3
3 2
0
3
3 1
3 2
1 2
OutputCopy
Yes
Yes
No
Note
For the first test case, the following are the graphs of and such that is superb graph of and is superb graph of .
In each graph, vertex of corresonds to independent set of corresponding and remaining vertices of correspond to independent set/clique in corresponding (a single vertex set can be considered both an independent set and a clique).
In the third test case, it can be proven that the answer is "No".
中文c++回答。
下面的做法基于这样一个观察:
另一方面,若所有 都是无模的,那么只要对每个 取
此时每个顶点单独成块(大小 1 的块显然既是团又是独立集),就能保证:
于是问题简化为:检查每个输入图 是否无模。若有任意一个带模(存在“同邻”或“同非邻”)的 ,答案即为 “No”,否则为 “Yes”。
下面给出基于 C++ 的高效实现,用 bitset
加速模检:
cpp#include <bits/stdc++.h> using namespace std; int main(){ ios::sync_with_stdio(false); cin.tie(nullptr); int T; cin >> T; while(T--){ int n, k; cin >> n >> k; vector< vector< bitset<300> > > all_bs(k, vector<bitset<300>>(n)); for(int i = 0; i < k; i++){ int m; cin >> m; // 读边并填邻接 for(int e = 0; e < m; e++){ int u, v; cin >> u >> v; --u; --v; all_bs[i][u].set(v); all_bs[i][v].set(u); } } bool ok = true; // 对每个图,检验是否存在一对顶点 u<v 构成 module for(int i = 0; i < k && ok; i++){ auto &bs = all_bs[i]; for(int u = 0; u < n && ok; u++){ for(int v = u+1; v < n; v++){ // 检查 u,v 除彼此外的邻接是否完全一致 // 构造两个 bitset 副本,屏蔽掉对彼此的位 bitset<300> bu = bs[u], bv = bs[v]; bu.reset(v); bv.reset(u); if(bu == bv){ // 找到一对同邻/同非邻 ok = false; break; } } } } cout << (ok ? "Yes\n" : "No\n"); } return 0; }
复杂度分析
bitset<300>
(5~6 个 64 位字)做一次按位异或并全零检查,开销常数非常小。