結果

問題 No.1451 集団登校
ユーザー fgwiebfaoishfgwiebfaoish
提出日時 2021-03-31 15:00:08
言語 C#(csc)
(csc 3.9.0)
結果
WA  
実行時間 -
コード長 6,192 bytes
コンパイル時間 3,755 ms
コンパイル使用メモリ 111,104 KB
実行使用メモリ 32,116 KB
最終ジャッジ日時 2024-12-14 22:36:02
合計ジャッジ時間 5,245 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 26 ms
18,176 KB
testcase_01 AC 28 ms
18,176 KB
testcase_02 AC 28 ms
18,176 KB
testcase_03 AC 28 ms
17,920 KB
testcase_04 AC 29 ms
18,176 KB
testcase_05 AC 29 ms
17,920 KB
testcase_06 AC 29 ms
18,048 KB
testcase_07 AC 26 ms
18,176 KB
testcase_08 AC 132 ms
31,996 KB
testcase_09 AC 28 ms
18,048 KB
testcase_10 WA -
testcase_11 AC 61 ms
30,968 KB
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 AC 44 ms
24,064 KB
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 -
権限があれば一括ダウンロードができます
コンパイルメッセージ
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;
using System.Diagnostics;
//using nint=System.Int32;
static class Program{
	const int mod=(int)1e9+7;
	static void Main(){
		Sc sc=new Sc();
		var s=sc.Ia;
		var h=new Mint[s[0]];
		var ba=new bool[s[0]];
		for(int i = 0;i<s[0];i++) {h[i]=1;}
		Uf uf=new Uf(s[0]+1);
		Uf2 uf2=new Uf2(s[0]+1);
		var g=Mint.Mi(2);
		for(int i = 0;i<s[1];i++) {
			var e=sc.Ia;
			if(!uf.Same(e[0],e[1])){
				var a=uf.Root(e[0]);
				var b=uf.Root(e[1]);
				if(uf.Size(a)>uf.Size(b)){
					uf2.Scan(uf2.Root(b),uf2.Size(b),x=>{ba[x-1]=true;});
				}
				else if(uf.Size(a)<uf.Size(b)){
					uf2.Scan(uf2.Root(a),uf2.Size(a),x=>{ba[x-1]=true;});
				}
				else{
					uf2.Union(e[0],e[1]);
					h[a-1]*=g;
					h[b-1]*=g;
				}
				uf.Union(a,b);
			}
		}
		for(int i = 0;i<s[0];i++) {
			if(ba[i]){h[i]=0;}
			else {h[i]=h[uf.Root(i+1)-1];}
		}
		Console.WriteLine("{0}",string.Join("\n",h));
	}
}
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 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 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 Uf2{
	public class Nd{
		public int n;
		public Nd ne;
		public Nd(int n){this.n=n;}
	}
	private int[] arr,hi;
	public Nd[] f,l;
	public int all;
	public Uf2(int n){
		arr=new int[n];
		hi=new int[n];
		f=new Nd[n];
		l=new Nd[n];
		all=n;
		for(int i=0;i<n;i++){
			arr[i]=-1;
			f[i]=l[i]=new Nd(i);
		}
	}
	public bool 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--;
			l[a].ne=f[b];
			l[a]=l[b];
			return true;
		}
		return false;
	}
	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 void Scan(int a,int m,Action<int> fu){
		var e=f[a];
		for(int j = 0;j<m;j++) {
			fu(e.n);
			e=e.ne;
		}
	}
}
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 int[] Ia3(int a){return Array.ConvertAll((Console.ReadLine()+" "+a.ToString()).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 long[] La3(int a){return Array.ConvertAll((Console.ReadLine()+" "+a.ToString()).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;}
	public (T,T) Tp2<T>(){var s=Console.ReadLine().Split();return (Ct<T>(s[0]),Ct<T>(s[1]));}
	public (T,T,T) Tp3<T>(){var s=Console.ReadLine().Split();return (Ct<T>(s[0]),Ct<T>(s[1]),Ct<T>(s[2]));}
	public (T1,T1,T2) Tp3<T1,T2>(){var s=Console.ReadLine().Split();return (Ct<T1>(s[0]),Ct<T1>(s[1]),Ct<T2>(s[2]));}
	private T Ct<T>(string s){return (T)Convert.ChangeType(s,typeof(T));}
}
0