Problem:
 Score : 800 points    Problem Statement You are given a simple connected undirected graph consisting of N vertices and M edges.
The vertices are numbered 1 to N , and the edges are numbered 1 to M .  Edge i connects Vertex a_i and b_i bidirectionally.  Determine if three circuits (see Notes) can be formed using each of the edges exactly once.      Notes A circuit is a cycle allowing repetitions of vertices but not edges.      Constraints  All values in input are integers.  1 \leq N,M \leq 10^{5}  1 \leq a_i, b_i \leq N  The given graph is simple and connected.         Input Input is given from Standard Input in the following format:  N  M  a_1  b_1  :  a_M  b_M       Output If three circuits can be formed using each of the edges exactly once, print Yes ; if they cannot, print No .        Sample Input 1 7 9
1 2
1 3
2 3
1 4
1 5
4 5
1 6
1 7
6 7      Sample Output 1 Yes   Three circuits can be formed using each of the edges exactly once, as follows:          Sample Input 2 3 3
1 2
2 3
3 1      Sample Output 2 No   Three circuits are needed.        Sample Input 3 18 27
17 7
12 15
18 17
13 18
13 6
5 7
7 1
14 5
15 11
7 6
1 9
5 4
18 16
4 6
7 2
7 11
6 3
12 14
5 2
10 5
7 8
10 15
3 15
9 8
7 15
5 16
18 15      Sample Output 3 Yes

Solution (Python):
N, M = map(int, raw_input().split())
E = [[] for _ in range(N)]
for _ in range(M):
  a, b = map(int, raw_input().split())
  E[a-1] += [b-1]
  E[b-1] += [a-1]

ok = True
for e in E:
  if len(e)%2!=0:
    ok = False
if not ok:
  print "No"
  exit(0)

ok = False
for e in E:
  if len(e)>=6:
    ok = True
if ok:
  print "Yes"
  exit(0)

c = 0
for e in E:
  if len(e)==4:
    c += 1
if c>=3:
  print "Yes"
  exit(0)
if c<=1:
  print "No"
  exit(0)

for i in range(len(E)):
  if len(E[i])==4:
    c = 0
    for v in E[i]:
      p = i
      while len(E[v])==2:
        if E[v][0]==p:
          p = v
          v = E[v][1]
        else:
          p = v
          v = E[v][0]
      if v==i:
        c += 1
    if c==2:
      print "Yes"
      exit(0)
    else:
      print "No"
      exit(0)

Solution (Python):
n, m = map(int, input().split())

graph = {}
for u in range(1, n + 1):
  graph[u] = set()
for i in range(m):
  u, v = map(int, input().split())
  graph[u].add(v)
  graph[v].add(u)
  

odd = 0
v6 = []
v4 = []
for u in range(1, n + 1): 
  if len(graph[u]) % 2 != 0: 
    odd += 1
    break
  elif len(graph[u]) >= 6:
    v6.append(u)
  elif len(graph[u]) == 4:
    v4.append(u)
if odd: 
  print('No')
else:
  
  
  if len(v6) > 0 or len(v4) > 2:
    print('Yes')
  
  elif len(v4) < 2:
    print('No')
  else:
    s = v4.pop()
    t = v4.pop()
    has_3_cycles= False
    for c in graph[s]:
      visited = {s, t}
      q = [c]
      while q:
        v = q.pop()
        if v in visited:
          continue
        visited.add(v)
        for u in graph[v]:
          if v != c and u == s:
            has_3_cycles = True
            break
          if u not in visited:
            q.append(u)
        if has_3_cycles:
          break
      if has_3_cycles:
        break
    print('Yes') if has_3_cycles else print('No')

Solution (Python):
from sys import setrecursionlimit
setrecursionlimit(10 ** 9)

n, m = [int(i) for i in input().split()]
E = [[] for i in range(n+1)]
used = [False] * (n + 1)
IN = [0] * (n + 1)

for i in range(m):
    a, b = [int(i) for i in input().split()]
    E[a].append(b)
    E[b].append(a)
    IN[a] += 1
    IN[b] += 1

cnt_4 = 0
cnt_6 = 0
v_4 = []
for j, i in enumerate(IN[1:]):
    if i % 2 == 1 or i == 0:
        print('No')
        exit()
    if i >= 4:
        cnt_4 += 1
        v_4.append(j + 1)
    if i >= 6:
        cnt_6 += 1

def dfs(p, v):
    if v == v_4[0]:
        return v_4[0]
    if v == v_4[1]:
        return v_4[1]

    for e in E[v]:
        if e == p:
            continue
        return dfs(v, e)

if cnt_4 > 2 or cnt_6 >= 1:
    print('Yes')
elif cnt_4 == 2:
    if all(dfs(v_4[0], e) == v_4[1] for e in E[v_4[0]]):
        print('No')
    else:
        print('Yes')

else:
    print('No')

Solution (Python):
import sys
sys.setrecursionlimit(10**7)
INF = 10 ** 18
MOD = 10 ** 9 + 7
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x) - 1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def II(): return int(sys.stdin.readline())
def SI(): return input()

from collections import defaultdict

def main():
    N, M = LI()
    edges = defaultdict(list)
    for _ in range(M):
        a, b = LI_()
        edges[a].append(b)
        edges[b].append(a)
    quads = []
    large = 0
    for i, e in edges.items():
        c = len(e)
        if c % 2 == 1:
            return 'No'
        if c >= 6:
            large += 1
        if c == 4:
            quads.append(i)
    if large > 0:
        return 'Yes'
    if len(quads) > 2:
        return 'Yes'
    if len(quads) < 2:
        return 'No'
    for v in edges[quads[0]]:
        visited = [0] * N
        visited[quads[0]] = 1
        while v != quads[1]:
            visited[v] = 1
            for to in edges[v]:
                if visited[to] == 0:
                    v = to
                    break
            if visited[v] == 1:
                break
        if v != quads[1]:
            return 'Yes'

    return 'No'

print(main())

Solution (Python):
import sys
sys.setrecursionlimit(10**7)
INF = 10 ** 18
MOD = 10 ** 9 + 7
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x) - 1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def II(): return int(sys.stdin.readline())
def SI(): return input()

from collections import defaultdict, deque

def main():
    N, M = LI()
    edges = defaultdict(set)
    for _ in range(M):
        a, b = LI_()
        edges[a].add(b)
        edges[b].add(a)
    quads = set()
    sixes = 0
    for i, e in edges.items():
        c = len(e)
        if c % 2 == 1:
            return False
        if c >= 6:
            sixes += 1
        if c == 4:
            quads.add(i)
    if sixes > 0:
        return True
    if len(quads) > 2:
        return True
    if len(quads) < 2:
        return False
    s = quads.pop()
    t = quads.pop()
    for v0 in edges[s]:
        q = deque([v0])
        visited = {s, t}
        while len(q) > 0:
            v = q.pop()
            for to in edges[v]:
                if v != v0 and to == s:
                    return True
                if to not in visited:
                    q.append(to)
                    visited.add(to)

    return False

print('Yes' if main() else 'No')

Solution (Python):
N, M = map(int, input().split())
G = [[] for i in range(N)]
for i in range(M):
  a, b = map(int, input().split())
  G[a - 1].append(b - 1)
  G[b - 1].append(a - 1)
V = []
D = 0
for i in range(N):
  if len(G[i]) % 2:
    print('No')
    break
  D = max(len(G[i]), D)
  if len(G[i]) == 4:
    V.append(i)
else:
  if D >= 6:
    print('Yes')
  elif len(V) != 2:
    print('Yes' if len(V) > 2 else 'No')
  else:
    s = V[0]
    for i in range(3):
      t = G[s].pop()
      G[t].pop(G[t].index(s))
      while t not in V:
        d = G[t].pop()
        G[d].pop(G[d].index(t))
        t = d
      if s == t:
        print('Yes')
        break
      else:
        s = t
    else:
      print('No')

Solution (Python):
from collections import deque
import sys
readline = sys.stdin.readline

N, M = map(int, readline().split())
G = [[] for i in range(N)]
for i in range(M):
    a, b = map(int, readline().split())
    a -= 1; b -= 1
    G[a].append(b)
    G[b].append(a)

def check(s, t):
    u = [0]*N
    u[s] = u[t] = 1
    que = deque()
    for v in G[s]:
        if v == t:
            continue
        u[v] = 1
        que.append(v)
        ok = 0
        while que:
            x = que.popleft()
            for y in G[x]:
                if y == s and x != v:
                    return 1
                if not u[y]:
                    u[y] = 1
                    que.append(y)
    return 0
C = [0]*4
ok = 1
for i in range(N):
    if len(G[i]) % 2 == 1:
        ok = 0
    C[min(3, len(G[i])//2)] += 1
if C[1] == N or (C[1] == N-1 and C[2] == 1):
    ok = 0
elif C[1] == N-2 and C[2] == 2:
    VV = []
    for i in range(N):
        if len(G[i]) == 4:
            VV.append(i)
    if not check(*VV):
        ok = 0
print("Yes" if ok else "No")

Solution (Python):
import sys
sys.setrecursionlimit(100000)

N, M = map(int, input().split())

count = [0] * (N)
E = [[] for _ in range(N)]

for _ in range(M):
    a, b = map(int, input().split())
    E[a - 1].append(b - 1)
    E[b - 1].append(a - 1)
    count[a - 1] += 1
    count[b - 1] += 1

count_sum = 0
count4 = 0
count6 = 0
node = 0

check = [False] * N

for i, temp in enumerate(count):
    if temp % 2 == 1:
        print("No")
        exit()
    else:
        if temp >= 4:
            count4 += 1
            check[i] = True
            node = i
        if temp >= 6:
            count6 += 1
        count_sum += temp

def dfs(v, first, depth):
    global E, check

    ret = 0
    for next_v in E[v]:
        if depth >= 2 and next_v == first:
            return 1
        elif not check[next_v]:
            check[next_v] = True
            ret += dfs(next_v, first, depth + 1)

    return ret

if count4 >= 3 or count6 >= 1:
    print("Yes")
elif count4 == 2:
    ret = dfs(node, node, 0)
    if ret > 0:
        print("Yes")
    else:
        print("No")
else:
    print("No")

Solution (Python):


def solve():
    N, M = map(int, input().split())
    F = [list() for _ in range(N+1)]
    D = [int() for _ in range(N+1)]
    for _ in range(M):
        a, b = map(int, input().split())
        D[a] += 1
        D[b] += 1
        F[a].append(b)
        F[b].append(a)

    E = [0 for _ in range(7)]
    X = list()
    for a,d in enumerate(D[1:], start=1):
        if d%2==0:
            if d >= 6:
                E[6] += 1
            elif d == 4:
                E[4] += 1
                X.append(a)
            else:
                E[d] += 1
        else:
            return 'No'
            E[1] += 1

    if E[6]>0 or E[4]>2:
        return 'Yes'

    elif E[4]<2:
        return 'No'

    else:
        x, y = X
        q = set((y,))
        R = set((x,))
        while q:
            z = q.pop()
            R.add(z)
            q |= set(F[z])-R
        if set(F[x])&R == set(F[x]):
            return 'No'
        else:
            return 'Yes'

if __name__ == '__main__':
    print(solve())

Solution (Python):
import sys


def solve(n, m, links):
    v4 = set()
    v6 = 0
    for v, link in enumerate(links):
        l = len(link)
        if l % 2 == 1:
            return False
        if l == 4:
            v4.add(v)
        elif l >= 6:
            v6 += 1
    if v6 > 0:
        return True
    if len(v4) > 2:
        return True
    if len(v4) < 2:
        return False
    s = v4.pop()
    t = v4.pop()
    for c in links[s]:
        visited = {s, t}
        q = [c]
        while q:
            v = q.pop()
            if v in visited:
                continue
            visited.add(v)
            for u in links[v]:
                if v != c and u == s:
                    return True
                if u not in visited:
                    q.append(u)
    return False


n, m = map(int, input().split())
links = [set() for _ in [0] * n]
for line in sys.stdin:
    a, b = map(int, line.split())
    a -= 1
    b -= 1
    links[a].add(b)
    links[b].add(a)
print('Yes' if solve(n, m, links) else 'No')

Solution (Python):
from collections import deque
import sys
readline = sys.stdin.readline

N, M = map(int, readline().split())
G = [[] for i in range(N)]
for i in range(M):
    a, b = map(int, readline().split())
    a -= 1; b -= 1
    G[a].append(b)
    G[b].append(a)
def check(s, t):
    u = [0]*N
    u[s] = u[t] = 1
    for v in G[s]:
        if v == t:
            continue
        u[v] = 1
        que = deque([v])
        ok = 0
        while que:
            x = que.popleft()
            for y in G[x]:
                if y == s and x != v:
                    return 1
                if u[y]:
                    continue
                u[y] = 1
                que.append(y)
    return 0
C = [0]*4
ok = 1
for i in range(N):
    if len(G[i]) % 2 == 1:
        ok = 0
    C[min(3, len(G[i])//2)] += 1
if C[1] == N or (C[1] == N-1 and C[2] == 1):
    ok = 0
elif C[1] == N-2 and C[2] == 2:
    VV = []
    for i in range(N):
        if len(G[i]) == 4:
            VV.append(i)
    if not check(*VV):
        ok = 0
print("Yes" if ok else "No")