题目链接:http://codeforces.com/contest/1169/problem/B


题意:给你 m 对数 ,问你能不能在 1 − n 之间找到俩个不相等的 x 和 y 使得 对于前面每一对之中至少有一个数可以等于 x 或 y,找得到输出 YES,否则输出NO。

思路:假设第一对为(a‚b),若x ,y存在,那么他们其中之一就在第一对(a‚b)中,那么如果存在(c,d)使得 c,d 与 a,b 完全不同,那么 x 和 y 另一个数就在(c,d)中,所以 x–y 就是    a–c、 a–d、 b–c、 b–d这四种组合的一种。如果不存在(c,d),那么说明a和b 就能满足,我们只要对所有的 m 对数检查 a–b、a–c、 a–d、 b–c、 b–d这 5 种组合能否满足即可,复杂度Ο(5m)。

AC代码:

 #include<bits/stdc++.h>
using namespace std;
const int maxn = 3e5+;
pair< int ,int > pa[maxn];
int n,m;
bool check(int a,int b)
{
for(int i = ;i < m;i++)
{
if(pa[i].first != a && pa[i].first != b && pa[i].second != a && pa[i].second != b) return false;
}
return true;
}
int main()
{
cin >> n >> m;
int a,b,c = ,d = ;
for(int i = ;i < m;i++)
{
scanf("%d %d",&pa[i].first,&pa[i].second);
if(i == ) a = pa[i].first,b = pa[i].second;
if(a != pa[i].first && a != pa[i].second && b != pa[i].first && b != pa[i].second)
c = pa[i].first,d = pa[i].second;
}
bool flag = false;
if(check(a,b)||check(a,c)||check(a,d)||check(b,c)||check(b,d) ) flag = true;
if(flag) cout << "YES" << endl;
else cout << "NO" << endl;
return ;
}

Codeforces 1169B Pairs的更多相关文章

  1. Educational Codeforces Round 10 C. Foe Pairs 水题

    C. Foe Pairs 题目连接: http://www.codeforces.com/contest/652/problem/C Description You are given a permu ...

  2. Codeforces Round #562 (Div. 2) B. Pairs

    链接:https://codeforces.com/contest/1169/problem/B 题意: Toad Ivan has mm pairs of integers, each intege ...

  3. Codeforces 1404 D. Game of Pairs

    Codeforces 1404 D.Game of Pairs 给定\(2n\)个数\(1,2,...,2n\),A 和 B 将进行交互,规则如下: A 需要将元素分成 n 组 \(\mathbf{p ...

  4. Codeforces 159D Palindrome pairs

    http://codeforces.com/problemset/problem/159/D 题目大意: 给出一个字符串,求取这个字符串中互相不覆盖的两个回文子串的对数. 思路:num[i]代表左端点 ...

  5. codeforces 652C C. Foe Pairs(尺取法+线段树查询一个区间覆盖线段)

    题目链接: C. Foe Pairs time limit per test 1 second memory limit per test 256 megabytes input standard i ...

  6. codeforces#572Div2 E---Count Pairs【数学】【同余】

    题目:http://codeforces.com/contest/1189/problem/E 题意:给定$n$个互不相同数,一个$k$和一个质数$p$.问这$n$个数中有多少对数$(a_i+a_j) ...

  7. CodeForces - 1189E Count Pairs(平方差)

    Count Pairs You are given a prime number pp, nn integers a1,a2,…,ana1,a2,…,an, and an integer kk. Fi ...

  8. CodeForces - 1189 E.Count Pairs (数学)

    You are given a prime number pp, nn integers a1,a2,…,ana1,a2,…,an, and an integer kk. Find the numbe ...

  9. Codeforces 1188B - Count Pairs(思维题)

    Codeforces 题面传送门 & 洛谷题面传送门 虽说是一个 D1B,但还是想了我足足 20min,所以还是写篇题解罢( 首先注意到这个式子里涉及两个参数,如果我们选择固定一个并动态维护另 ...

随机推荐

  1. Java 实例 - 状态监测

    以下实例演示了如何通过继承 Thread 类并使用 currentThread.getName() 方法来监测线程的状态: Main.java 文件 1 2 3 4 5 6 7 8 9 10 11 1 ...

  2. swoole安装异步reids

    /usr/local/php/bin/phpize ./configure --with-php-config=/usr/local/php/bin/php-config --enable-async ...

  3. 挂载时出现mount: RPC: Unable to receive; errno = Connection refused错误的解决方法

    当我们在做NFS开发板下挂载时,经常会出现mount: RPC: Unable to receive; errno = Connection refused的错误,连接被拒绝了,到底是什么原因呢? 这 ...

  4. 【Elasticsearch 7 探索之路】(六)初识 Mapping

    上一篇主要讲解什么是 URL Search 和 Request Body Search 的语法.本篇对 Mapping 的 Dynamic Mapping 以及手动创建 Mapping 进行讲解. 1 ...

  5. 利用HTML和CSS实现常见的布局

    水平居中的页面布局中最为常见的一种布局形式,多出现于标题,以及内容区域的组织形式,下面介绍四种实现水平居中的方法(注:下面各个实例中实现的是child元素的对齐操作,child元素的父容器是paren ...

  6. frp使用(windows+aliyun-windows)

    下载frp:https://github.com/fatedier/frp/releases/ 解压,修改服务端配置文件:frps.ini:如下: [common] # 设置连接端口 bind_por ...

  7. CFgym100020 Problem J. Uprtof

    题意:给你n个点m无向条边.每个点是黑色或者白色的.m条边第一条边边权为2^m,第二条边边权为2^(m-1)....... .在这个图上选择一些边连起来,使得满足:每个黑点连奇数条边,每个白点连偶数条 ...

  8. C# System.Windows.Forms.Panel

    UserControl 定义的界面 输出到panel 实现界面切换

  9. qt大小写字符串比较

    https://blog.csdn.net/GraceLand525/article/details/48625593 Qt::CaseSensitivity cs = Qt::CaseInsensi ...

  10. Eclipse如何设置jsp文件默认UTF-8格式编码

    我使用的是Eclipse Jee Neon, Window->Preferences 具体设置如图所示: