B. Pairs

该问题要求判断是否存在两个整数x和y(1≤x<y≤n),使得每对给定的整数中至少有一个等于x或y。输入包含n和m,表示数值范围和对数,然后给出m对不同的整数。输出'YES'如果存在这样的x和y,否则输出'NO'。示例展示了不同情况下的判断结果。

链接:https://codeforces.com/problemset/problem/1169/B

Toad Ivan has mm pairs of integers, each integer is between 11 and nn, inclusive. The pairs are (a1,b1),(a2,b2),…,(am,bm)(a1,b1),(a2,b2),…,(am,bm).

He asks you to check if there exist two integers xx and yy (1≤x<y≤n1≤x<y≤n) such that in each given pair at least one integer is equal to xx or yy.

Input

The first line contains two space-separated integers nn and mm (2≤n≤3000002≤n≤300000, 1≤m≤3000001≤m≤300000) — the upper bound on the values of integers in the pairs, and the number of given pairs.

The next mm lines contain two integers each, the ii-th of them contains two space-separated integers aiai and bibi (1≤ai,bi≤n,ai≠bi1≤ai,bi≤n,ai≠bi) — the integers in the ii-th pair.

Output

Output "YES" if there exist two integers xx and yy (1≤x<y≤n1≤x<y≤n) such that in each given pair at least one integer is equal to xx or yy. Otherwise, print "NO". You can print each letter in any case (upper or lower).

Examples

input

Copy

4 6
1 2
1 3
1 4
2 3
2 4
3 4

output

Copy

NO

input

Copy

5 4
1 2
2 3
3 4
4 5

output

Copy

YES

input

Copy

300000 5
1 2
1 2
1 2
1 2
1 2

output

Copy

YES

Note

In the first example, you can't choose any xx, yy because for each such pair you can find a given pair where both numbers are different from chosen integers.

In the second example, you can choose x=2x=2 and y=4y=4.

In the third example, you can choose x=1x=1 and y=2y=2.

代码:

#include <bits/stdc++.h>
using namespace std;
long long t,n,m,k,s=0,x,y,p,q,max1=0,c,d;
long long a[1000001],b[1000001],vis[1000001]={0};
int main()
{
	cin>>n>>m;
	for(int i=1;i<=m;i++)
	{
		cin>>a[i]>>b[i];
	}
	x=a[1],y=b[1];
	int fl=0;
	for(int i=2;i<=m;i++)
	{
		if(a[i]!=x&&b[i]!=y&&a[i]!=y&&b[i]!=x)
		{
			p=a[i];
			q=b[i];
			fl=1;
			break;
		}
	}
	if(fl==1)
	c=x,d=p;
	else
	{
		cout<<"YES";
		return 0;
	}
	
	
	int flag=0;
	for(int i=1;i<=m;i++)
	{
		if(a[i]==c||b[i]==c||a[i]==d||b[i]==d)
		{
			continue;
		}
		else
		{
			flag=1;
			break;
		}
	}
	if(flag==0)
	cout<<"YES";
	else
	{
		c=x,d=q;
		flag=0;
		for(int i=1;i<=m;i++)
		{
			if(a[i]==c||b[i]==c||a[i]==d||b[i]==d)
			{
				continue;
			}
			else
			{
				flag=1;
				break;
			}
		}
		if(flag==0)
		cout<<"YES";
		else
		{
			c=y,d=q;
			flag=0;
			for(int i=1;i<=m;i++)
			{
				if(a[i]==c||b[i]==c||a[i]==d||b[i]==d)
				{
					continue;
				}
				else
				{
					flag=1;
					break;
				}
			}
			if(flag==0)
			cout<<"YES";
			else
			{
				c=y,d=p;
				flag=0;
				for(int i=1;i<=m;i++)
				{
					if(a[i]==c||b[i]==c||a[i]==d||b[i]==d)
					{
						continue;
					}
					else
					{
						flag=1;
						break;
					}
				}
				if(flag==0)
				cout<<"YES";
				else
				cout<<"NO";
			}
		}
	}
}

 

[1] pyerr_check(msg::String, val::Nothing) @ PyCall C:\Users\Public\TongYuan\.julia\packages\PyCall\9ZDyP\src\exception.jl:75 [2] pyerr_check @ C:\Users\Public\TongYuan\.julia\packages\PyCall\9ZDyP\src\exception.jl:79 [inlined] [3] _handle_error(msg::String) @ PyCall C:\Users\Public\TongYuan\.julia\packages\PyCall\9ZDyP\src\exception.jl:96 [4] macro expansion @ C:\Users\Public\TongYuan\.julia\packages\PyCall\9ZDyP\src\exception.jl:110 [inlined] [5] #119 @ C:\Users\Public\TongYuan\.julia\packages\PyCall\9ZDyP\src\pyfncall.jl:43 [inlined] [6] disable_sigint(f::PyCall.var"#119#120"{PyCall.PyObject, Ptr{PyCall.PyObject_struct}, PyCall.PyObject, Ptr{Nothing}}) @ Base .\c.jl:473 [7] __pycall! @ C:\Users\Public\TongYuan\.julia\packages\PyCall\9ZDyP\src\pyfncall.jl:42 [inlined] [8] _pycall!(ret::PyCall.PyObject, o::PyCall.PyObject, args::Tuple{PyCall.PyObject, Vector{Int64}, Vector{Int64}, Dict{Symbol, Any}}, nargs::Int64, kw::Ptr{Nothing}) @ PyCall C:\Users\Public\TongYuan\.julia\packages\PyCall\9ZDyP\src\pyfncall.jl:29 [9] _pycall!(ret::PyCall.PyObject, o::PyCall.PyObject, args::Tuple{PyCall.PyObject, Vector{Int64}, Vector{Int64}, Dict{Symbol, Any}}, kwargs::Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}}) @ PyCall C:\Users\Public\TongYuan\.julia\packages\PyCall\9ZDyP\src\pyfncall.jl:11 [10] (::PyCall.PyObject)(::PyCall.PyObject, ::Vararg{Any}; kwargs::Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}}) @ PyCall C:\Users\Public\TongYuan\.julia\packages\PyCall\9ZDyP\src\pyfncall.jl:86 [11] (::PyCall.PyObject)(::PyCall.PyObject, ::Vararg{Any}) @ PyCall C:\Users\Public\TongYuan\.julia\packages\PyCall\9ZDyP\src\pyfncall.jl:86 [12] _ty_bar(ax::Any, x::Any, height::Any; width::Any, style::Any, text::Any, labels::Any, basevalue::Any, basestyle::Any, basecolor::Any, basewidth::Any, kwargs::Base.Pairs{Symbol, V, Tuple{Vararg{Symbol, N}}, NamedTuple{names, T}} where {V, N, names, T<:Tuple{Vararg{Any, N}}}) @ TyPlot C:\Users\Public\TongYuan\.julia\packages\TyPlot\AZhUQ\src\Bar Plots\ty_bar.jl:206 [13] _ty_bar_ax_x(ax::Any, x::Any, height::Any; kwargs::Base.Pairs{Symbol, V, Tuple{Vararg{Symbol, N}}, NamedTuple{names, T}} where {V, N, names, T<:Tuple{Vararg{Any, N}}}) @ TyPlot C:\Users\Public\TongYuan\.julia\packages\TyPlot\AZhUQ\src\Bar Plots\ty_bar.jl:126 [14] _ty_bar_x(x::Any, height::Any; kwargs::Base.Pairs{Symbol, V, Tuple{Vararg{Symbol, N}}, NamedTuple{names, T}} where {V, N, names, T<:Tuple{Vararg{Any, N}}}) @ TyPlot C:\Users\Public\TongYuan\.julia\packages\TyPlot\AZhUQ\src\Bar Plots\ty_bar.jl:159 [15] ty_bar(x::Any, height::Any; kwargs::Base.Pairs{Symbol, V, Tuple{Vararg{Symbol, N}}, NamedTuple{names, T}} where {V, N, names, T<:Tuple{Vararg{Any, N}}}) @ TyPlot C:\Users\Public\TongYuan\.julia\packages\TyPlot\AZhUQ\src\Bar Plots\ty_bar.jl:48 [16] top-level scope @ Untitled-1.jl:54
最新发布
10-28
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值