Codeforces 1169B Pairs
题目链接: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的更多相关文章
- Educational Codeforces Round 10 C. Foe Pairs 水题
C. Foe Pairs 题目连接: http://www.codeforces.com/contest/652/problem/C Description You are given a permu ...
- Codeforces Round #562 (Div. 2) B. Pairs
链接:https://codeforces.com/contest/1169/problem/B 题意: Toad Ivan has mm pairs of integers, each intege ...
- Codeforces 1404 D. Game of Pairs
Codeforces 1404 D.Game of Pairs 给定\(2n\)个数\(1,2,...,2n\),A 和 B 将进行交互,规则如下: A 需要将元素分成 n 组 \(\mathbf{p ...
- Codeforces 159D Palindrome pairs
http://codeforces.com/problemset/problem/159/D 题目大意: 给出一个字符串,求取这个字符串中互相不覆盖的两个回文子串的对数. 思路:num[i]代表左端点 ...
- codeforces 652C C. Foe Pairs(尺取法+线段树查询一个区间覆盖线段)
题目链接: C. Foe Pairs time limit per test 1 second memory limit per test 256 megabytes input standard i ...
- codeforces#572Div2 E---Count Pairs【数学】【同余】
题目:http://codeforces.com/contest/1189/problem/E 题意:给定$n$个互不相同数,一个$k$和一个质数$p$.问这$n$个数中有多少对数$(a_i+a_j) ...
- 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 ...
- 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 ...
- Codeforces 1188B - Count Pairs(思维题)
Codeforces 题面传送门 & 洛谷题面传送门 虽说是一个 D1B,但还是想了我足足 20min,所以还是写篇题解罢( 首先注意到这个式子里涉及两个参数,如果我们选择固定一个并动态维护另 ...
随机推荐
- Python 定时任务框架 APScheduler 详解
APScheduler 最近想写个任务调度程序,于是研究了下 Python 中的任务调度工具,比较有名的是:Celery,RQ,APScheduler. Celery:非常强大的分布式任务调度框架 R ...
- 数据访问层的基类BaseDALSQL
using System; using System.Text; using System.Collections; using System.Data; using System.Data.Comm ...
- 关于audio不能拖放
图一,图二均为wav格式文件 图一为播放本地的音频,可以拖放 图二为放在后台的音频,不可以拖放 把这两个图片发给后台,让后台分析下两个的headers不同之处
- 201⑨湘潭邀请赛 Chika and Friendly Pairs(HDU6534)
原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=6534 题意: 给你一个数组,对于第i个数来说,如果存在一个位置j,使得j>i并且a[j]-k&l ...
- prim算法【最小生成树1】
适用范围:要求无向图 prim算法(读者可以将其读作“普里姆算法”)用来解决最小生成树问题, 其基本思想是: ·对图G(VE)设置集合S,存放已被访问的顶点, ·然后每次从集合V-S中选择与集合S的最 ...
- UVA10118_Free Candies状态压缩
这题大概题意是,有四列糖果,一个人手中最多拿五个水果,每次拿水果只能从每一列最上面开始拿. 而如果手中的糖果相同就会成对抵消,奖励给玩家 问玩家怎样取能取到最多的糖果,并输出对数 这题是运用动态规划, ...
- span 设置inline-block 写文字的span错位
写一个如下图这样排版 设置几个span为inline-block 中间的span写了文字的span错位了 解决方案 给span添加 vertical-align: top
- hduoj-1301 Jungle Roads(最小生成树-克鲁斯卡尔和普里姆求解)
普里姆求解: #include<cstdio> #include<cmath> #include<cstring> #include<iostream> ...
- @Validated和@Valid区别:Spring validation验证框架对入参实体进行嵌套验证必须在相应属性(字段)加上@Valid而不是@Validated
Spring Validation验证框架对参数的验证机制提供了@Validated(Spring's JSR-303规范,是标准JSR-303的一个变种),javax提供了@Valid(标准JSR- ...
- Andoid之硬件访问服务1(让Andoid应用程序访问c库)
andoid 项目文件结构图 新建Hardcontrol.java package com.thisway.hardlibrary; public class HardControl { public ...