結果

問題 No.1418 Sum of Sum of Subtree Size
ユーザー fgwiebfaoishfgwiebfaoish
提出日時 2021-03-05 22:57:52
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 276 ms / 2,000 ms
コード長 3,060 bytes
コンパイル時間 2,351 ms
コンパイル使用メモリ 110,688 KB
実行使用メモリ 41,084 KB
最終ジャッジ日時 2024-04-16 10:57:47
合計ジャッジ時間 8,093 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 26 ms
17,792 KB
testcase_01 AC 25 ms
17,792 KB
testcase_02 AC 27 ms
17,792 KB
testcase_03 AC 256 ms
35,072 KB
testcase_04 AC 264 ms
35,324 KB
testcase_05 AC 273 ms
35,188 KB
testcase_06 AC 258 ms
35,196 KB
testcase_07 AC 276 ms
35,576 KB
testcase_08 AC 184 ms
31,344 KB
testcase_09 AC 85 ms
26,496 KB
testcase_10 AC 88 ms
26,624 KB
testcase_11 AC 60 ms
24,704 KB
testcase_12 AC 135 ms
29,168 KB
testcase_13 AC 162 ms
30,316 KB
testcase_14 AC 164 ms
30,956 KB
testcase_15 AC 124 ms
28,652 KB
testcase_16 AC 36 ms
21,120 KB
testcase_17 AC 36 ms
21,376 KB
testcase_18 AC 230 ms
34,300 KB
testcase_19 AC 56 ms
24,448 KB
testcase_20 AC 31 ms
19,456 KB
testcase_21 AC 122 ms
28,400 KB
testcase_22 AC 108 ms
27,648 KB
testcase_23 AC 31 ms
19,712 KB
testcase_24 AC 31 ms
19,968 KB
testcase_25 AC 30 ms
19,072 KB
testcase_26 AC 32 ms
19,840 KB
testcase_27 AC 27 ms
18,048 KB
testcase_28 AC 27 ms
18,304 KB
testcase_29 AC 35 ms
20,352 KB
testcase_30 AC 32 ms
20,352 KB
testcase_31 AC 29 ms
18,944 KB
testcase_32 AC 30 ms
19,584 KB
testcase_33 AC 64 ms
26,624 KB
testcase_34 AC 213 ms
41,084 KB
testcase_35 AC 97 ms
30,208 KB
testcase_36 AC 46 ms
23,680 KB
testcase_37 AC 216 ms
35,316 KB
testcase_38 AC 194 ms
34,168 KB
testcase_39 AC 25 ms
17,920 KB
testcase_40 AC 25 ms
17,792 KB
testcase_41 AC 25 ms
17,664 KB
testcase_42 AC 26 ms
17,920 KB
testcase_43 AC 25 ms
17,920 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc)
Copyright (C) Microsoft Corporation. All rights reserved.

ソースコード

diff #

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;
using System.Threading;
using System.Runtime.CompilerServices;
static class Program{
	const int mod=(int)1e9+7;
	static List<int>[] li;
	static bool[] b;
	static int[] pa;
	static int n;
	static long ans=0;
	static long[] dp1,dp2,dp3;
	static void Main(){
		Sc sc=new Sc();
		var s=sc.Ia;
		n=s[0];
		int m=n-1;
		li=new List<int>[n+1];
		b=new bool[n+1];
		pa=new int[n+1];
		dp1=new long[n+1];
		dp2=new long[n+1];
		dp3=new long[n+1];
		for(int i=1;i<=n;i++){li[i]=new List<int>();}
		for(int i=0;i<m;i++){
			var e=sc.Ia;
			li[e[0]].Add(e[1]);
			li[e[1]].Add(e[0]);
		}
		Fu(1,0);
		Fu2(1);
		Console.WriteLine("{0}",n*(long)n+ans);
	}
	static void Fu2(int a) {
		//Console.WriteLine((a,n-dp1[a]));
		for(int i=0;i<li[a].Count;i++){
			if(li[a][i]!=pa[a]){
				Fu2(li[a][i]);
				ans+=(n-dp1[li[a][i]])*dp1[li[a][i]];
			}
		}
	}
	static void Fu(int a,int g){
		b[a]=true;
		pa[a]=g;
		for(int i=0;i<li[a].Count;i++){
			if(!b[li[a][i]]){
				Fu(li[a][i],a);
				dp1[a]+=dp1[li[a][i]];
				//dp2[a]+=dp2[li[a][i]];
			}
		}
		dp1[a]++;
		//dp2[a]+=dp1[a];
		ans+=(n-dp1[a])*dp1[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;}
}
0