日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Pairs

發(fā)布時間:2023/12/14 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Pairs 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

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

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

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

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

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

Examples
input

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

output

NO

題目大意:給m對數(shù),每個數(shù)字都在1~n的范圍內(nèi),求解是否存在一對數(shù),使得每組輸入的數(shù)據(jù)中至少有一個在這對數(shù)中,如果存在,輸出YES,否則輸出NO。

思路:
既然每組數(shù)至少有一個存在與正確答案中,如果最終結(jié)果是YES,那么在第一對數(shù)里面至少有一個數(shù)字在正確答案中。可以遍歷第一對數(shù)的兩個數(shù)字,然后枚舉給定的m組數(shù),如果某一組數(shù)的兩個值都沒有出現(xiàn),讓這兩個數(shù)的標記位++,記錄不滿足條件的個數(shù)的變量 sum 也 ++。
如果一共有 t 組不滿足條件,恰好有一個數(shù)字不滿足條件的次數(shù)為 t ,也就是說可以用 t 和剛剛遍歷的數(shù)字來組成一組,一定可以滿足條件。
因為遍歷用的數(shù)字不能滿足條件的有 sum 組,而這 sum 組被 t 恰好補充。

#include<bits/stdc++.h> using namespace std; struct p {int a, b;p() {}p(int a, int b) {this->a = a;this->b = b;} }; int n, m, a, b; int main() {ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);cin >> n >> m;int v[n + 10];vector<p> arr;for (int i = 0; i < m; i++) {cin >> a >> b;arr.emplace_back(p(a, b));}vector<int> d = { arr[0].a,arr[0].b };for (int i : d) {memset(v, 0, sizeof v);int sum = 0;for (auto j : arr) {if (j.a != i && j.b != i) {sum++;v[j.a]++;v[j.b]++;}}int ma = *max_element(v + 1, v + n + 1);if (ma == sum) {cout << "YES" << endl;return 0;}}cout << "NO" << endl;return 0; }

總結(jié)

以上是生活随笔為你收集整理的Pairs的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。