結果

問題 No.1900 Don't be Powers of 2
ユーザー とりゐとりゐ
提出日時 2022-04-09 00:37:23
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,269 ms / 2,000 ms
コード長 2,986 bytes
コンパイル時間 188 ms
コンパイル使用メモリ 82,492 KB
実行使用メモリ 290,308 KB
最終ジャッジ日時 2024-11-28 16:07:56
合計ジャッジ時間 13,779 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 37 ms
54,436 KB
testcase_01 AC 38 ms
54,968 KB
testcase_02 AC 40 ms
59,720 KB
testcase_03 AC 283 ms
77,616 KB
testcase_04 AC 274 ms
77,668 KB
testcase_05 AC 274 ms
77,772 KB
testcase_06 AC 298 ms
77,672 KB
testcase_07 AC 281 ms
77,880 KB
testcase_08 AC 153 ms
79,604 KB
testcase_09 AC 110 ms
77,648 KB
testcase_10 AC 75 ms
77,072 KB
testcase_11 AC 93 ms
77,544 KB
testcase_12 AC 187 ms
80,320 KB
testcase_13 AC 55 ms
76,300 KB
testcase_14 AC 206 ms
77,408 KB
testcase_15 AC 55 ms
76,456 KB
testcase_16 AC 53 ms
76,248 KB
testcase_17 AC 260 ms
77,776 KB
testcase_18 AC 220 ms
77,404 KB
testcase_19 AC 273 ms
78,028 KB
testcase_20 AC 213 ms
77,228 KB
testcase_21 AC 201 ms
77,296 KB
testcase_22 AC 194 ms
77,536 KB
testcase_23 AC 62 ms
69,968 KB
testcase_24 AC 149 ms
77,440 KB
testcase_25 AC 171 ms
77,552 KB
testcase_26 AC 1,222 ms
289,244 KB
testcase_27 AC 346 ms
106,752 KB
testcase_28 AC 218 ms
81,592 KB
testcase_29 AC 213 ms
81,372 KB
testcase_30 AC 76 ms
77,344 KB
testcase_31 AC 37 ms
55,496 KB
testcase_32 AC 37 ms
55,980 KB
testcase_33 AC 1,127 ms
285,792 KB
testcase_34 AC 1,143 ms
285,484 KB
testcase_35 AC 1,269 ms
290,308 KB
testcase_36 AC 1,029 ms
264,060 KB
testcase_37 AC 252 ms
80,444 KB
testcase_38 AC 287 ms
129,916 KB
testcase_39 AC 183 ms
98,880 KB
testcase_40 AC 318 ms
79,036 KB
testcase_41 AC 324 ms
81,232 KB
testcase_42 AC 37 ms
54,168 KB
testcase_43 AC 35 ms
55,780 KB
testcase_44 AC 36 ms
55,648 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import deque
 
class MaxFlow:
    inf = 10**18
 
    class E:
        def __init__(self,to,cap):
            self.to = to
            self.cap = cap
            self.rev = None
 
    def __init__(self,n):
        self.n = n
        self.graph = [[] for _ in range(n)]
 
    def add_edge(self, fr, to, cap):
        graph = self.graph
        edge = self.E(to,cap)
        edge2 = self.E(fr,0)
        edge.rev = edge2
        edge2.rev = edge
        graph[fr].append(edge)
        graph[to].append(edge2)
 
    def bfs(self, s, t):
        level = self.level = [self.n]*self.n
        q = deque([s])
        level[s] = 0
        while q:
            now = q.popleft()
            lw = level[now]+1
            for e in self.graph[now]:
                if e.cap and level[e.to]> lw:
                    level[e.to] = lw
                    if e.to == t:
                        return True
                    q.append(e.to)
        return False
 
    def dfs(self, s, t, up):
        graph = self.graph
        it = self.it
        level = self.level
 
        st = deque([t])
        while st:
            v = st[-1]
            if v == s:
                st.pop()
                flow = up
                for w in st:
                    e = graph[w][it[w]].rev
                    flow = min(flow, e.cap)
                for w in st:
                    e = graph[w][it[w]]
                    e.cap += flow
                    e.rev.cap -= flow
                return flow
            lv = level[v]-1
            while it[v] < len(graph[v]):
                e = graph[v][it[v]]
                re = e.rev
                if re.cap == 0 or lv != level[e.to]:
                    it[v] += 1
                    continue
                st.append(e.to)
                break
            if it[v] == len(graph[v]):
                st.pop()
                level[v] = self.n
 
        return 0
 
    def flow(self,s,t,flow_limit=inf):
        flow = 0
        while flow < flow_limit and self.bfs(s,t):
            self.it = [0]*self.n
            while flow < flow_limit:
                f = self.dfs(s,t,flow_limit-flow)
                if f == 0:
                    break
                flow += f
        return flow
 
    def min_cut(self,s):
        visited = [0]*self.n
        q = deque([s])
        while q:
            v = q.pop()
            visited[v] = 1
            for e in self.graph[v]:
                if e.cap and not visited[e.to]:
                    q.append(e.to)
        return visited

def popcount(m):
  k=m
  cnt=0
  while k:
    cnt+=k&1
    k>>=1
  return cnt
  
n=int(input())
a=list(map(int,input().split()))

s,t=n,n+1
g=MaxFlow(n+2)
inf=n+10

for i in range(n):
  if popcount(a[i])%2==0:
    g.add_edge(s,i,1)
  else:
    g.add_edge(i,t,1)

for i in range(n):
  for j in range(i+1,n):
    if popcount(a[i]^a[j])==1:
      if popcount(a[i])%2==0:
        g.add_edge(i,j,inf)
      else:
        g.add_edge(j,i,inf)
print(n-g.flow(s,t))
0