QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#338627 | #5548. Increase the Toll Fees | vwxyz | TL | 899ms | 54944kb | Python3 | 18.6kb | 2024-02-26 05:02:10 | 2024-02-26 05:02:10 |
Judging History
answer
from collections import defaultdict
class Graph:
def __init__(self,V,edges=None,graph=None,directed=False,weighted=False,inf=float("inf")):
self.V=V
self.directed=directed
self.weighted=weighted
self.inf=inf
if graph!=None:
self.graph=graph
"""
self.edges=[]
for i in range(self.V):
if self.weighted:
for j,d in self.graph[i]:
if self.directed or not self.directed and i<=j:
self.edges.append((i,j,d))
else:
for j in self.graph[i]:
if self.directed or not self.directed and i<=j:
self.edges.append((i,j))
"""
else:
self.edges=edges
self.graph=[[] for i in range(self.V)]
if weighted:
for i,j,d in self.edges:
self.graph[i].append((j,d))
if not self.directed:
self.graph[j].append((i,d))
else:
for i,j in self.edges:
self.graph[i].append(j)
if not self.directed:
self.graph[j].append(i)
def Build_HLD(self,s):
self.hld_parents,size,self.hld_depth=self.SIV_DFS(s,parents=True,subtree_size=True,unweighted_dist=True)
stack=[s]
self.hld_tour=[]
self.hld_path_parents=[None]*self.V
self.hld_path_parents[s]=s
while stack:
x=stack.pop()
self.hld_tour.append(x)
max_size=0
max_size_y=None
for y in self.graph[x]:
if self.weighted:
y,d=y
if y==self.hld_parents[x]:
continue
if max_size<size[y]:
max_size=size[y]
max_size_y=y
for y in self.graph[x]:
if self.weighted:
y,d=y
if y==self.hld_parents[x]:
continue
if y!=max_size_y:
stack.append(y)
self.hld_path_parents[y]=y
if max_size_y!=None:
stack.append(max_size_y)
self.hld_path_parents[max_size_y]=self.hld_path_parents[x]
self.hld_tour_idx=[None]*self.V
for i in range(self.V):
self.hld_tour_idx[self.hld_tour[i]]=i
def HLD(self,a,b,edge=False):
L,R=[],[]
while self.hld_path_parents[a]!=self.hld_path_parents[b]:
if self.hld_depth[self.hld_path_parents[a]]<self.hld_depth[self.hld_path_parents[b]]:
R.append((self.hld_tour_idx[self.hld_path_parents[b]],self.hld_tour_idx[b]+1))
b=self.hld_parents[self.hld_path_parents[b]]
else:
L.append((self.hld_tour_idx[a]+1,self.hld_tour_idx[self.hld_path_parents[a]]))
a=self.hld_parents[self.hld_path_parents[a]]
if edge:
if self.hld_depth[a]!=self.hld_depth[b]:
retu=L+[(self.hld_tour_idx[a]+1,self.hld_tour_idx[b]+1)]+R[::-1]
else:
retu=L+R[::-1]
else:
if self.hld_depth[a]<self.hld_depth[b]:
retu=L+[(self.hld_tour_idx[a],self.hld_tour_idx[b]+1)]+R[::-1]
else:
retu=L+[(self.hld_tour_idx[a]+1,self.hld_tour_idx[b])]+R[::-1]
return retu
def Kruskal(self,maximize=False,spanning_tree=False):
UF=UnionFind(self.V)
sorted_edges=sorted(self.edges if self.weighted else [(x,y,1) for x,y in self.edges],key=lambda tpl:tpl[2],reverse=maximize)
if spanning_tree:
st=[]
else:
cost=0
for x,y,d in sorted_edges:
if not UF.Same(x,y):
UF.Union(x,y)
if spanning_tree:
st.append((x,y,d))
else:
cost+=d
return st if spanning_tree else cost
def SIV_DFS(self,s,bipartite_graph=False,cycle_detection=False,directed_acyclic=False,euler_tour=False,linked_components=False,lowlink=False,parents=False,postorder=False,preorder=False,subtree_size=False,topological_sort=False,unweighted_dist=False,weighted_dist=False):
seen=[False]*self.V
finished=[False]*self.V
if directed_acyclic or cycle_detection or topological_sort:
dag=True
if euler_tour:
et=[]
if linked_components:
lc=[]
if lowlink:
order=[None]*self.V
ll=[None]*self.V
idx=0
if parents or cycle_detection or lowlink or subtree_size:
ps=[None]*self.V
if postorder or topological_sort:
post=[]
if preorder:
pre=[]
if subtree_size:
ss=[1]*self.V
if unweighted_dist or bipartite_graph:
uwd=[self.inf]*self.V
uwd[s]=0
if weighted_dist:
wd=[self.inf]*self.V
wd[s]=0
stack=[(s,0)] if self.weighted else [s]
while stack:
if self.weighted:
x,d=stack.pop()
else:
x=stack.pop()
if not seen[x]:
seen[x]=True
stack.append((x,d) if self.weighted else x)
if euler_tour:
et.append(x)
if linked_components:
lc.append(x)
if lowlink:
order[x]=idx
ll[x]=idx
idx+=1
if preorder:
pre.append(x)
for y in self.graph[x]:
if self.weighted:
y,d=y
if not seen[y]:
stack.append((y,d) if self.weighted else y)
if parents or cycle_detection or lowlink or subtree_size:
ps[y]=x
if unweighted_dist or bipartite_graph:
uwd[y]=uwd[x]+1
if weighted_dist:
wd[y]=wd[x]+d
elif not finished[y]:
if (directed_acyclic or cycle_detection or topological_sort) and dag:
dag=False
if cycle_detection:
cd=(y,x)
elif not finished[x]:
finished[x]=True
if euler_tour:
et.append(~x)
if lowlink:
bl=True
for y in self.graph[x]:
if self.weighted:
y,d=y
if ps[x]==y and bl:
bl=False
continue
ll[x]=min(ll[x],order[y])
if x!=s:
ll[ps[x]]=min(ll[ps[x]],ll[x])
if postorder or topological_sort:
post.append(x)
if subtree_size:
for y in self.graph[x]:
if self.weighted:
y,d=y
if y==ps[x]:
continue
ss[x]+=ss[y]
if bipartite_graph:
bg=[[],[]]
for tpl in self.edges:
x,y=tpl[:2] if self.weighted else tpl
if uwd[x]==self.inf or uwd[y]==self.inf:
continue
if not uwd[x]%2^uwd[y]%2:
bg=False
break
else:
for x in range(self.V):
if uwd[x]==self.inf:
continue
bg[uwd[x]%2].append(x)
retu=()
if bipartite_graph:
retu+=(bg,)
if cycle_detection:
if dag:
cd=[]
else:
y,x=cd
cd=self.Route_Restoration(y,x,ps)
retu+=(cd,)
if directed_acyclic:
retu+=(dag,)
if euler_tour:
retu+=(et,)
if linked_components:
retu+=(lc,)
if lowlink:
retu=(ll,)
if parents:
retu+=(ps,)
if postorder:
retu+=(post,)
if preorder:
retu+=(pre,)
if subtree_size:
retu+=(ss,)
if topological_sort:
if dag:
tp_sort=post[::-1]
else:
tp_sort=[]
retu+=(tp_sort,)
if unweighted_dist:
retu+=(uwd,)
if weighted_dist:
retu+=(wd,)
if len(retu)==1:
retu=retu[0]
return retu
class Segment_Tree:
def __init__(self,N,f,e,lst=None,dynamic=False):
self.f=f
self.e=e
self.N=N
if dynamic:
self.segment_tree=defaultdict(lambda:self.e)
else:
if lst==None:
self.segment_tree=[self.e]*2*self.N
else:
assert len(lst)<=self.N
self.segment_tree=[self.e]*self.N+[x for x in lst]+[self.e]*(N-len(lst))
for i in range(self.N-1,0,-1):
self.segment_tree[i]=self.f(self.segment_tree[i<<1],self.segment_tree[i<<1|1])
def __getitem__(self,i):
if type(i)==int:
if -self.N<=i<0:
return self.segment_tree[i+self.N*2]
elif 0<=i<self.N:
return self.segment_tree[i+self.N]
else:
raise IndexError("list index out of range")
else:
a,b,c=i.start,i.stop,i.step
if a==None:
a=self.N
else:
a+=self.N
if b==None:
b=self.N*2
else:
b+=self.N
return self.segment_tree[slice(a,b,c)]
def __setitem__(self,i,x):
if -self.N<=i<0:
i+=self.N*2
elif 0<=i<self.N:
i+=self.N
else:
raise IndexError("list index out of range")
self.segment_tree[i]=x
while i>1:
i>>= 1
self.segment_tree[i]=self.f(self.segment_tree[i<<1],self.segment_tree[i<<1|1])
def Build(self,lst):
for i,x in enumerate(lst,self.N):
self.segment_tree[i]=x
for i in range(self.N-1,0,-1):
self.segment_tree[i]=self.f(self.segment_tree[i<<1],self.segment_tree[i<<1|1])
def Fold(self,L=None,R=None):
if L==None:
L=self.N
else:
L+=self.N
if R==None:
R=self.N*2
else:
R+=self.N
vL=self.e
vR=self.e
while L<R:
if L&1:
vL=self.f(vL,self.segment_tree[L])
L+=1
if R&1:
R-=1
vR=self.f(self.segment_tree[R],vR)
L>>=1
R>>=1
return self.f(vL,vR)
def Fold_Index(self,L=None,R=None):
if L==None:
L=self.N
else:
L+=self.N
if R==None:
R=self.N*2
else:
R+=self.N
if L==R:
return None
x=self.Fold(L-self.N,R-self.N)
while L<R:
if L&1:
if self.segment_tree[L]==x:
i=L
break
L+=1
if R&1:
R-=1
if self.segment_tree[R]==x:
i=R
break
L>>=1
R>>=1
while i<self.N:
if self.segment_tree[i]==self.segment_tree[i<<1]:
i<<=1
else:
i<<=1
i|=1
i-=self.N
return i
def Bisect_Right(self,L=None,f=None):
if L==self.N:
return self.N
if L==None:
L=0
L+=self.N
vl=self.e
vr=self.e
l,r=L,self.N*2
while l<r:
if l&1:
vl=self.f(vl,self.segment_tree[l])
l+=1
if r&1:
r-=1
vr=self.f(self.segment_tree[r],vr)
l>>=1
r>>=1
if f(self.f(vl,vr)):
return self.N
v=self.e
while True:
while L%2==0:
L>>=1
vv=self.f(v,self.segment_tree[L])
if f(vv):
v=vv
L+=1
else:
while L<self.N:
L<<=1
vv=self.f(v,self.segment_tree[L])
if f(vv):
v=vv
L+=1
return L-self.N
def Bisect_Left(self,R=None,f=None):
if R==0:
return 0
if R==None:
R=self.N
R+=self.N
vl=self.e
vr=self.e
l,r=self.N,R
while l<r:
if l&1:
vl=self.f(vl,self.segment_tree[l])
l+=1
if r&1:
r-=1
vr=self.f(self.segment_tree[r],vr)
l>>=1
r>>=1
if f(self.f(vl,vr)):
return 0
v=self.e
while True:
R-=1
while R>1 and R%2:
R>>=1
vv=self.f(self.segment_tree[R],v)
if f(vv):
v=vv
else:
while R<self.N:
R=2*R+1
vv=self.f(self.segment_tree[R],v)
if f(vv):
v=vv
R-=1
return R+1-self.N
def __str__(self):
return "["+", ".join(map(str,self.segment_tree[self.N:]))+"]"
class UnionFind:
def __init__(self,N,label=None,f=None,weighted=False,rollback=False):
self.N=N
self.parents=[None]*self.N
self.size=[1]*self.N
self.roots={i for i in range(self.N)}
self.label=label
if self.label!=None:
self.label=[x for x in label]
self.f=f
self.weighted=weighted
if self.weighted:
self.weight=[0]*self.N
self.rollback=rollback
if self.rollback:
self.operate_list=[]
self.operate_set=[]
def Find(self,x):
stack=[]
while self.parents[x]!=None:
stack.append(x)
x=self.parents[x]
if not self.rollback:
if self.weighted:
w=0
for y in stack[::-1]:
self.parents[y]=x
w+=self.weight[y]
self.weight[y]=w
else:
for y in stack[::-1]:
self.parents[y]=x
return x
def Union(self,x,y,w=None):
root_x=self.Find(x)
root_y=self.Find(y)
if self.rollback:
self.operate_list.append([])
self.operate_set.append([])
if root_x==root_y:
if self.weighted:
if self.weight[y]-self.weight[x]==w:
return True
else:
return False
else:
if self.size[root_x]<self.size[root_y]:
x,y=y,x
root_x,root_y=root_y,root_x
if self.weighted:
w=-w
if self.rollback:
self.operate_list[-1].append((self.parents,root_y,self.parents[root_y]))
self.operate_list[-1].append((self.size,root_x,self.size[root_x]))
self.operate_set[-1].append(root_y)
if self.label!=None:
self.operate_list[-1]((self.label,root_x,self.label[root_x]))
if self.weighted:
self.operate_list[-1].append((self.weight,root_y,self.weight[root_y]))
self.parents[root_y]=root_x
self.size[root_x]+=self.size[root_y]
self.roots.remove(root_y)
if self.label!=None:
self.label[root_x]=self.f(self.label[root_x],self.label[root_y])
if self.weighted:
self.weight[root_y]=w+self.weight[x]-self.weight[y]
def Size(self,x):
return self.size[self.Find(x)]
def Same(self,x,y):
return self.Find(x)==self.Find(y)
def Label(self,x):
return self.label[self.Find(x)]
def Weight(self,x,y):
root_x=self.Find(x)
root_y=self.Find(y)
if root_x!=root_y:
return None
return self.weight[y]-self.weight[x]
def Roots(self):
return list(self.roots)
def Linked_Components_Count(self):
return len(self.roots)
def Linked_Components(self):
linked_components=defaultdict(list)
for x in range(self.N):
linked_components[self.Find(x)].append(x)
return linked_components
def Rollback(self):
assert self.rollback
if self.operate_list:
for lst,x,v in self.operate_list.pop():
lst[x]=v
for x in self.operate_set.pop():
self.roots.add(x)
return True
else:
return False
def __str__(self):
linked_components=defaultdict(list)
for x in range(self.N):
linked_components[self.Find(x)].append(x)
return "\n".join(f"{r}: {linked_components[r]}" for r in sorted(list(linked_components.keys())))
N,M=map(int,input().split())
edges=[]
for m in range(M):
u,v,w=map(int,input().split())
u-=1;v-=1
edges.append((u,v,w))
T0=Graph(N,edges=edges,weighted=True).Kruskal(spanning_tree=True)
set_T0=set(T0)
T1=Graph(N,edges=[(u,v,w) for u,v,w in edges if not(u,v,w) in set_T0],weighted=True).Kruskal(spanning_tree=True)
if len(T1)<N-1:
ans=-1
else:
T1=Graph(N,edges=T1,weighted=True)
T1.Build_HLD(0)
inf=1<<30
ST=[-inf]*N
for u in range(N):
for v,w in T1.graph[u]:
if v==T1.hld_parents[u]:
continue
ST[T1.hld_tour_idx[v]]=w
ST=Segment_Tree(N,max,-inf,ST)
ans=0
for u,v,w in T0:
ma=-inf
for a,b in T1.HLD(u,v,edge=True):
if a>b:
a,b=b,a
ma=max(ma,ST.Fold(a,b))
ans+=ma+1-w
print(ans)
详细
Test #1:
score: 100
Accepted
time: 14ms
memory: 10336kb
input:
4 6 1 2 2 1 3 5 1 4 5 2 3 3 2 4 5 3 4 4
output:
9
result:
ok single line: '9'
Test #2:
score: 0
Accepted
time: 14ms
memory: 10352kb
input:
3 4 1 2 3 2 3 4 1 3 5 1 3 10
output:
-1
result:
ok single line: '-1'
Test #3:
score: 0
Accepted
time: 9ms
memory: 10444kb
input:
5 10 1 2 14 1 3 14 1 4 9 1 5 15 2 3 8 2 3 10 2 4 13 3 4 8 4 5 10 4 5 15
output:
21
result:
ok single line: '21'
Test #4:
score: 0
Accepted
time: 14ms
memory: 10484kb
input:
2 1 1 2 1
output:
-1
result:
ok single line: '-1'
Test #5:
score: 0
Accepted
time: 899ms
memory: 54944kb
input:
29171 100000 7223 21138 270743473 5598 27967 847631606 12666 26050 847631606 75 15747 270743473 8522 12955 847631606 6069 23750 270743473 18708 22605 847631606 16814 22169 847631606 11550 27119 847631606 562 15959 847631606 9400 11110 270743473 15325 23805 270743473 19169 24404 270743473 6649 12062 ...
output:
16827826868780
result:
ok single line: '16827826868780'
Test #6:
score: -100
Time Limit Exceeded
input:
47977 200000 10970 47321 440845807 1166 29708 767952745 319 37520 546280762 17581 29425 558321466 22079 26884 344816304 7479 44260 791002634 14685 44163 837529020 1537 10359 330017953 8634 27064 969738917 32950 37192 728271930 34751 42782 63025978 32540 34226 86057211 36786 46050 826927874 30444 436...
output:
-1