首页 > 其他分享 >D. Fish Graph

D. Fish Graph

时间:2023-05-06 20:34:37浏览次数:44  
标签:环上 subgraph int Graph Fish contains 鱼图 edges

D. Fish Graph

You are given a simple undirected graph with $n$ nodes and $m$ edges. Note that the graph is not necessarily connected. The nodes are labeled from $1$ to $n$.

We define a graph to be a Fish Graph if it contains a simple cycle with a special node $u$ belonging to the cycle. Apart from the edges in the cycle, the graph should have exactly $2$ extra edges. Both edges should connect to node $u$, but they should not be connected to any other node of the cycle.

Determine if the graph contains a subgraph that is a Fish Graph, and if so, find any such subgraph.

In this problem, we define a subgraph as a graph obtained by taking any subset of the edges of the original graph.

Visualization of example 1. The red edges form one possible subgraph that is a Fish Graph.

Input

The first line of input contains the integer $t$ ($1 \leq t \leq 1000$), the number of test cases. The description of test cases follows.

The first line of each test case contains two integers, $n$ and $m$ ($1 \le n, m \le 2\,000$) — the number of nodes and the number of edges.

Each of the next $m$ lines contains the description of an edge. Each line contains two integers $u_i$ and $v_i$ ($1 \leq u_i, v_i \leq n$, $u_i\neq v_i$) — an edge connects node $u_i$ to node $v_i$.

It is guaranteed that no two edges connect the same unordered pair of nodes.

Furthermore, it is guaranteed that the sum of $n$ and the sum of $m$ over all test cases both do not exceed $2\,000$.

Output

For each testcase, output "YES" if the graph contains a subgraph that is a Fish Graph, otherwise print "NO". If the answer is "YES", on the following lines output a description of the subgraph.

The first line of the description contains one integer $k$ — the number of edges of the subgraph.

On the next $k$ lines, output the edges of the chosen subgraph. Each of the $k$ lines should contains two integers $u$ and $v$ ($1\le u, v\le n$, $u\neq v$) — the edge between $u$ and $v$ belongs to the subgraph. The order in which $u$ and $v$ are printed does not matter, as long as the two nodes are connected by an edge in the original graph. The order in which you print the edges does not matter, as long as the resulting subgraph is a fish graph.

If there are multiple solutions, print any.

Example

input

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

output

YES
6
5 4
6 4
4 3
1 4
2 1
3 2
YES
5
5 3
2 3
3 1
4 3
1 4
NO

Note

In the first example, a possible valid subgraph contains the cycle $1 \rightarrow 2 \rightarrow 3 \rightarrow 4 \rightarrow 1$. The special node of this cycle is node $4$. The two extra edges $4 - 5$ and $4 - 6$ are both connected to $4$, completing the Fish Graph.

In the second example, a possible valid subgraph contains the cycle $1 \rightarrow 3 \rightarrow 4 \rightarrow 1$. The special node of this cycle is node $3$. The two extra edges $3 - 2$ and $3 - 5$ are both connected to $3$, completing the Fish Graph.

In the last example, it can be proven that there is no valid subgraph.

 

解题思路

  首先可以想到枚举每一个点$u$,把这个点当作鱼图中的特殊点,看看这个点是否满足在某个环内且连着另外两个不在环中的点。因此容易知道如果某个子图是鱼图,那么这个鱼图中的特殊点$u$的度数至少为$4$,其中由于$u$在某个环内因此就贡献了$2$个度数,另外还要再连两个不在环中的点又贡献了$2$个度数,因此至少有$4$个度数。

  那么如果某个点的度数至少为$4$能否说明这个点是某个鱼图的特殊点呢?并不能,参考下图:

  对于一个度数至少为$4$的点,还应该要满足这个点在某个环内且存在另外两个不在环中直接相连的点,这时才可以判断这个点是某个鱼图的特殊点。

  因此现在的问题就变成了给定一个点$u$,如何找到一个$u$所在的环,且还存在两个不在环中的点与$u$相连。然而包含$u$的环可能有非常多,如果直接暴搜来找的话肯定会超时,因此我们尝试去想能不能只找长度最小的环,且使得存在两个不在环中的点。要证明这点,只需证明对于任意一个鱼图,如果$u$不在最小环那么一定可以变成最小环且仍然是鱼图。

  假设另外两个直接相连的点$a$和$b$都不在最小环上,那么直接把环变成最小环即可。如果$a$或$b$有一个在最小环上(不失一般性假设为$a$),由于当前环的长度至少要比最小环多$1$,意味着当前环上至少存在一个点不在最小环上,因此这个点可以与$b$作为不在最小环上且与$u$直接相连的点。如果$a$与$b$都在最小环上,那么一定可以在当前环上找到两个不在最小环上的点,否则当前环上的点都在最小环上那么最小环的长度就比当前环的长度多出$2$就矛盾了。

  因此对于每个度数大于$4$的节点$u$,枚举所有邻边$(u,v)$,然后把$(u,v)$删掉跑一遍bfs,如果存在从$v$到$u$的路径那么就存在一个$u \to \cdots \to v \to u$的最小环,把在这个环上的所有点都标出来。最后再遍历所有含$u$的边,如果存在至少两个没有被标记的相点,则说明$u$是某个鱼图的特殊点,即存在某个子图是鱼图。

  AC代码如下,时间复杂度为$O(m \times (n+m))$:

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 
 4 const int N = 2010, M = N * 2;
 5 
 6 int head[N], e[M], ne[M], idx;
 7 int deg[N];
 8 int q[N], dist[N], p[N];
 9 bool vis[N];
10 
11 void add(int v, int w) {
12     e[idx] = w, ne[idx] = head[v], head[v] = idx++;
13 }
14 
15 void solve() {
16     int n, m;
17     scanf("%d %d", &n, &m);
18     idx = 0;
19     memset(head, -1, sizeof(head));
20     memset(deg, 0, sizeof(deg));
21     while (m--) {
22         int v, w;
23         scanf("%d %d", &v, &w);
24         add(v, w), add(w, v);
25         deg[v]++, deg[w]++;
26     }
27     for (int i = 1; i <= n; i++) {
28         if (deg[i] >= 4) {  // 只有度数至少为4的点才有可能是鱼图的特殊点
29             for (int j = head[i]; j != -1; j = ne[j]) { // 枚举所有的边,删去这条边跑bfs
30                 memset(dist, 0x3f, sizeof(dist));
31                 dist[i] = 0;
32                 int hh = 0, tt = -1;
33                 q[++tt] = i;
34                 memset(p, 0, sizeof(p));
35                 while (hh <= tt) {
36                     int t = q[hh++];
37                     for (int k = head[t]; k != -1; k = ne[k]) {
38                         if (t == i && e[k] == e[j] || t == e[j] && e[k] == i) continue; // 跳过删除的边
39                         if (dist[e[k]] > dist[t] + 1) {
40                             dist[e[k]] = dist[t] + 1;
41                             p[e[k]] = t;    // 记录从哪个点转移过来的
42                             q[++tt] = e[k];
43                         }
44                     }
45                 }
46                 if (dist[e[j]] < 0x3f3f3f3f) {  // 存在从i到e[j]的路径,说明存在环
47                     memset(vis, 0, sizeof(vis));
48                     int t = e[j];
49                     vector<vector<int>> ans({{i, e[j]}});
50                     while (p[t]) {  // 把环上所有点标出来
51                         vis[t] = true;
52                         ans.push_back({t, p[t]});
53                         t = p[t];
54                     }
55                     int cnt = 2;    // 找两个不在环上的点
56                     for (int k = head[i]; k != -1; k = ne[k]) {
57                         if (!vis[e[k]]) {
58                             ans.push_back({i, e[k]});
59                             if (--cnt == 0) break;
60                         }
61                     }
62                     if (!cnt) { // 存在环且存在两个不在环上的点,则存在鱼图
63                         printf("YES\n%d\n", ans.size());
64                         for (auto &p : ans) {
65                             printf("%d %d\n", p[0], p[1]);
66                         }
67                         return;
68                     }
69                 }
70             }
71         }
72     }
73     printf("NO\n");
74 }
75 
76 int main() {
77     int t;
78     scanf("%d", &t);
79     while (t--) {
80         solve();
81     }
82     
83     return 0;
84 }

 

参考资料

  Codeforces Round #869 (Div.1, Div.2) Editorial:https://codeforces.com/blog/entry/115586

标签:环上,subgraph,int,Graph,Fish,contains,鱼图,edges
From: https://www.cnblogs.com/onlyblues/p/17378382.html

相关文章

  • Three.js#04#Responsive Design&Scenegraph
    参考https://threejs.org/manual/#en/responsive和https://threejs.org/manual/#en/scenegraph前者主要是说怎样创建一个响应式的three.js应用,就是在变化屏幕大小的时候,画面不会畸形。后者是再说,怎么组合小的组件变成一个大的组件(依赖于一个空组件object3D)下面是示例代码:index.......
  • Handling Information Loss of Graph Neural Networks for Session-based Recommendat
    目录概符号说明存在的问题LossysessionencodingproblemIneffectivelong-rangedependencycapturingproblemLESSRS2MGS2SG模型EOPA(Edge-OrderPreservingAggregation)SGAT(ShortcutGraphAttention)叠加代码ChenT.andWongR.C.Handlinginformationlossofgrap......
  • 轻松绕过 Graphql 接口爬取有米有数的商品数据
    轻松绕过Graphql接口爬取有米有数的商品数据有米有数数据的API接口,使用的是一种API查询语言graphql。所有的API只有一个入口,具体的操作隐藏在请求数据体里面传输。模拟登录,获取sessionId调用登录接口,进行模拟登录。cookies={}headers={}json_data={'......
  • API 架构风格演化史:CORBA-XMLRPC(SOAP)-REST-JSONRPC-GraphQL-gRPC
    我们先来看一张 TwitterArchitecture2022CodeFirstv.sAPIFirst软件开发理念的改变下图显示了代码优先开发和API优先开发之间的差异。为什么我们要考虑API优先设计?微服务增加了系统的复杂性。我们有单独的服务来服务系统的不同功能。尽管这种体系结构促进了职责的脱钩和分......
  • Codeforces Round 847 (Div. 3) G.Tokens on Graph (构造)
    传送门题目大意  给定一个无向图,我们定义图中有四种点,一种是黑色的点表示终点,并且黑色的点始终是1号点,一种是红色的点,一种是灰色的点,最后一种就是没有颜色的点。  游戏规则:我们必须移动任意一个灰色的点到相邻的点上,如果灰色的点移动到了红色的点上,那么我们可以移动其他灰......
  • spectral-graph-theory-in-GCN
    GCN中的谱图理论笔记Datetime:2023-04-26T09:36+08:00Categories:MachineLearningTags:GNN写毕设,发现自己没法绕过第一代GCN的谱图变换原理我知道啥是傅里叶变化,但是我感觉不到那种新奇,或许这就是无法感觉到数学的美吧。本文默认读者知道傅里叶变换,就数学分析/高等数......
  • nginx-lua-fastdfs-GraphicsMagick整合
      无意发现了一个不错的分布式文件系统。fastdfs开源的分布式文件系统,此脚本利用nginxlua模块,动态生成图片缩略图,fastdfs只存一份原图。lua通过socket获取fastdfs的原图,并存放到本地,根据不同规则url,例如:_60x60.jpg、_80x80.jpg,类似淘宝图片url规则。利用gm命令生成本地缩略图......
  • Python 实时生成曲线的两种方法-Matplotlib/Pyqtgraph
    前言Matplotlib更倾向于制作出版质量的图形,对matlab程序员来说更直观。pyqtgraph不像matplotlib那样完整/成熟,但运行速度要快得多,而且pyqtgraph旨在用于数据采集和分析应用程序,对于python/qt程序员来说更直观。Matplotlib(据我所知)不包括许多pyqtgraph的功能,例如图像......
  • codeforces 505B B. Mr. Kitayuta's Colorful Graph(bfs)
    题目链接:codeforces505B题目大意:给出一个有向图,边有不同的颜色,任意给出查询,查询两点能够只通过一种颜色连通的颜色的种类数。题目分析:根据不同颜色建边,bfs即可,队列维护可用的点。AC代码:#include<iostream>#include<cstring>#include<cstdio>#include<vector>#include<alg......
  • 图(Graph)与图论
    听到图这个字,很多人会联想到图片、折线图、设计图等传统的图,今天要聊的图(Graph)是一种基本研究对象,用于表示实体与实体之间的关系。先说结论:图论:是组合数学分支,是主要研究图的学问,起源于柯尼斯堡七桥问题。图(数学):是用于表示物体与物体之间存在某种关系的结构。数学......