結果

問題 No.1507 Road Blocked
ユーザー fgwiebfaoishfgwiebfaoish
提出日時 2021-05-14 22:32:08
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 415 ms / 2,000 ms
コード長 5,500 bytes
コンパイル時間 1,987 ms
コンパイル使用メモリ 114,360 KB
実行使用メモリ 58,748 KB
最終ジャッジ日時 2024-10-02 02:19:24
合計ジャッジ時間 14,638 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 27 ms
18,176 KB
testcase_01 AC 27 ms
18,304 KB
testcase_02 AC 26 ms
18,048 KB
testcase_03 AC 281 ms
58,748 KB
testcase_04 AC 400 ms
54,264 KB
testcase_05 AC 313 ms
54,260 KB
testcase_06 AC 319 ms
54,128 KB
testcase_07 AC 340 ms
54,004 KB
testcase_08 AC 390 ms
54,260 KB
testcase_09 AC 390 ms
54,268 KB
testcase_10 AC 405 ms
54,264 KB
testcase_11 AC 379 ms
54,136 KB
testcase_12 AC 404 ms
54,272 KB
testcase_13 AC 411 ms
54,260 KB
testcase_14 AC 398 ms
54,128 KB
testcase_15 AC 415 ms
54,132 KB
testcase_16 AC 395 ms
54,132 KB
testcase_17 AC 405 ms
54,260 KB
testcase_18 AC 412 ms
54,132 KB
testcase_19 AC 398 ms
54,012 KB
testcase_20 AC 394 ms
54,272 KB
testcase_21 AC 402 ms
54,128 KB
testcase_22 AC 401 ms
54,132 KB
testcase_23 AC 399 ms
54,252 KB
testcase_24 AC 402 ms
54,132 KB
testcase_25 AC 314 ms
54,384 KB
testcase_26 AC 316 ms
54,144 KB
testcase_27 AC 313 ms
54,144 KB
testcase_28 AC 312 ms
54,132 KB
testcase_29 AC 315 ms
54,004 KB
testcase_30 AC 311 ms
54,120 KB
testcase_31 AC 394 ms
54,256 KB
testcase_32 AC 396 ms
54,260 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=998244353;
	static List<int>[] li;
	static bool[] b;
	static int[] g;
	static void Main(){
		Sc sc=new Sc();
		var s=sc.Ia;
		int n=s[0],m=n-1;
		li=new List<int>[n+1];
		b=new bool[n+1];
		g=new int[n+1];
		for(int i=1;i<=n;i++){li[i]=new List<int>();}
		var h=new int[m][];
		for(int i=0;i<m;i++){
			h[i]=sc.Ia;
			li[h[i][0]].Add(h[i][1]);
			li[h[i][1]].Add(h[i][0]);
		}
		int r=1;
		Fu(r);
		var al=new Alca(li,true,r);
		Mint ans=0;
		for(int i = 0;i<m;i++) {
			long c=0,d=0;
			if(al.Di(r,h[i][0])<al.Di(r,h[i][1])){
				c=n-g[h[i][1]];
				d=g[h[i][1]];
			}
			else{
				c=g[h[i][0]];
				d=n-g[h[i][0]];
			}
			Mint p=(c*(c-1)/2+d*(d-1)/2)%mod;
			Mint q=(c*(c-1)/2+d*(d-1)/2+c*d)%mod;
			ans+=p/q;
		}
		Console.WriteLine("{0}",ans/(n-1));
	}
	static void Fu(int a){
		b[a]=true;
		g[a]=1;
		for(int i=0;i<li[a].Count;i++){
			if(!b[li[a][i]]){
				Fu(li[a][i]);
				g[a]+=g[li[a][i]];
			}
		}
	}
}
public struct Mint{
	const int mod=998244353;
	private long d;
	public Mint(long d){this.d=d;}
	public static implicit operator Mint(long d){return new Mint(d%mod);}
	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;}
	public static bool operator==(Mint a,long b){return (long)a==b;}
	public static bool operator!=(Mint a,long b){return (long)a!=b;}
	public override bool Equals(object obj){return false;}
	public override int GetHashCode(){return 0;}
	public 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 Alca{
	private List<int>[] li;
	private readonly int[] at,l;
	public readonly int[] ix,de;
	private readonly int[][] g;
	private int f=0;
	public Alca(List<int>[] li,bool bo,int r){
		int n=li.Length,z=(n<<1)-3,m=0;
		this.li=li;
		at=new int[z];
		ix=new int[n];
		de=new int[n];
		if(bo){Fu1(r,1);}
		else{
			for(int i = 1;i<n;i++) {
				if(de[i]==0){Fu1(i,1);f++;}
			}
		}
		m=(int)Log(z,2)+1;
		g=new int[m][];
		g[0]=new int[z];
		l=new int[z+1];
		for(int i=0;i<z;i++){
			g[0][i]=i;
			l[i+1]=l[i>>1]+1;
		}
		for(int i=1,k=1;i<m;i++,k<<=1){
			int p=z+1-(k<<1);
			g[i]=new int[p];
			for(int j=0;j<p;j++){g[i][j]=de[at[g[i-1][j]]]<de[at[g[i-1][j+k]]]?g[i-1][j]:g[i-1][j+k];}
		}
	}
	public int Lca(int a,int b){return Get(ix[a],ix[b]);}
	public int Di(int a,int b){
		int p=Get(ix[a],ix[b]);
		return p!=0?de[a]+de[b]-(de[p]<<1):-1;
	}
	private int Get(int a,int b){
		if(a>b){(a,b)=(b,a);}
		int p=l[b-a];
		return at[de[at[g[p][a]]]<de[at[g[p][b+1-(1<<p)]]]?g[p][a]:g[p][b+1-(1<<p)]];
	}
	private void Fu1(int a,int g){
		at[f]=a;
		de[a]=g;
		ix[a]=f;
		f++;
		for(int i=0;i<li[a].Count;i++){
			if(de[li[a][i]]==0){
				Fu1(li[a][i],g+1);
				at[f]=a;
				f++;
			}
		}
	}
}
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