102605: [AtCoder]ABC260 F - Find 4-cycle

Memory Limit:256 MB Time Limit:2 S
Judge Style:Text Compare Creator:
Submit:0 Solved:0

Description

Score : $500$ points

Problem Statement

We have a simple undirected graph $G$ with $(S+T)$ vertices and $M$ edges. The vertices are numbered $1$ through $(S+T)$, and the edges are numbered $1$ through $M$. Edge $i$ connects Vertices $u_i$ and $v_i$.
Here, vertex sets $V_1 = \lbrace 1, 2,\dots, S\rbrace$ and $V_2 = \lbrace S+1, S+2, \dots, S+T \rbrace$ are both independent sets.

A cycle of length $4$ is called a 4-cycle.
If $G$ contains a 4-cycle, choose any of them and print the vertices in the cycle. You may print the vertices in any order.
If $G$ does not contain a 4-cycle, print -1.

What is an independent set? An independent set of a graph $G$ is a set $V'$ of some of the vertices in $G$ such that no two vertices of $V'$ have an edge between them.

Constraints

  • $2 \leq S \leq 3 \times 10^5$
  • $2 \leq T \leq 3000$
  • $4 \leq M \leq \min(S \times T,3 \times 10^5)$
  • $1 \leq u_i \leq S$
  • $S + 1 \leq v_i \leq S + T$
  • If $i \neq j$, then $(u_i, v_i) \neq (u_j, v_j)$.
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

$S$ $T$ $M$
$u_1$ $v_1$
$u_2$ $v_2$
$\vdots$
$u_M$ $v_M$

Output

If $G$ contains a 4-cycle, choose any of them, and print the indices of the four distinct vertices in the cycle. (The order of the vertices does not matter.)
If $G$ does not contain a 4-cycle, print -1.


Sample Input 1

2 3 5
1 3
1 4
1 5
2 4
2 5

Sample Output 1

1 2 4 5

There are edges between Vertices $1$ and $4$, $4$ and $2$, $2$ and $5$, and $5$ and $1$, so Vertices $1$, $2$, $4$, and $5$ form a 4-cycle. Thus, $1$, $2$, $4$, and $5$ should be printed.
The vertices may be printed in any order. Besides the Sample Output, 2 5 1 4 is also considered correct, for example.


Sample Input 2

3 2 4
1 4
1 5
2 5
3 5

Sample Output 2

-1

Some inputs may give $G$ without a 4-cycle.


Sample Input 3

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

Sample Output 3

1 7 2 9

Input

Output

分数:500分

问题描述

我们有一个简单的无向图$G$,包含$(S+T)$个顶点和$M$条边。顶点编号为$1$到$(S+T)$,边编号为$1$到$M$。第$i$条边连接顶点$u_i$和$v_i$。

长度为$4$的循环被称为$4$-循环。

如果$G$包含一个$4$-循环,选择其中一个并打印循环中的顶点。顶点的顺序可以任意。

如果$G$不包含$4$-循环,打印-1

什么是独立集?

图$G$的一个独立集是一个顶点集$V'$,其中$G$中的任意两个顶点之间都没有边。

约束条件

  • $2 \leq S \leq 3 \times 10^5$
  • $2 \leq T \leq 3000$
  • $4 \leq M \leq \min(S \times T,3 \times 10^5)$
  • $1 \leq u_i \leq S$
  • $S + 1 \leq v_i \leq S + T$
  • 如果$i \neq j$,则$(u_i, v_i) \neq (u_j, v_j)$。
  • 输入中的所有值都是整数。

输入

输入从标准输入按以下格式给出:

$S$ $T$ $M$
$u_1$ $v_1$
$u_2$ $v_2$
$\vdots$
$u_M$ $v_M$

输出

如果$G$包含一个$4$-循环,选择其中一个,并打印循环中四个不同顶点的索引。顶点的顺序无关紧要。

如果$G$不包含$4$-循环,打印-1


样例输入1

2 3 5
1 3
1 4
1 5
2 4
2 5

样例输出1

1 2 4 5

顶点$1$和$4$、$4$和$2$、$2$和$5$以及$5$和$1$之间有边,因此顶点$1$、$2$、$4$和$5$形成一个$4$-循环。因此,应该打印$1$、$2$、$4$和$5$。

顶点的顺序可以任意。除了样例输出外,2 5 1 4也被认为是正确的,例如。


样例输入2

3 2 4
1 4
1 5
2 5
3 5

样例输出2

-1

某些输入可能会给出不含$4$-循环的$G$。


样例输入3

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

样例输出3

1 7 2 9

加入题单

算法标签: