Pairs
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
output
NO題目大意:給m對數,每個數字都在1~n的范圍內,求解是否存在一對數,使得每組輸入的數據中至少有一個在這對數中,如果存在,輸出YES,否則輸出NO。
思路:
既然每組數至少有一個存在與正確答案中,如果最終結果是YES,那么在第一對數里面至少有一個數字在正確答案中。可以遍歷第一對數的兩個數字,然后枚舉給定的m組數,如果某一組數的兩個值都沒有出現,讓這兩個數的標記位++,記錄不滿足條件的個數的變量 sum 也 ++。
如果一共有 t 組不滿足條件,恰好有一個數字不滿足條件的次數為 t ,也就是說可以用 t 和剛剛遍歷的數字來組成一組,一定可以滿足條件。
因為遍歷用的數字不能滿足條件的有 sum 組,而這 sum 組被 t 恰好補充。
總結
- 上一篇: 视频背景不好看?想要给视频里的人物抠出来
- 下一篇: 豆豆游黄山[201602]