QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#334420#3292. Counting CycleschmproRE 0ms0kbPython32.2kb2024-02-21 22:16:062024-02-21 22:16:06

Judging History

This is the latest submission verdict.

  • [2024-02-21 22:16:06]
  • Judged
  • Verdict: RE
  • Time: 0ms
  • Memory: 0kb
  • [2024-02-21 22:16:06]
  • Submitted

answer

import sys
from collections import deque
input=sys.stdin.readline
F=lambda:[*map(int,input().split())]
N,M=F()
G=[[]for _ in range(N)]
for _ in range(M):
  a,b=F()
  G[a-1].append(b-1)
  G[b-1].append(a-1)
if M==N-1:print(0);quit()

Q=deque()
for i in range(N):
  if len(G[i])<=2:Q.append(i)

V=[1]*N
while Q:
  x=Q.popleft()
  if len(G[x])==1:
    V[x]=0
    y=G[x][0]
    G[y].remove(x)
    if len(G[y])<=2:Q.append(y)
  else:
    y,z=G[x]
    if z not in G[y]:
      V[x]=0
      G[y].remove(x);G[z].remove(x)
      G[y].append(z);G[z].append(y)

NN=sum(V)
NG=[[]for _ in range(NN)]
EDGE=[]
LAB=[-1]*N ;now=0
for i in range(N):
  if V[i]==1:
    if LAB[i]==-1:LAB[i]=now;now+=1
    for j in G[i]:
      if j<i:continue
      if LAB[j]==-1:LAB[j]=now;now+=1
      NG[LAB[i]].append((LAB[j],len(EDGE)))
      NG[LAB[j]].append((LAB[i],len(EDGE)))
      EDGE.append((i,j))

def dfs(x):
  for y,idx in NG[x]:
    if y==PRV[x][0]:continue
    if LV[y]>=0:
      if LV[y]>LV[x]:continue
      CYCLE.append({idx})
      if LV[x]>LV[y]:a,b=x,y
      else:a,b=y,x
      while LV[a]>LV[b]:
        a,idx=PRV[a]
        CYCLE[-1].add(idx)
      while a!=b:
        a,idx=PRV[a]
        CYCLE[-1].add(idx)
        b,idx=PRV[b]
        CYCLE[-1].add(idx)
    else:
      LV[y]=LV[x]+1
      PRV[y]=(x,idx)
      dfs(y)

CYCLE=[]
LV=[-1]*N
LV[0]=0
PRV=[(-1,-1)]*N
dfs(0)

#print(NG)
#print(EDGE)
#print(CYCLE)

def root(a):
  L=[]
  while a!=U[a]:
   L.append(a)
   a=U[a]
  for l in L:U[l]=a
  return a
def union(a,b):
  x,y=root(a),root(b)
  U[max(x,y)]=min(x,y)


K=len(CYCLE); E=len(EDGE)
ans=0
for bit in range(1,1<<K):
  if bit.bit_count()==1:ans+=1;continue
  E_CNT=[0]*E
  for i in range(K):
    if bit&(1<<i):
      for e in CYCLE[i]:E_CNT[e]^=1
  
  DEG=[0]*NN
  U=[i for i in range(NN)]
  comp=0;chk=0
  for e in range(E):
    if E_CNT[e]:
      x,y=EDGE[e]
      if DEG[x]==DEG[y]==0:comp+=1
      if DEG[x] and DEG[y] and root(x)!=root(y):comp-=1
      union(x,y)
      DEG[x]+=1;DEG[y]+=1
      if DEG[x]>2 or DEG[y]>2:chk=1
  #print(bit,comp,E_CNT,DEG)
  if chk==0 and comp==1:ans+=1

print(ans)

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Dangerous Syscalls

input:

100000 100015
1 2
1 3
2 4
2 5
3 6
3 7
4 8
4 9
5 10
5 11
6 12
6 13
7 14
7 15
8 16
8 17
9 18
9 19
10 20
10 21
11 22
11 23
12 24
12 25
13 26
13 27
14 28
14 29
15 30
15 31
16 32
16 33
17 34
17 35
18 36
18 37
19 38
19 39
20 40
20 41
21 42
21 43
22 44
22 45
23 46
23 47
24 48
24 49
25 50
25 51
26 52
26 53
...

output:


result: