Aryan loves graph theory more than anything. Well,...
تم الإنشاء في: ٢١ يونيو ٢٠٢٥
تمت الإجابة باستخدام O4 Mini High بواسطة Chat01
تم الإنشاء في: ٢١ يونيو ٢٠٢٥
تمت الإجابة باستخدام O4 Mini High بواسطة Chat01
Aryan loves graph theory more than anything. Well, no, he likes to flex his research paper on line graphs to everyone more. To start a conversation with you, he decides to give you a problem on line graphs. In the mathematical discipline of graph theory, the line graph of a simple undirected graph is another simple undirected graph that represents the adjacency between every two edges in .
Precisely speaking, for an undirected graph without self-loops or multiple edges, its line graph is a graph such that
Also, and for .
An Euler trail is a sequence of edges that visits every edge of the graph exactly once. This trail can be either a path (starting and ending at different vertices) or a cycle (starting and ending at the same vertex). Vertices may be revisited during the trail, but each edge must be used exactly once.
Aryan gives you a simple connected graph with vertices and edges and an integer , and it is guaranteed that has an Euler trail and it is not a path graph. He asks you to determine if has an Euler trail.
A path graph is a tree where every vertex is connected to atmost two other 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 three space-separated integers , , and (, , ).
The next lines of each test case contain two space-separated integers and (, ), denoting that an edge connects vertices and .
It is guaranteed that the sum of and over all test cases does not exceed .
Output
For each testcase, print "YES" if has an Euler trail; 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
4
5 5 2
1 2
2 3
3 4
4 5
5 1
5 6 1
1 2
2 3
3 4
4 5
5 1
1 3
10 11 3
1 2
2 3
3 4
4 5
4 6
4 7
5 7
6 7
7 8
8 9
9 10
7 8 2
1 3
2 3
1 4
4 5
2 5
1 6
6 7
2 7
OutputCopy
YES
NO
YES
NO
Note
For the first test case, is isomorphic to itself. So, since has an Euler trail, also has an Euler trail.
For the second test case, looks as follows(Vertex of in figure corresponds to edge between vertices and of ). It can be proven that this doesn't have an Euler trail.
中文c++回答。
思路很简单:记原图中度数为奇数的顶点集合 ,由于题目保证原图恰有欧拉迹,所以 。
如果 ,原图有欧拉回路。可以证明任意 时 中所有顶点度数都为偶数,故始终有欧拉回路,直接输出 YES。
如果 ,设两奇顶点为 。对任意 ,在 中度数为奇(也就是“奇顶点”)的数目恰好等于原图中所有长度为 的边迹(trail)两端分别位于 “奇/偶” 顶点的总数。
整体时间复杂度 (对每个测试过所有顶点一次,链长累加不会超过 ),非常高效。
下面是完整的 C++ 实现(用中文注释):
cpp#include <bits/stdc++.h> using namespace std; using ll = long long; int main(){ ios::sync_with_stdio(false); cin.tie(nullptr); int T; cin >> T; while(T--){ int n, m, k; cin >> n >> m >> k; vector<vector<int>> adj(n+1); vector<int> deg(n+1, 0); for(int i = 0; i < m; i++){ int u, v; cin >> u >> v; adj[u].push_back(v); adj[v].push_back(u); deg[u]++; deg[v]++; } // 找奇度顶点 vector<int> odd; for(int i = 1; i <= n; i++){ if(deg[i] & 1) odd.push_back(i); } // 情况1:欧拉回路 if(odd.size() == 0){ cout << "YES\n"; continue; } // 情况2:欧拉迹,恰有两个奇度顶点 if(odd.size() == 2){ int s = odd[0], t = odd[1]; // 计算从 x 出发沿度=2链的最大步数 auto calc = [&](int x)->int{ // 如果不是叶子,立刻返回 0 if(deg[x] != 1) return 0; int prev = x; int cur = adj[x][0]; // 唯一的邻居 int depth = 1; // 只要当前节点度=2,就只能沿着“另一个”邻居继续走 while(deg[cur] == 2){ int nxt = adj[cur][0] == prev ? adj[cur][1] : adj[cur][0]; prev = cur; cur = nxt; depth++; } return depth; }; int ds = calc(s); int dt = calc(t); int mx = min(ds, dt); // 只有 k<=mx 时,L^k(G) 才可能有欧拉迹 cout << (k <= mx ? "YES\n" : "NO\n"); } else { // 理论上不会出现其他情况 cout << "NO\n"; } } return 0; }
说明