题目链接: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. PAT_A1104#Sum of Number Segments

    Source: PAT A1104 Sum of Number Segments (20 分) Description: Given a sequence of positive numbers, a ...

  2. fedora 26

    图标文件路径: /home/xiezhiyan/.local/share/applications

  3. 微信小程序のwxss选择器

    一.什么是选择器 选择器就是选择标签所用样式的模式,即:以什么方式设置样式. 二.微信小程序的样式选择器 .calss就是选择器的一种 三.选择器的优先级 element表示样式元素:.element ...

  4. Gym 102028J 扫描线/二维差分 + 解方程

    题意:有一个二维平面,以及n个操作,每个操作会选择一个矩形,使得这个二维平面的一部分被覆盖.现在你可以取消其中的2个操作,问最少有多少块地方会被覆盖? 思路:官方题解简洁明了,就不细说了:https: ...

  5. 【Luogu】【关卡1-8】BOSS战-入门综合练习2(2017年10月)【AK】------都是基础题

    P1426 小鱼会有危险吗 我个人觉得这个题目出的不好,没说明白,就先只粘贴的AC代码吧 #include <bits/stdc++.h> using namespace std; int ...

  6. java笔试手写算法面试题大全含答案

    1.统计一篇英文文章单词个数.public class WordCounting {public static void main(String[] args) {try(FileReader fr ...

  7. CSIC_716_20191216【pymysql模块】

    强调:mysql要设置严格模式,在my.ini 配置文件中 sql-mode="strict_trans_tables,only_full_group_by"    ,设置完要重启 ...

  8. vue 条件渲染v-if v-show

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  9. Ubuntu 图形桌面死机重启(机器不重启)

    Ubuntu的图形界面容易死机,如果正在跑程序的话又不能重启.这时候可以通过终端来_重启_图形界面. 首先按Alt+Ctrl+F1进入终端界面.查看图形界面的进程: ps -t tty7 查看到名为X ...

  10. Delphi QueryPerformanceCounter、QueryPerformanceFrequency函数,精确定时到ns

    var t1,t2:int64; r1,r2,r3:double; begin QueryPerformanceFrequency(c1);//WINDOWS API 返回计数频率 (Intel86: ...