QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#309498#8131. Filesystemucup-team1631#WA 11ms10000kbPython35.4kb2024-01-20 17:48:412024-01-20 17:48:42

Judging History

你现在查看的是最新测评结果

  • [2024-01-20 17:48:42]
  • 评测
  • 测评结果:WA
  • 用时:11ms
  • 内存:10000kb
  • [2024-01-20 17:48:41]
  • 提交

answer

class MaxFlow:
    _INF = 9_223_372_036_854_775_807
 
    def __init__(self, n=0):
        self._n = n
        self._pos = []
        self._g = [[] for _ in range(n)]  # [to, rev, cap]
 
    def add_edge(self, from_, to, cap):
        assert 0 <= from_ < self._n
        assert 0 <= to < self._n
        assert 0 <= cap
        m = len(self._pos)
        from_id = len(self._g[from_])
        self._pos.append([from_, from_id])
        to_id = len(self._g[to])
        if from_ == to:
            to_id += 1
        self._g[from_].append([to, to_id, cap])
        self._g[to].append([from_, from_id, 0])
        return m
 
    def get_edge(self, i):
        m = len(self._pos)
        assert 0 <= i < m
        _e = self._g[self._pos[i][0]][self._pos[i][1]]
        _re = self._g[_e[0]][_e[1]]
        return [self._pos[i][0], _e[0], _e[2] + _re[2], _re[2]] # from, to, cap, flow
 
    def edges(self):
        m = len(self._pos)
        result = [self.get_edge(i) for i in range(m)]
        return result
 
    def change_edge(self, i, new_cap, new_flow):
        m = len(self._pos)
        assert 0 <= i < m
        assert 0 <= new_flow <= new_cap
        _e = self._g[self._pos[i][0]][self._pos[i][1]]
        _re = self._g[_e[0]][_e[1]]
        _e[2] = new_cap - new_flow
        _re[2] = new_flow
 
    def _flow_bfs(self, s, t):
        level = [-1] * self._n
        level[s] = 0
        que = [s]
        while que:
            next_que = []
            for v in que:
                for to, rev, cap in self._g[v]:
                    if cap == 0 or level[to] >= 0:
                        continue
                    level[to] = level[v] + 1
                    if to == t:
                        return level
                    next_que.append(to)
            que, next_que = next_que, que
        return level
 
    def flow(self, s, t, flow_limit=_INF):
        assert 0 <= s < self._n
        assert 0 <= t < self._n
        assert s != t
 
        flow = 0
        while flow < flow_limit:
            level = self._flow_bfs(s, t)
            if level[t] == -1:
                break
 
            iterator = [0] * self._n
            in_ = [0] * self._n
            out = [0] * self._n
 
            in_[t] = flow_limit - flow
            route = [t]
            while route:
                v = route[-1]
                if in_[v] == out[v] and v == t:
                    flow += out[t]
                    return flow
                if v == s or in_[v] == out[v]:
                    route.pop()
                    w = route[-1]
                    flow_vw = in_[v]
                    i = iterator[w]
                    to, rev, cap = self._g[w][i]
                    self._g[v][rev][2] -= flow_vw
                    self._g[w][i][2] += flow_vw
                    out[w] += flow_vw
                    continue
 
                for i in range(iterator[v], len(self._g[v])):
                    to, rev, cap = self._g[v][i]
                    if(level[to] == -1
                       or level[v] <= level[to]
                       or self._g[to][rev][2] == 0):
                        continue
                    in_[to] = min(in_[v]-out[v], self._g[to][rev][2])
                    out[to] = 0
                    route.append(to)
                    iterator[v] = i
                    break
                else:
                    iterator[v] = len(self._g[v])
                    route.pop()
                    if v == t:
                        if out[t] == 0:
                            return flow
                        flow += out[t]
                        continue
                    w = route[-1]
                    flow_vw = out[v]
                    i = iterator[w]
                    to, rev, cap = self._g[w][i]
                    self._g[v][rev][2] -= flow_vw
                    self._g[w][i][2] += flow_vw
                    out[w] += flow_vw
                    iterator[w] += 1
        return flow
 
    def min_cut(self, s):
        visited = [False] * self._n
        visited[s] = True
        que = [s]
        while que:
            next_que = []
            for p in que:
                for to, rev, cap in self._g[p]:
                    if cap > 0 and not visited[to]:
                        visited[to] = True
                        next_que.append(to)
            que, next_que = next_que, que
        return visited


from sys import stdin
input=lambda :stdin.readline()[:-1]

def solve():
  n,m=map(int,input().split())
  use=[0]*n
  for i in list(map(lambda x:int(x)-1,input().split())):
    use[i]=1
  p=list(map(lambda x:int(x)-1,input().split()))
  edges=[]
  S=n
  T=n+1
  now=0
  res=n+2
  while now<n:
    if use[now]==0:
      now+=1
    else:
      l=now
      while now+1<n and use[now+1]==1:
        now+=1
      r=now
      for i in range(l,r+1):
        edges.append([res,i])
      edges.append([S,res])
      now+=1
      res+=1
  now=0
  while now<n:
    if use[p[now]]==0:
      now+=1
    else:
      l=now
      while now+1<n and use[p[now+1]]==1:
        now+=1
      r=now
      for i in range(l,r+1):
        edges.append([p[i],res])
      edges.append([res,T])
      now+=1
      res+=1
  
  G=MaxFlow(res)
  for x,y in edges:
    G.add_edge(x,y,1)
  print(G.flow(S,T))

for _ in range(int(input())):
  solve()

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 11ms
memory: 9936kb

input:

2
12 8
2 5 8 3 4 10 12 1
10 5 8 6 4 7 2 3 11 12 1 9
8 4
1 3 5 7
1 4 5 8 7 6 3 2

output:

3
4

result:

ok 2 number(s): "3 4"

Test #2:

score: 0
Accepted
time: 4ms
memory: 9900kb

input:

100
10 3
10 5 2
2 4 9 8 7 3 1 5 10 6
10 3
8 1 10
8 4 3 7 1 2 9 5 6 10
10 3
6 5 2
8 7 6 4 2 10 1 3 5 9
10 3
5 8 4
10 4 5 7 8 9 1 2 3 6
10 3
8 4 10
10 6 9 2 8 7 1 4 3 5
10 3
9 8 1
8 5 6 10 2 4 1 7 9 3
10 3
5 4 1
7 5 8 4 3 6 9 10 2 1
10 3
2 4 3
6 7 3 9 1 2 5 8 4 10
10 3
9 5 3
6 10 7 4 9 3 1 8 5 2
10 3
...

output:

2
3
2
2
3
2
2
1
2
2
3
2
2
2
2
2
2
2
3
2
2
2
2
2
2
2
2
3
1
3
1
2
2
2
2
2
2
2
3
2
2
3
3
3
2
2
2
3
2
2
2
2
2
2
3
2
2
2
2
2
3
2
2
1
2
1
2
2
2
1
3
2
3
1
2
2
3
2
2
3
3
2
3
2
2
2
2
3
2
2
2
2
3
1
3
3
2
2
2
2

result:

ok 100 numbers

Test #3:

score: 0
Accepted
time: 9ms
memory: 9936kb

input:

100
10 5
2 6 1 9 3
10 8 2 6 9 7 5 1 3 4
10 5
7 10 1 3 6
5 7 3 9 4 1 8 2 10 6
10 5
8 3 6 2 9
4 2 9 3 8 1 6 10 7 5
10 5
5 6 7 4 3
3 7 6 2 1 5 9 8 10 4
10 5
1 6 8 10 5
8 6 3 10 1 4 7 9 5 2
10 5
6 5 1 3 9
8 10 5 3 6 1 9 2 7 4
10 5
8 6 2 7 1
3 6 5 10 9 1 2 7 4 8
10 5
6 10 8 4 2
3 9 7 5 4 1 10 6 2 8
10 5
...

output:

2
3
2
1
3
1
2
2
2
3
3
2
2
2
2
4
3
2
3
2
2
3
3
2
3
3
2
1
1
2
2
2
3
3
2
3
3
3
3
1
4
3
3
3
2
2
2
3
4
3
2
2
2
2
2
3
2
2
2
2
2
1
2
2
3
2
2
2
3
3
3
3
3
4
2
3
2
3
2
3
2
2
2
2
2
2
2
2
2
2
3
3
3
2
2
2
2
3
3
2

result:

ok 100 numbers

Test #4:

score: -100
Wrong Answer
time: 5ms
memory: 10000kb

input:

100
10 8
3 4 9 1 8 7 2 10
9 1 7 10 5 6 2 4 8 3
10 8
9 1 8 4 2 10 3 7
4 9 8 3 7 10 2 1 5 6
10 8
4 8 3 6 9 10 7 1
10 3 5 1 6 4 8 9 2 7
10 8
2 4 5 9 7 3 10 8
10 7 2 9 3 4 8 6 1 5
10 8
3 1 8 5 9 2 10 6
10 9 2 7 1 8 3 5 4 6
10 8
10 9 3 5 6 4 7 2
1 10 2 4 7 5 8 3 9 6
10 8
7 1 8 9 2 10 5 4
2 4 10 3 6 1 8 7...

output:

2
1
3
2
3
2
2
2
2
3
2
1
1
2
2
2
2
2
2
3
3
3
2
2
2
3
2
2
2
2
3
2
2
3
2
2
2
2
2
2
2
3
2
2
1
2
1
2
2
2
2
2
2
2
3
2
3
2
3
2
2
2
1
2
1
2
2
2
1
2
2
2
2
3
1
2
3
2
3
2
3
2
2
3
3
2
3
2
2
2
2
2
2
3
2
2
2
3
2
2

result:

wrong answer 48th numbers differ - expected: '3', found: '2'