結果
問題 | No.1207 グラフX |
ユーザー | fgwiebfaoish |
提出日時 | 2020-08-30 17:04:31 |
言語 | C#(csc) (csc 3.9.0) |
結果 |
AC
|
実行時間 | 923 ms / 2,000 ms |
コード長 | 5,193 bytes |
コンパイル時間 | 4,663 ms |
コンパイル使用メモリ | 108,868 KB |
実行使用メモリ | 103,536 KB |
最終ジャッジ日時 | 2024-11-15 12:47:18 |
合計ジャッジ時間 | 36,459 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 917 ms
66,160 KB |
testcase_01 | AC | 921 ms
66,284 KB |
testcase_02 | AC | 923 ms
65,664 KB |
testcase_03 | AC | 917 ms
66,408 KB |
testcase_04 | AC | 900 ms
66,408 KB |
testcase_05 | AC | 815 ms
103,532 KB |
testcase_06 | AC | 817 ms
103,536 KB |
testcase_07 | AC | 827 ms
103,280 KB |
testcase_08 | AC | 637 ms
52,724 KB |
testcase_09 | AC | 748 ms
58,360 KB |
testcase_10 | AC | 791 ms
85,104 KB |
testcase_11 | AC | 806 ms
103,536 KB |
testcase_12 | AC | 638 ms
53,628 KB |
testcase_13 | AC | 305 ms
35,072 KB |
testcase_14 | AC | 875 ms
64,196 KB |
testcase_15 | AC | 708 ms
56,192 KB |
testcase_16 | AC | 314 ms
35,840 KB |
testcase_17 | AC | 528 ms
48,752 KB |
testcase_18 | AC | 488 ms
47,216 KB |
testcase_19 | AC | 468 ms
43,768 KB |
testcase_20 | AC | 891 ms
64,760 KB |
testcase_21 | AC | 74 ms
23,040 KB |
testcase_22 | AC | 544 ms
48,752 KB |
testcase_23 | AC | 611 ms
52,208 KB |
testcase_24 | AC | 441 ms
44,160 KB |
testcase_25 | AC | 899 ms
64,248 KB |
testcase_26 | AC | 705 ms
54,252 KB |
testcase_27 | AC | 818 ms
61,156 KB |
testcase_28 | AC | 740 ms
58,472 KB |
testcase_29 | AC | 775 ms
60,652 KB |
testcase_30 | AC | 351 ms
38,784 KB |
testcase_31 | AC | 265 ms
32,768 KB |
testcase_32 | AC | 323 ms
37,876 KB |
testcase_33 | AC | 329 ms
38,260 KB |
testcase_34 | AC | 723 ms
55,676 KB |
testcase_35 | AC | 76 ms
23,552 KB |
testcase_36 | AC | 708 ms
57,316 KB |
testcase_37 | AC | 586 ms
50,544 KB |
testcase_38 | AC | 153 ms
28,800 KB |
testcase_39 | AC | 363 ms
40,564 KB |
testcase_40 | AC | 216 ms
29,696 KB |
testcase_41 | AC | 442 ms
43,004 KB |
testcase_42 | AC | 24 ms
17,664 KB |
testcase_43 | AC | 24 ms
17,920 KB |
testcase_44 | AC | 25 ms
18,048 KB |
testcase_45 | AC | 882 ms
66,160 KB |
testcase_46 | AC | 872 ms
66,404 KB |
testcase_47 | AC | 896 ms
66,160 KB |
testcase_48 | AC | 873 ms
66,164 KB |
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc) Copyright (C) Microsoft Corporation. All rights reserved.
ソースコード
using System; using System.Collections.Generic; using System.Collections; using System.Collections.Specialized; using System.Linq; using System.Text; using System.IO; using System.Reflection; using static System.Math; using System.Numerics; static class Program{ const int mod=(int)1e9+7; const long inf=long.MaxValue-1; static List<ValueTuple<int,int>>[] li; static bool[] b; static Mint[] d; static int[] pa; static long x; static int n; static void Main(){ Sc sc=new Sc(); var s=sc.Ia; var g=new int[s[1]][]; n=s[0]; int m=n-1; x=s[2]; for(int i = 0;i<s[1];i++) {g[i]=sc.Ia;} Uf uf=new Uf(n+1); li=new List<ValueTuple<int,int>>[n+1]; b=new bool[n+1]; d=new Mint[n+1]; pa=new int[n+1]; for(int i=1;i<=n;i++){ li[i]=new List<ValueTuple<int,int>>(); } for(int i = 0;i<s[1];i++) { if(!uf.Same(g[i][0],g[i][1])){ uf.Union(g[i][0],g[i][1]); li[g[i][0]].Add(ValueTuple.Create(g[i][1],g[i][2])); li[g[i][1]].Add(ValueTuple.Create(g[i][0],g[i][2])); } } int o=1; Fu(o); b=new bool[n+1]; Fu2(o); Mint ans=0; for(int i = 1;i<=n;i++) {ans+=d[i];} Console.WriteLine("{0}",ans/2); } static int Fu2(int a){ b[a]=true; for(int i=0;i<li[a].Count;i++){ if(!b[li[a][i].Item1]){ var c=Fp(x,li[a][i].Item2); d[li[a][i].Item1]+=d[a]-d[li[a][i].Item1]-c*pa[li[a][i].Item1]+c*(n-pa[li[a][i].Item1]); Fu2(li[a][i].Item1); } } return 0; } static int Fu(int a){ b[a]=true; var p=1; for(int i=0;i<li[a].Count;i++){ if(!b[li[a][i].Item1]){ var k=Fu(li[a][i].Item1); var c=Fp(x,li[a][i].Item2); p+=k; d[a]+=c*k; d[a]+=d[li[a][i].Item1]; } } return pa[a]=p; } static long Fp(long x,long e){ long r=1; while(e>0){ if((e&1)>0){r*=x;r%=mod;} x=(x*x)%mod; e>>=1; } return r; } } public struct Mint{ const int mod=(int)1e9+7; private long d; public Mint(long d){this.d=d;} public static implicit operator Mint(long d){return new Mint(d%mod);} public static implicit operator bool(Mint d){return (int)d!=0;} public static explicit operator long(Mint d){return d.d;} public override string ToString(){return d.ToString();} public static Mint operator+(Mint a,long b){a.d=(a.d+b)%mod;return a;} public static Mint operator+(Mint a,Mint b){a.d=(a.d+b.d)%mod;return a;} public static Mint operator-(Mint a,long b){a.d=(mod+a.d-b)%mod;return a;} public static Mint operator-(Mint a,Mint b){a.d=(mod+a.d-b.d)%mod;return a;} public static Mint operator*(Mint a,long b){a.d=(a.d*b)%mod;return a;} public static Mint operator*(Mint a,Mint b){a.d=(a.d*b.d)%mod;return a;} public static Mint operator/(Mint a,long b){a.d=(a.d*Mi(b))%mod;return a;} public static Mint operator/(Mint a,Mint b){a.d=(a.d*Mi(b.d))%mod;return a;} private static long Mi(long a){ a=(a+mod)%mod; long b=mod,u=1,v=0; while(b>0){ long t=a/b; a-=t*b;(a,b)=(b,a); u-=t*v;(u,v)=(v,u); } u%=mod; if(u<0){u+=mod;} return u%mod; } } public class Uf{ private int[] arr,hi; public int all; public Uf(int n){ arr=new int[n]; hi=new int[n]; all=n; for(int i=0;i<n;i++){arr[i]=-1;} } public void Union(int a,int b){ a=Root(a); b=Root(b); if(a!=b){ if(hi[a]<hi[b]){(a,b)=(b,a);} arr[a]+=arr[b]; hi[a]=Max(hi[a],hi[b]+1); arr[b]=a; all--; } } public bool Same(int a,int b){return Root(a)==Root(b);} public int Size(int a){return -arr[Root(a)];} public int Root(int a){return arr[a]<0?a:arr[a]=Root(arr[a]);} } public class Sc{ public int I{get{return int.Parse(Console.ReadLine());}} public long L{get{return long.Parse(Console.ReadLine());}} public double D{get{return double.Parse(Console.ReadLine());}} public string S{get{return Console.ReadLine();}} public int[] Ia{get{return Array.ConvertAll(Console.ReadLine().Split(),int.Parse);}} public long[] La{get{return Array.ConvertAll(Console.ReadLine().Split(),long.Parse);}} public double[] Da{get{return Array.ConvertAll(Console.ReadLine().Split(),double.Parse);}} public string[] Sa{get{return Console.ReadLine().Split();}} public object[] Oa{get{return Console.ReadLine().Split();}} public int[] Ia2{get{return Array.ConvertAll(("0 "+Console.ReadLine()+" 0").Split(),int.Parse);}} public int[] Ia3(string a,string b){return Array.ConvertAll((a+Console.ReadLine()+b).Split(),int.Parse);} public long[] La2{get{return Array.ConvertAll(("0 "+Console.ReadLine()+" 0").Split(),long.Parse);}} public long[] La3(string a,string b){return Array.ConvertAll((a+Console.ReadLine()+b).Split(),long.Parse);} public double[] Da2{get{return Array.ConvertAll(("0 "+Console.ReadLine()+" 0").Split(),double.Parse);}} public double[] Da3(string a,string b){return Array.ConvertAll((a+Console.ReadLine()+b).Split(),double.Parse);} public T[] Arr<T>(int n,Func<T> f){var a=new T[n];for(int i=0;i<n;i++){a[i]=f();}return a;} public T[] Arr<T>(int n,Func<int,T> f){var a=new T[n];for(int i=0;i<n;i++){a[i]=f(i);}return a;} public T[] Arr<T>(int n,Func<string[],T> f){var a=new T[n];for(int i=0;i<n;i++){a[i]=f(Console.ReadLine().Split());}return a;} public T[] Arr<T>(int n,Func<int,string[],T> f){var a=new T[n];for(int i=0;i<n;i++){a[i]=f(i,Console.ReadLine().Split());}return a;} }