結果

問題 No.1900 Don't be Powers of 2
ユーザー とりゐとりゐ
提出日時 2022-04-08 23:08:14
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,800 ms / 2,000 ms
コード長 2,955 bytes
コンパイル時間 303 ms
コンパイル使用メモリ 87,072 KB
実行使用メモリ 292,292 KB
最終ジャッジ日時 2023-08-19 01:43:12
合計ジャッジ時間 32,416 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 76 ms
71,864 KB
testcase_01 AC 77 ms
71,932 KB
testcase_02 AC 77 ms
71,440 KB
testcase_03 AC 1,178 ms
79,076 KB
testcase_04 AC 1,181 ms
79,148 KB
testcase_05 AC 1,202 ms
79,236 KB
testcase_06 AC 1,186 ms
79,256 KB
testcase_07 AC 1,180 ms
79,184 KB
testcase_08 AC 381 ms
80,580 KB
testcase_09 AC 237 ms
79,124 KB
testcase_10 AC 139 ms
78,280 KB
testcase_11 AC 173 ms
78,632 KB
testcase_12 AC 493 ms
81,952 KB
testcase_13 AC 104 ms
77,584 KB
testcase_14 AC 831 ms
78,916 KB
testcase_15 AC 102 ms
77,692 KB
testcase_16 AC 100 ms
77,584 KB
testcase_17 AC 1,095 ms
79,048 KB
testcase_18 AC 892 ms
78,908 KB
testcase_19 AC 1,188 ms
79,012 KB
testcase_20 AC 850 ms
79,044 KB
testcase_21 AC 809 ms
79,040 KB
testcase_22 AC 765 ms
79,132 KB
testcase_23 AC 354 ms
78,988 KB
testcase_24 AC 507 ms
78,860 KB
testcase_25 AC 624 ms
79,020 KB
testcase_26 AC 1,758 ms
292,292 KB
testcase_27 AC 745 ms
108,012 KB
testcase_28 AC 633 ms
82,560 KB
testcase_29 AC 593 ms
82,776 KB
testcase_30 AC 137 ms
78,252 KB
testcase_31 AC 79 ms
71,808 KB
testcase_32 AC 78 ms
71,856 KB
testcase_33 AC 1,673 ms
286,212 KB
testcase_34 AC 1,673 ms
286,120 KB
testcase_35 AC 1,800 ms
290,912 KB
testcase_36 AC 1,495 ms
265,364 KB
testcase_37 AC 796 ms
81,972 KB
testcase_38 AC 508 ms
133,604 KB
testcase_39 AC 382 ms
99,540 KB
testcase_40 AC 1,197 ms
80,604 KB
testcase_41 AC 1,175 ms
82,796 KB
testcase_42 AC 75 ms
71,760 KB
testcase_43 AC 79 ms
71,680 KB
testcase_44 AC 75 ms
71,852 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):
  return bin(m).count('1')
n=int(input())
a=list(map(int,input().split()))

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

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