Aryan loves graph theory more than anything. Well,...

تم الإنشاء في: ٢١ يونيو ٢٠٢٥

تمت الإجابة باستخدام 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 GG is another simple undirected graph L(G)L(G) that represents the adjacency between every two edges in GG.

Precisely speaking, for an undirected graph GG without self-loops or multiple edges, its line graph L(G)L(G) is a graph such that

  • Each vertex of L(G)L(G) represents an edge of GG.
  • Two vertices of L(G)L(G) are adjacent if and only if their corresponding edges share a common endpoint in GG.

Also, L0(G)=GL^0(G)=G and Lk(G)=L(Lk1(G))L^k(G)=L(L^{k-1}(G)) for k1k\geq 1.

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 GG with nn vertices and mm edges and an integer kk, and it is guaranteed that GG has an Euler trail and it is not a path graph^{\text{∗}}. He asks you to determine if Lk(G)L^k(G) has an Euler trail.

^{\text{∗}}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 tt (1t1041 \le t \le 10^4). The description of the test cases follows.

The first line of each test case contains three space-separated integers nn, mm, and kk (5n21055 \le n \le 2 \cdot 10^5, n1mmin(n(n1)2,2105)n-1 \le m \le \min(\frac{n\cdot(n-1)}{2} ,2 \cdot 10^5), 1k21051 \le k \le 2 \cdot 10^5).

The next mm lines of each test case contain two space-separated integers uu and vv (1u,vn1 \le u,v \le n, uvu \neq v), denoting that an edge connects vertices uu and vv.

It is guaranteed that the sum of nn and mm over all test cases does not exceed 21052\cdot 10^5.

Output

For each testcase, print "YES" if Lk(G)L^k(G) 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, L2(G)L^2(G) is isomorphic to GG itself. So, since GG has an Euler trail, L2(G)L^2(G) also has an Euler trail.

For the second test case, L(G)L(G) looks as follows(Vertex iji-j of L(G)L(G) in figure corresponds to edge between vertices ii and jj of GG). It can be proven that this doesn't have an Euler trail.

中文c++回答。

الفكرة

Providing C++ solution

The user wants a solution in Chinese with C++ code addressing Euler trails in the line graph of a given graph. This involves reasoning about whether L^k(G) has an Euler trail. I'll tackle the problem algorithmically and write code in C++ to check for the conditions of an Euler trail. I'll also explain the solution in Chinese, ensuring that the explanation and code are clear and understandable without referencing external resources. Let's prepare that explanation and code!

Analyzing Euler trails in line graphs

The goal is to check if the k-th iterated line graph of a connected graph with an Euler trail also has an Euler trail. I’ve observed that if L(G) has an Euler trail, you must have at most 2 vertices with odd degrees. For L(G), a vertex corresponds to an edge in G and has the degree sum of its vertices. The connectivity of L(G) depends on whether G is connected and not a star. I’ll need to check these conditions and ensure the graph’s connectivity for multiple test cases.

Tracking Euler trails in iterated line graphs

I’m thinking about the conditions that determine whether iterated line graphs Lk(G)L^k(G) have an Euler trail. The shape of the graph matters: For a connected graph with an Euler trail that's not a path graph, I have to determine if the iterated line graphs are connected and if odd degree vertices are within limits.

The key idea is tracking the parity of degrees across iterations, with a closed-form logic for deciding YES/NO based on the number of vertices with odd degrees. I'll also need to account for the stability of the line graph over multiple iterations!

Evaluating Euler trails in iterated line graphs

I'm thinking through the pattern of Euler trails in iterated line graphs. Given the degrees of vertices in the original graph, it’s clear that we track which vertices have odd degrees across iterations. This helps determine if the line graph retains connectivity and an Euler trail. There seems to be a pattern in computing whether Euler trails appear, especially for cycles and additional edges, and I can derive a closed-form solution that checks if the later graphs have trails.

Analyzing Euler conditions

I'm examining the conditions for an Euler trail and how odd-degree vertices impact it. Specifically, for G1, I need to consider the degrees of odd vertices and ensure their sum satisfies the rule: no more than two odd vertices after modification. This can happen if both odd vertices have a degree of 1 and are non-adjacent. If their degrees are higher, some combinations can lead to more connections, breaking the condition. Let's check how this plays out across different graph configurations.

Thought for 2m 14s

الإجابة

思路很简单:记原图中度数为奇数的顶点集合 OO,由于题目保证原图恰有欧拉迹,所以 O{0,2}\lvert O\rvert\in\{0,2\}

  1. 如果 O=0\lvert O\rvert=0,原图有欧拉回路。可以证明任意 k1k\ge1Lk(G)L^k(G) 中所有顶点度数都为偶数,故始终有欧拉回路,直接输出 YES

  2. 如果 O=2\lvert O\rvert=2,设两奇顶点为 s,ts,t。对任意 kk,在 Lk(G)L^k(G) 中度数为奇(也就是“奇顶点”)的数目恰好等于原图中所有长度为 kk 的边迹(trail)两端分别位于 “奇/偶” 顶点的总数。

    • 如果 sstt 的原图度数不为 1,则从它出发的 1 条边迹就有多于 1 条(分支),故 k=1k=1 即失败;更大 kk 肯定也失败。
    • 否则,假设 ss(或 tt)是度 1 的叶子,则从它沿着度数恰好为 2 的“链”一直走到第一个度 2\ne2 的结点(或者走到 tt),在此之前都是唯一可走的路,每走一条边 kk 就+1。记这样的最大步数为 dsd_s,同理算出 dtd_t
    • 那么恰有且仅有当且仅当 kmin(ds,  dt) k \le \min(d_s,\;d_t) 时,ok2\displaystyle o_k\le2,才有欧拉迹;否则答案是 NO

整体时间复杂度 O(n+m)O(n+m)(对每个测试过所有顶点一次,链长累加不会超过 nn),非常高效。

下面是完整的 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; }

说明

  • 首先统计原图中奇度顶点数;
  • 若无奇度顶点,原图有欧拉回路,任意 kk 下都成立;
  • 若恰有两个奇度顶点,必须它们都是度 1 的叶子且不走出“唯一链”就到不了分叉/另一端,否则一开始或中途就会产生多条不同的尾迹使奇点数超过 2。
  • 因此对每个叶子沿度 2 的链一直往前推,算出它能“唯一延伸”的最大边数 dd,若 kk 超过两端中的最小值,答案必然是 NO

مشاركة هذا السؤال والجواب