結果
問題 | No.1207 グラフX |
ユーザー | Keroru |
提出日時 | 2020-08-30 17:05:18 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 1,147 ms / 2,000 ms |
コード長 | 10,379 bytes |
コンパイル時間 | 176 ms |
コンパイル使用メモリ | 82,640 KB |
実行使用メモリ | 251,564 KB |
最終ジャッジ日時 | 2024-04-27 11:22:57 |
合計ジャッジ時間 | 35,418 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 788 ms
242,028 KB |
testcase_01 | AC | 805 ms
241,268 KB |
testcase_02 | AC | 851 ms
251,564 KB |
testcase_03 | AC | 883 ms
242,408 KB |
testcase_04 | AC | 787 ms
241,884 KB |
testcase_05 | AC | 1,147 ms
231,056 KB |
testcase_06 | AC | 1,130 ms
230,504 KB |
testcase_07 | AC | 1,107 ms
230,776 KB |
testcase_08 | AC | 692 ms
195,324 KB |
testcase_09 | AC | 705 ms
199,140 KB |
testcase_10 | AC | 1,139 ms
227,408 KB |
testcase_11 | AC | 1,124 ms
230,716 KB |
testcase_12 | AC | 675 ms
188,020 KB |
testcase_13 | AC | 416 ms
127,760 KB |
testcase_14 | AC | 852 ms
213,768 KB |
testcase_15 | AC | 697 ms
196,356 KB |
testcase_16 | AC | 441 ms
126,704 KB |
testcase_17 | AC | 604 ms
175,592 KB |
testcase_18 | AC | 530 ms
171,936 KB |
testcase_19 | AC | 559 ms
148,476 KB |
testcase_20 | AC | 791 ms
227,640 KB |
testcase_21 | AC | 184 ms
95,808 KB |
testcase_22 | AC | 606 ms
170,616 KB |
testcase_23 | AC | 624 ms
178,672 KB |
testcase_24 | AC | 477 ms
167,008 KB |
testcase_25 | AC | 802 ms
216,100 KB |
testcase_26 | AC | 657 ms
182,892 KB |
testcase_27 | AC | 726 ms
218,220 KB |
testcase_28 | AC | 705 ms
203,428 KB |
testcase_29 | AC | 729 ms
216,984 KB |
testcase_30 | AC | 467 ms
139,804 KB |
testcase_31 | AC | 404 ms
126,036 KB |
testcase_32 | AC | 418 ms
144,232 KB |
testcase_33 | AC | 432 ms
142,872 KB |
testcase_34 | AC | 703 ms
193,052 KB |
testcase_35 | AC | 213 ms
96,224 KB |
testcase_36 | AC | 676 ms
206,448 KB |
testcase_37 | AC | 624 ms
183,324 KB |
testcase_38 | AC | 298 ms
111,436 KB |
testcase_39 | AC | 444 ms
148,872 KB |
testcase_40 | AC | 303 ms
109,776 KB |
testcase_41 | AC | 566 ms
153,856 KB |
testcase_42 | AC | 130 ms
91,796 KB |
testcase_43 | AC | 132 ms
91,528 KB |
testcase_44 | AC | 134 ms
91,836 KB |
testcase_45 | AC | 730 ms
247,248 KB |
testcase_46 | AC | 746 ms
245,280 KB |
testcase_47 | AC | 731 ms
244,872 KB |
testcase_48 | AC | 749 ms
245,188 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)