結果

問題 No.1507 Road Blocked
ユーザー fgwiebfaoishfgwiebfaoish
提出日時 2021-05-14 22:31:00
言語 C#(csc)
(csc 3.9.0)
結果
WA  
実行時間 -
コード長 5,558 bytes
コンパイル時間 2,273 ms
コンパイル使用メモリ 116,240 KB
実行使用メモリ 66,588 KB
最終ジャッジ日時 2024-10-02 02:15:44
合計ジャッジ時間 28,533 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
権限があれば一括ダウンロードができます
コンパイルメッセージ
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((c,d,c*(c-1)/2+d*(d-1)/2,c*d,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