1169B - Pairs - CodeForces Solution


graphs implementation *1500

Please click on ads to support us..

Python Code:

import sys 
input = sys.stdin.readline
n, m = [int(i) for i in input().split()] 
p = [] 
l = []
ans = 'YES'
for i in range(m): 
    a, b =[ int(i) for i in input().split()] 
    if i==0 : l.append(a); l.append(b)
    if a not in l and b not in l : 
        if len(l) == 4 : ans = "NO"
        else : l.append(a); l.append(b)
    p.append((a, b)) 
if ans == "NO" : print(ans)
else : 
   
    res = "NO"
    if len(l) == 2 : print("YES") 
    else : 
                for i in range(4): 
            for j in range(i+1, 4): 
                x, y = l[i], l[j] 
                c = "YES"
                for k in range(m): 
                    if p[k][0] not in (x, y) and p[k][1] not in (x, y) : 
                        c = "NO"
                        break 
               
                if c=="YES" : res = "YES"; break 
            if res == "YES": break
            
        print(res)

C++ Code:

#include <iostream>
#include <algorithm>
#include <utility>
#include <cmath>
#include <stack>
#include <cstdlib>
#include <vector>
#include <queue>
#include <set>
#include <map>
using namespace std;
#define ll long long
#define endl "\n"
#define pb(x) push_back(x)
#define all(v) (v).begin(), (v).end()
#define MAX  INT64_MAX
//============================================================================================================//
//-----------------------------------directions array---------------------------------------------------------//
int di[] = {1, -1, 0, 0, 1, -1, 1, -1};
int dj[] = {0, 0, 1, -1, 1, -1, -1, 1};
int dx[] = {-1, 1, 0, 0};
int dy[] = {0, 0, -1, 1};
//============================================================================================================//
//-----------------------------------some constant--------------------------------------------------------------
const int SZ = 2 * (1e5 + 9);
const ll CON = (1e18);
const ll inf = (1 << 30);
const int MOD = (1e9 + 7);
const int M = (3005);
//============================================================================================================//
//-----------------------------------some functions--------------------------------------------------------------

void FAST() {
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); }
void Mostafa()
{
#ifndef ONLINE_JUDGE
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
    freopen("errors.txt","w",stderr);
#endif
}
ll gcd(ll a, ll b)
{ return b == 0 ? a : gcd(b, a % b); }
ll lcm(ll a, ll b)
{ return a / gcd(a, b) * b; }

//============================================================================================================//
//---------------------------------------------------------------------------------------------------------------
bool so(int x,int n, vector<pair<int ,int>>v1,int v)
{
    int arr[v+10]{0};
    int bad =0,ma=0;
    for (int i = 0; i < n; ++i) {
       if  (x != v1[i].first && x != v1[i].second)
       {
            bad++;
            if (v1[i].first != v1[i].second)
            {
                arr[v1[i].first]++;
                arr[v1[i].second]++;
            }
            else arr[v1[i].first]++;
            ma = max ({arr[v1[i].first],ma,arr[v1[i].second]});
       }
    }
   // cout <<bad <<" "<<ma<<endl;
    return (bad==ma);

}

void solve() {
   int n,v;
   cin >>v>>n;
    vector<pair<int ,int>>v1(n);
    for (int i = 0; i < n; ++i) {
        cin >> v1[i].first>>v1[i].second;
    }
    if (so(v1[0].first,n,v1,v) ||so(v1[0].second,n,v1,v) )
        cout <<"YES";
    else cout <<"NO";



}
int main ()
{
    FAST();
    Mostafa();
    int q = 1;
  // cin >>q;
    while(q--)
    {
        solve();
    }
}
 		    	 	  		   		 	 	 	  			


Comments

Submit
0 Comments
More Questions

680B - Bear and Finding Criminals
1036E - Covered Points
1015D - Walking Between Houses
155B - Combination
1531A - Зингер | color
1678A - Tokitsukaze and All Zero Sequence
896A - Nephren gives a riddle
761A - Dasha and Stairs
1728B - Best Permutation
1728A - Colored Balls Revisited
276B - Little Girl and Game
1181A - Chunga-Changa
1728C - Digital Logarithm
1728D - Letter Picking
792B - Counting-out Rhyme
1195A - Drinks Choosing
5D - Follow Traffic Rules
1272A - Three Friends
1632D - New Year Concert
1400D - Zigzags
716C - Plus and Square Root
412A - Poster
844B - Rectangles
1591A - Life of a Flower
1398C - Good Subarrays
629A - Far Relative’s Birthday Cake
1166A - Silent Classroom
1000B - Light It Up
218B - Airport
1463B - Find The Array