A16057 | Good Graph
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
You are given a bipartite graph $G$ with the vertex set in the left part $L$ , in the right part $R$ , and $m$ edges connecting these two sets. We know that $|L| = |R| = n$ .
For any subset $S \subseteq L$ , let $N(S)$ denote the set of all neighbors of vertices in $S$ . We say that a subset $S \subseteq L$ in graph $G$ is tight if $|S| = |N(S)|$ . We say that graph $G$ is good if $\forall_{S \subseteq L}, |S| \leq |N(S)|$ .
Your task is to verify whether the graph is good and, if so, to optimize it. If the graph is not good, find a subset $S \subseteq L$ such that $|S| > |N(S)|$ . However, if the graph is good, your task is to find a good bipartite graph $G'$ with the same set of vertices $L \cup R$ , in which $\forall_{S \subseteq L}$ , $S$ is tight in $G$ if and only if $S$ is tight in $G'$ . If there are multiple such graphs, choose one with the smallest possible number of edges. If there are still multiple such graphs, print any.
For any subset $S \subseteq L$ , let $N(S)$ denote the set of all neighbors of vertices in $S$ . We say that a subset $S \subseteq L$ in graph $G$ is tight if $|S| = |N(S)|$ . We say that graph $G$ is good if $\forall_{S \subseteq L}, |S| \leq |N(S)|$ .
Your task is to verify whether the graph is good and, if so, to optimize it. If the graph is not good, find a subset $S \subseteq L$ such that $|S| > |N(S)|$ . However, if the graph is good, your task is to find a good bipartite graph $G'$ with the same set of vertices $L \cup R$ , in which $\forall_{S \subseteq L}$ , $S$ is tight in $G$ if and only if $S$ is tight in $G'$ . If there are multiple such graphs, choose one with the smallest possible number of edges. If there are still multiple such graphs, print any.
输入格式
The first line of the input contains two integers $n$ and $m$ ( $1 \leq n \leq 10^3$ , $0 \leq m \leq n^2$ ), separated by a single space. The number $n$ denotes the number of vertices in each of the sets $L$ and $R$ , and the number $m$ denotes the number of edges between them.
The following $m$ lines describe the edges. Each of them contains two integers $l$ and $r$ ( $1 \leq l \leq n$ , $n+1 \leq r \leq 2 \cdot n$ ), separated by a single space, indicating that there is an edge from vertex $l \in L$ to vertex $r \in R$ .
The following $m$ lines describe the edges. Each of them contains two integers $l$ and $r$ ( $1 \leq l \leq n$ , $n+1 \leq r \leq 2 \cdot n$ ), separated by a single space, indicating that there is an edge from vertex $l \in L$ to vertex $r \in R$ .
输出格式
If the graph $G$ given in the input is not good, output one word "NO" in the first line of the output. In the second line of the output, output the number $k$ , and in the third line, output $k$ numbers $l_1, l_2, \dots, l_k$ , separated by single spaces. These numbers should indicate that for the set $S = \{l_1, l_2, \dots, l_k\}$ , $|S| > |N(S)|$ .
However, if the graph $G$ given in the input is good, output one word "YES" in the first line of the output. In the second line of the output, output the number $m'$ , indicating the number of edges in the new, also good graph $G'$ . Then, in the following $m'$ lines, output the edges of the graph $G'$ in the same format as given in the input.
However, if the graph $G$ given in the input is good, output one word "YES" in the first line of the output. In the second line of the output, output the number $m'$ , indicating the number of edges in the new, also good graph $G'$ . Then, in the following $m'$ lines, output the edges of the graph $G'$ in the same format as given in the input.
输入输出样例
输入 #1
3 8 1 4 1 5 1 6 2 4 2 5 2 6 3 5 3 6
输出 #1
YES 6 1 4 1 5 2 5 2 6 3 6 3 4
输入 #2
3 4 1 4 1 5 2 6 3 6
输出 #2
NO 2 2 3
In the first sample test, the graph $G$ is good; thus, we are looking for an equivalent graph with the same tight sets. The only tight set is $\{ 1, 2, 3 \}$ , which remains tight in the resulting graph. Moreover, no other set is tight in the resulting graph. One can prove that no graph with less than $6$ edges and the same tight sets exists.
In the second sample test, the graph $G$ is not good. Set $\{ 2, 3 \}$ has only one neighbour — vertex $6$ . Thus, $|\{ 2, 3 \}| > |\{ 6 \}|$ , which is a prove that the input graph is not good.
In the second sample test, the graph $G$ is not good. Set $\{ 2, 3 \}$ has only one neighbour — vertex $6$ . Thus, $|\{ 2, 3 \}| > |\{ 6 \}|$ , which is a prove that the input graph is not good.
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted