結果
問題 | No.1207 グラフX |
ユーザー | Keroru |
提出日時 | 2020-08-30 17:05:18 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 1,278 ms / 2,000 ms |
コード長 | 10,379 bytes |
コンパイル時間 | 493 ms |
コンパイル使用メモリ | 82,152 KB |
実行使用メモリ | 251,932 KB |
最終ジャッジ日時 | 2024-11-15 12:51:59 |
合計ジャッジ時間 | 38,211 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 888 ms
241,772 KB |
testcase_01 | AC | 866 ms
241,392 KB |
testcase_02 | AC | 889 ms
251,932 KB |
testcase_03 | AC | 863 ms
242,680 KB |
testcase_04 | AC | 868 ms
241,804 KB |
testcase_05 | AC | 1,229 ms
230,588 KB |
testcase_06 | AC | 1,222 ms
230,188 KB |
testcase_07 | AC | 1,256 ms
230,844 KB |
testcase_08 | AC | 784 ms
195,604 KB |
testcase_09 | AC | 809 ms
199,088 KB |
testcase_10 | AC | 1,278 ms
227,436 KB |
testcase_11 | AC | 1,233 ms
230,492 KB |
testcase_12 | AC | 762 ms
187,552 KB |
testcase_13 | AC | 469 ms
127,900 KB |
testcase_14 | AC | 904 ms
213,820 KB |
testcase_15 | AC | 772 ms
198,048 KB |
testcase_16 | AC | 475 ms
126,788 KB |
testcase_17 | AC | 668 ms
175,176 KB |
testcase_18 | AC | 577 ms
171,924 KB |
testcase_19 | AC | 654 ms
148,188 KB |
testcase_20 | AC | 889 ms
227,412 KB |
testcase_21 | AC | 202 ms
95,736 KB |
testcase_22 | AC | 660 ms
170,364 KB |
testcase_23 | AC | 717 ms
178,528 KB |
testcase_24 | AC | 529 ms
166,532 KB |
testcase_25 | AC | 917 ms
215,764 KB |
testcase_26 | AC | 750 ms
182,784 KB |
testcase_27 | AC | 822 ms
217,696 KB |
testcase_28 | AC | 788 ms
203,060 KB |
testcase_29 | AC | 793 ms
216,564 KB |
testcase_30 | AC | 514 ms
139,856 KB |
testcase_31 | AC | 436 ms
126,028 KB |
testcase_32 | AC | 469 ms
144,212 KB |
testcase_33 | AC | 483 ms
142,664 KB |
testcase_34 | AC | 777 ms
193,364 KB |
testcase_35 | AC | 230 ms
96,412 KB |
testcase_36 | AC | 775 ms
206,608 KB |
testcase_37 | AC | 724 ms
183,216 KB |
testcase_38 | AC | 344 ms
111,692 KB |
testcase_39 | AC | 500 ms
148,484 KB |
testcase_40 | AC | 336 ms
110,044 KB |
testcase_41 | AC | 642 ms
153,528 KB |
testcase_42 | AC | 138 ms
91,724 KB |
testcase_43 | AC | 138 ms
91,560 KB |
testcase_44 | AC | 145 ms
91,628 KB |
testcase_45 | AC | 823 ms
247,272 KB |
testcase_46 | AC | 832 ms
244,916 KB |
testcase_47 | AC | 829 ms
245,064 KB |
testcase_48 | AC | 852 ms
245,244 KB |
ソースコード
import sys,bisect,string,math,time,functools,random,fractions from heapq import heappush,heappop,heapify from collections import deque,defaultdict,Counter from itertools import permutations,combinations,groupby rep=range;R=range def Golf():n,*t=map(int,open(0).read().split()) def I():return int(input()) def S_():return input() def IS():return input().split() def LS():return [i for i in input().split()] def MI():return map(int,input().split()) def LI():return [int(i) for i in input().split()] def LI_():return [int(i)-1 for i in input().split()] def NI(n):return [int(input()) for i in range(n)] def NI_(n):return [int(input())-1 for i in range(n)] def NLI(n):return [[int(i) for i in input().split()] for i in range(n)] def NLI_(n):return [[int(i)-1 for i in input().split()] for i in range(n)] def StoLI():return [ord(i)-97 for i in input()] def ItoS(n):return chr(n+97) def LtoS(ls):return ''.join([chr(i+97) for i in ls]) def RA():return map(int,open(0).read().split()) def RLI(n=8,a=1,b=10):return [random.randint(a,b)for i in range(n)] def RI(a=1,b=10):return random.randint(a,b) def Rtest(T): case,err=0,0 for i in range(T): inp=INP() a1,ls=naive(*inp) a2=solve(*inp) if a1!=a2: print((a1,a2),inp) err+=1 case+=1 print('Tested',case,'case with',err,'errors') def GI(V,E,ls=None,Directed=False,index=1): org_inp=[];g=[[] for i in range(V)] FromStdin=True if ls==None else False for i in range(E): if FromStdin: inp=LI() org_inp.append(inp) else: inp=ls[i] if len(inp)==2: a,b=inp;c=1 else: a,b,c=inp if index==1:a-=1;b-=1 aa=(a,c);bb=(b,c);g[a].append(bb) if not Directed:g[b].append(aa) return g,org_inp def GGI(h,w,search=None,replacement_of_found='.',mp_def={'#':1,'.':0},boundary=1): #h,w,g,sg=GGI(h,w,search=['S','G'],replacement_of_found='.',mp_def={'#':1,'.':0},boundary=1) # sample usage mp=[boundary]*(w+2);found={} for i in R(h): s=input() for char in search: if char in s: found[char]=((i+1)*(w+2)+s.index(char)+1) mp_def[char]=mp_def[replacement_of_found] mp+=[boundary]+[mp_def[j] for j in s]+[boundary] mp+=[boundary]*(w+2) return h+2,w+2,mp,found def TI(n):return GI(n,n-1) def accum(ls): rt=[0] for i in ls:rt+=[rt[-1]+i] return rt def bit_combination(n,base=2): rt=[] for tb in R(base**n):s=[tb//(base**bt)%base for bt in R(n)];rt+=[s] return rt def gcd(x,y): if y==0:return x if x%y==0:return y while x%y!=0:x,y=y,x%y return y def YN(x):print(['NO','YES'][x]) def Yn(x):print(['No','Yes'][x]) def show(*inp,end='\n'): if show_flg:print(*inp,end=end) mo=10**9+7 #mo=998244353 inf=float('inf') FourNb=[(-1,0),(1,0),(0,1),(0,-1)];EightNb=[(-1,0),(1,0),(0,1),(0,-1),(1,1),(-1,-1),(1,-1),(-1,1)];compas=dict(zip('WENS',FourNb));cursol=dict(zip('LRUD',FourNb)) l_alp=string.ascii_lowercase #sys.setrecursionlimit(10**9) read=sys.stdin.buffer.read;readline=sys.stdin.buffer.readline;input=lambda:sys.stdin.readline().rstrip() class UnionFind: def __init__(self, n): # 負 : 根であることを示す。絶対値はランクを示す # 非負: 根でないことを示す。値は親を示す self.table = [-1] * n def _root(self, x): if self.table[x] < 0: return x else: # 経路の圧縮 self.table[x] = self._root(self.table[x]) return self.table[x] def find(self, x, y): return self._root(x) == self._root(y) def union(self, x, y): r1 = self._root(x) r2 = self._root(y) if r1 == r2: return # ランクの取得 d1 = self.table[r1] d2 = self.table[r2] if d1 <= d2: self.table[r2] = r1 if d1 == d2: self.table[r1] -= 1 else: self.table[r1] = r2 def __str__(self): rt=[i if j<0 else j for i,j in enumerate(self.table)] return str(rt) class Tree: def __init__(self,inp_size=None,ls=None,init=True,index=1): self.LCA_init_stat=False self.ETtable=[] if init: if ls==None: self.stdin(inp_size,index=index) else: self.node_size=len(ls)+1 self.edges,_=GI(self.node_size,self.node_size-1,ls,index=index) return def stdin(self,inp_size=None,index=1): if inp_size==None: self.node_size=int(input()) else: self.node_size=inp_size self.edges,_=GI(self.node_size,self.node_size-1,index=index) return def listin(self,ls,index=0): self.node_size=len(ls)+1 self.edges,_=GI(self.node_size,self.node_size-1,ls,index=index) return def dfs(self,x,func=lambda pr,prv,nx,dist:prv+dist,root_v=0): q=deque([x]) v=[None]*self.node_size v[x]=root_v while q: c=q.pop() for nb,d in self.edges[c]: if v[nb]==None: q.append(nb) v[nb]=func(c,v[c],nb,d) return v def bfs(self,x,func=lambda pr,prv,nx,dist:prv+dist,root_v=0): q=deque([x]) v=[None]*self.node_size v[x]=root_v while q: c=q.popleft() for nb,d in self.edges[c]: if v[nb]==None: q.append(nb) v[nb]=func(c,v[c],nb,d) return v def parent(self,x): return self.dfs(0,func=lambda pr,prv,nx,dist:pr,root_v=-1) def topological_sort(self,x): # return topological sort of the tree tps=[] q=deque([x]) v=[None]*self.node_size v[x]=0 while q: c=q.popleft() tps.append(c) for nb,d in self.edges[c]: if v[nb]==None: q.append(nb) v[nb]=0 return tps def EulerTour(self,x): q=deque() q.append(x) self.depth=[None]*self.node_size self.depth[x]=0 self.ETtable=[] self.ETdepth=[] self.ETin=[-1]*self.node_size self.ETout=[-1]*self.node_size cnt=0 while q: c=q.pop() if c<0: ce=~c else: ce=c for nb,d in self.edges[ce]: if self.depth[nb]==None: q.append(~ce) q.append(nb) self.depth[nb]=self.depth[ce]+1 self.ETtable.append(ce) self.ETdepth.append(self.depth[ce]) if self.ETin[ce]==-1: self.ETin[ce]=cnt else: self.ETout[ce]=cnt cnt+=1 return def doubling_LCA(self,root,x,y): if self.LCA_init_stat==False: self.depth=[None]*self.node_size self.depth=self.bfs(0,func=lambda pr,prv,nxt,dist:prv+1) self.par=self.bfs(0,func=lambda pr,prv,nxt,dist:pr) self.db=[self.par] for i in range(self.node_size.bit_length()): #show(self.db) self.db+=[[self.db[-1][self.db[-1][i]] for i in range(self.node_size)]] self.LCA_init_stat=True dx=self.depth[x] dy=self.depth[y] if dx>dy: dx,dy=dy,dx c=self.node_size.bit_length() while c>0: if dx+c<=dy: x=self.db[c][x] dx+=1<<c c>>=1 if x==y: return x c=self.node_size.bit_length() while c>0: if self.db[c][x]!=self.db[c][y]: x=self.db[c][x] y=self.db[c][y] c>>=1 return self.par[x] def LCA_init(self,root): self.EulerTour(root) self.st=SparseTable(self.ETdepth,init_func=min,init_idl=inf) #self.st=SegTree(self.node_size*2-1,self.ETdepth,function=min,ide=inf) self.LCA_init_stat=True return def LCA(self,root,x,y): if self.LCA_init_stat==False: self.LCA_init(root) xin,xout=self.ETin[x],self.ETout[x] yin,yout=self.ETin[y],self.ETout[y] a=min(xin,yin) b=max(xout,yout,xin,yin) id_of_min_dep_in_et=self.st.query_id(a,b+1) return self.ETtable[id_of_min_dep_in_et] def __str__(self): return str(self.edges) def show(self): if all([all([d==1 for nd,d in e]) for e in self.edges]): print( [[nd for nd,d in e] for e in self.edges] ) else: print(self) def dfs2(self,x,func=lambda pr,prv,nx,dist:prv+dist,root_v=0): q=deque([x]) v=[None]*self.node_size v[x]=root_v dfs_tr=[x] while q: c=q.pop() for nb,d in self.edges[c]: if v[nb]==None: q.append(nb) v[nb]=func(c,v[c],nb,d) dfs_tr+=nb, return v,dfs_tr show_flg=False show_flg=True ans=0 for i in range(1): ans=0 n,m,x=LI() r=[] for i in range(m): a,b,c=LI_() c+=1 r+=(c,(a,b)), g=[[]for i in range(n)] dc={} uf=UnionFind(n) r.sort(key=lambda x:-x[0]) while r: c,(a,b)=r.pop() if uf._root(a)==uf._root(b): continue uf.union(a,b) dc[(a,b)]=c dc[(b,a)]=c g[a]+=b, g[b]+=a, def dfs(x,func=lambda pr,prv,nx,dist:prv+dist,root_v=0): q=deque([x]) v=[None]*n p=[-1]*n v[x]=root_v dfs_tr=[x] while q: c=q.pop() for nb in g[c]: if v[nb]==None: q.append(nb) v[nb]=func(c,v[c],nb,1) p[nb]=c dfs_tr+=nb, return v,dfs_tr,p v,dfs_tr,par=dfs(0) ch=[0]*n for c in dfs_tr[::-1]: if par[c]==-1: continue ch[par[c]]+=ch[c]+1 ans+=(ch[c]+1)*(n-ch[c]-1)*pow(x,dc[(c,par[c])],mo) ans%=mo print(ans)