結果

問題 No.1479 Matrix Eraser
ユーザー fairy_lettucefairy_lettuce
提出日時 2021-03-05 21:43:14
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 686 ms / 3,000 ms
コード長 14,726 bytes
コンパイル時間 1,451 ms
コンパイル使用メモリ 122,640 KB
実行使用メモリ 77,896 KB
最終ジャッジ日時 2024-10-07 14:49:29
合計ジャッジ時間 17,086 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 46 ms
25,864 KB
testcase_01 AC 48 ms
29,892 KB
testcase_02 AC 46 ms
27,420 KB
testcase_03 AC 47 ms
28,104 KB
testcase_04 AC 46 ms
27,176 KB
testcase_05 AC 46 ms
28,248 KB
testcase_06 AC 46 ms
28,144 KB
testcase_07 AC 116 ms
39,560 KB
testcase_08 AC 176 ms
45,916 KB
testcase_09 AC 319 ms
52,672 KB
testcase_10 AC 568 ms
66,292 KB
testcase_11 AC 365 ms
56,392 KB
testcase_12 AC 142 ms
43,240 KB
testcase_13 AC 171 ms
43,732 KB
testcase_14 AC 146 ms
42,208 KB
testcase_15 AC 72 ms
34,232 KB
testcase_16 AC 158 ms
40,664 KB
testcase_17 AC 672 ms
71,980 KB
testcase_18 AC 673 ms
71,896 KB
testcase_19 AC 679 ms
76,612 KB
testcase_20 AC 686 ms
75,368 KB
testcase_21 AC 676 ms
71,396 KB
testcase_22 AC 662 ms
74,752 KB
testcase_23 AC 660 ms
77,896 KB
testcase_24 AC 656 ms
74,164 KB
testcase_25 AC 647 ms
70,256 KB
testcase_26 AC 652 ms
71,212 KB
testcase_27 AC 390 ms
60,180 KB
testcase_28 AC 389 ms
58,440 KB
testcase_29 AC 396 ms
59,256 KB
testcase_30 AC 388 ms
59,776 KB
testcase_31 AC 397 ms
59,708 KB
testcase_32 AC 245 ms
66,664 KB
testcase_33 AC 251 ms
64,932 KB
testcase_34 AC 246 ms
64,748 KB
testcase_35 AC 241 ms
64,688 KB
testcase_36 AC 247 ms
64,720 KB
testcase_37 AC 126 ms
36,708 KB
testcase_38 AC 572 ms
58,776 KB
testcase_39 AC 681 ms
77,096 KB
testcase_40 AC 44 ms
26,916 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.Diagnostics;
using System.IO;
using System.Linq;
using System.Numerics;
using System.Runtime.CompilerServices;
using System.Threading;
namespace CpLibrary.Contest
{
	public class SolverA : SolverBase
	{
		Scanner sr;
		bool HasMultipleTestcases { get; }

		public override void Solve()
		{
			var (h, w) = sr.ReadValue<int, int>();
			Assert(1 <= h && h <= 500);
			Assert(1 <= w && w <= 500);
			var a = Enumerable.Repeat(0, h).Select(p => sr.ReadLongArray(w)).ToArray();
			var dic = new Dictionary<long, List<(int h, int w)>>();
			for (int i = 0; i < h; i++)
			{
				for (int j = 0; j < w; j++)
				{
					Assert(0 <= a[i][j] && a[i][j] <= 500000);
					if (!dic.ContainsKey(a[i][j])) dic.Add(a[i][j], new List<(int h, int w)>());
					dic[a[i][j]].Add((i, j));
				}
			}
			var ans = 0L;
			foreach (var (num, list) in dic)
			{
				if (num == 0) continue;
				var hv = new Dictionary<int, int>();
				var wv = new Dictionary<int, int>();
				var hmax = 0;
				var wmax = 0;
				foreach (var (i, j) in list)
				{
					if (!hv.ContainsKey(i)) hv.Add(i, hmax++);
					if (!wv.ContainsKey(j)) wv.Add(j, wmax++);
				}
				var mfg = new AtCoder.MFGraphInt(hv.Count + wv.Count + 2);
				foreach (var (i, j) in list)
				{
					mfg.AddEdge(hv[i], wv[j] + hv.Count, 1);
				}
				for (int k = 0; k < hv.Count; k++)
				{
					mfg.AddEdge(hv.Count + wv.Count, k, 1);
				}
				for (int k = 0; k < wv.Count; k++)
				{
					mfg.AddEdge(k + hv.Count, hv.Count + wv.Count + 1, 1);
				}
				var flow = mfg.Flow(hv.Count + wv.Count, hv.Count + wv.Count + 1);
				ans += flow;
			}
			Console.WriteLine(ans);
		}

		public void Assert(bool condition)
		{
			if (!condition) throw new ArgumentException();
		}

		public SolverA(Scanner sr) => this.sr = sr;

		public override void Run()
		{
			var _t = 1;
			if (HasMultipleTestcases) _t = sr.ReadInt();
			while (_t-- > 0) Solve();
		}
	}

	public static class ProgramA
	{
		private static bool StartsOnThread = true;

		public static void Main(string[] args)
		{
			var sw = new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false };
			Console.SetOut(sw);
			var sr = new Scanner(new StreamReader(Console.OpenStandardInput()));
			var solver = new SolverA(sr);
			if (StartsOnThread)
			{
				var thread = new Thread(new ThreadStart(() => solver.Run()), 1 << 27);
				thread.Start();
				thread.Join();
			}
			else solver.Run();
			Console.Out.Flush();
		}

		public static void Expand() => SourceExpander.Expander.Expand();
	}
}
#region Expanded by https://github.com/naminodarie/SourceExpander
namespace AtCoder{public class MFGraph<TValue,TOp>where TOp:struct,INumOperator<TValue>{static readonly TOp op=default;public MFGraph(int n){_n=n;_g=new List<EdgeInternal>[n];for(int i=0;i<n;i++){_g[i]=new List<EdgeInternal>();}_pos=new List<(int first,int second)>();}public int AddEdge(int from,int to,TValue cap){int m=_pos.Count;_pos.Add((from,_g[from].Count));int fromId=_g[from].Count;int toId=_g[to].Count;if(from==to)toId++;_g[from].Add(new EdgeInternal(to,toId,cap));_g[to].Add(new EdgeInternal(from,fromId,default));return m;}public Edge GetEdge(int i){int m=_pos.Count;var _e=_g[_pos[i].first][_pos[i].second];var _re=_g[_e.To][_e.Rev];return new Edge(_pos[i].first,_e.To,op.Add(_e.Cap,_re.Cap),_re.Cap);}public List<Edge>Edges(){int m=_pos.Count;var result=new List<Edge>();for(int i=0;i<m;i++){result.Add(GetEdge(i));}return result;}public void ChangeEdge(int i,TValue newCap,TValue newFlow){int m=_pos.Count;var _e=_g[_pos[i].first][_pos[i].second];var _re=_g[_e.To][_e.Rev];_e.Cap=op.Subtract(newCap,newFlow);_re.Cap=newFlow;}public TValue Flow(int s,int t){return Flow(s,t,op.MaxValue);}public TValue Flow(int s,int t,TValue flowLimit){var level=new int[_n];var iter=new int[_n];var que=new Queue<int>();void Bfs(){for(int i=0;i<_n;i++){level[i]=-1;}level[s]=0;que.Clear();que.Enqueue(s);while(que.Count>0){int v=que.Dequeue();foreach(var e in _g[v]){if(EqualityComparer<TValue>.Default.Equals(e.Cap,default)||level[e.To]>=0)continue;level[e.To]=level[v]+1;if(e.To==t)return;que.Enqueue(e.To);}}}TValue Dfs(int v,TValue up){if(v==s)return up;var res=default(TValue);int level_v=level[v];for(;iter[v]<_g[v].Count;iter[v]++){EdgeInternal e=_g[v][iter[v]];if(level_v<=level[e.To]||EqualityComparer<TValue>.Default.Equals(_g[e.To][e.Rev].Cap,default))continue;var up1=op.Subtract(up,res);var up2=_g[e.To][e.Rev].Cap;var d=Dfs(e.To,op.LessThan(up1,up2)?up1:up2);if(op.LessThanOrEqual(d,default))continue;_g[v][iter[v]].Cap=op.Add(_g[v][iter[v]].Cap,d);_g[e.To][e.Rev].Cap=op.Subtract(_g[e.To][e.Rev].Cap,d);res=op.Add(res,d);if(res.Equals(up))return res;}level[v]=_n;return res;}TValue flow=default;while(op.LessThan(flow,flowLimit)){Bfs();if(level[t]==-1)break;for(int i=0;i<_n;i++){iter[i]=0;}var f=Dfs(t,op.Subtract(flowLimit,flow));if(EqualityComparer<TValue>.Default.Equals(f,default))break;flow=op.Add(flow,f);}return flow;}public bool[]MinCut(int s){var visited=new bool[_n];var que=new Queue<int>();que.Enqueue(s);while(que.Count>0){int p=que.Dequeue();visited[p]=true;foreach(var e in _g[p]){if(!EqualityComparer<TValue>.Default.Equals(e.Cap,default)&&!visited[e.To]){visited[e.To]=true;que.Enqueue(e.To);}}}return visited;}public struct Edge:IEquatable<Edge>{public int From{get;set;}public int To{get;set;}public TValue Cap{get;set;}public TValue Flow{get;set;}public Edge(int from,int to,TValue cap,TValue flow){From=from;To=to;Cap=cap;Flow=flow;}public override bool Equals(object obj)=>obj is Edge edge&&Equals(edge);public bool Equals(Edge other)=>From==other.From&&To==other.To&&EqualityComparer<TValue>.Default.Equals(Cap,other.Cap)&&EqualityComparer<TValue>.Default.Equals(Flow,other.Flow);public override int GetHashCode()=>HashCode.Combine(From,To,Cap,Flow);public static bool operator==(Edge left,Edge right)=>left.Equals(right);public static bool operator!=(Edge left,Edge right)=>!left.Equals(right);};internal class EdgeInternal{public int To{get;set;}public int Rev{get;set;}public TValue Cap{get;set;}public EdgeInternal(int to,int rev,TValue cap){To=to;Rev=rev;Cap=cap;}};internal readonly int _n;internal readonly List<(int first,int second)>_pos;internal readonly List<EdgeInternal>[]_g;}}
namespace AtCoder{public class MFGraphInt:MFGraph<int,IntOperator>{public MFGraphInt(int n):base(n){}}public class MFGraphLong:MFGraph<long,LongOperator>{public MFGraphLong(int n):base(n){}}}
namespace CpLibrary { public class Scanner { public StreamReader sr { get; private set; }  string[] str; int index; char[] separators; public Scanner(StreamReader sr, char[] separators) { this.sr = sr; this.separators = separators; str = new string[0]; index = 0; }  public Scanner(StreamReader sr): this(sr, new char[]{' '}) { }  public Scanner(): this(new StreamReader(Console.OpenStandardInput()), new char[]{' '}) { }  public string Read() { if (index < str.Length) return str[index++]; string s; do s = sr.ReadLine(); while (s == ""); str = s.Split(separators, StringSplitOptions.RemoveEmptyEntries); index = 0; return str[index++]; }  public string ReadString() => Read(); public string[] ReadStringArray(int n) { var arr = new string[n]; for (int i = 0; i < n; i++) { arr[i] = ReadString(); }  return arr; }  public int ReadInt() => int.Parse(ReadString()); public int[] ReadIntArray(int n) => ReadValueArray<int>(n); public long ReadLong() => long.Parse(ReadString()); public long[] ReadLongArray(int n) => ReadValueArray<long>(n); public double ReadDouble() => double.Parse(ReadString()); public double[] ReadDoubleArray(int n) => ReadValueArray<double>(n); public BigInteger ReadBigInteger() => BigInteger.Parse(ReadString()); public T1 ReadValue<T1>() => (T1)Convert.ChangeType(ReadString(), typeof(T1)); public T1[] ReadValueArray<T1>(int n) { var arr = new T1[n]; for (int i = 0; i < n; i++) { arr[i] = ReadValue<T1>(); }  return arr; }  public (T1, T2) ReadValue<T1, T2>() => (ReadValue<T1>(), ReadValue<T2>()); public (T1, T2, T3) ReadValue<T1, T2, T3>() => (ReadValue<T1>(), ReadValue<T2>(), ReadValue<T3>()); public (T1, T2, T3, T4) ReadValue<T1, T2, T3, T4>() => (ReadValue<T1>(), ReadValue<T2>(), ReadValue<T3>(), ReadValue<T4>()); public (T1, T2, T3, T4, T5) ReadValue<T1, T2, T3, T4, T5>() => (ReadValue<T1>(), ReadValue<T2>(), ReadValue<T3>(), ReadValue<T4>(), ReadValue<T5>()); public (T1, T2, T3, T4, T5, T6) ReadValue<T1, T2, T3, T4, T5, T6>() => (ReadValue<T1>(), ReadValue<T2>(), ReadValue<T3>(), ReadValue<T4>(), ReadValue<T5>(), ReadValue<T6>()); public (T1, T2, T3, T4, T5, T6, T7) ReadValue<T1, T2, T3, T4, T5, T6, T7>() => (ReadValue<T1>(), ReadValue<T2>(), ReadValue<T3>(), ReadValue<T4>(), ReadValue<T5>(), ReadValue<T6>(), ReadValue<T7>()); public (T1[], T2[]) ReadValueArray<T1, T2>(int n) { var(v1, v2) = (new T1[n], new T2[n]); for (int i = 0; i < n; i++) { (v1[i], v2[i]) = ReadValue<T1, T2>(); }  return (v1, v2); }  public (T1[], T2[], T3[]) ReadValueArray<T1, T2, T3>(int n) { var(v1, v2, v3) = (new T1[n], new T2[n], new T3[n]); for (int i = 0; i < n; i++) { (v1[i], v2[i], v3[i]) = ReadValue<T1, T2, T3>(); }  return (v1, v2, v3); }  public (T1[], T2[], T3[], T4[]) ReadValueArray<T1, T2, T3, T4>(int n) { var(v1, v2, v3, v4) = (new T1[n], new T2[n], new T3[n], new T4[n]); for (int i = 0; i < n; i++) { (v1[i], v2[i], v3[i], v4[i]) = ReadValue<T1, T2, T3, T4>(); }  return (v1, v2, v3, v4); }  public (T1[], T2[], T3[], T4[], T5[]) ReadValueArray<T1, T2, T3, T4, T5>(int n) { var(v1, v2, v3, v4, v5) = (new T1[n], new T2[n], new T3[n], new T4[n], new T5[n]); for (int i = 0; i < n; i++) { (v1[i], v2[i], v3[i], v4[i], v5[i]) = ReadValue<T1, T2, T3, T4, T5>(); }  return (v1, v2, v3, v4, v5); }  public (T1[], T2[], T3[], T4[], T5[], T6[]) ReadValueArray<T1, T2, T3, T4, T5, T6>(int n) { var(v1, v2, v3, v4, v5, v6) = (new T1[n], new T2[n], new T3[n], new T4[n], new T5[n], new T6[n]); for (int i = 0; i < n; i++) { (v1[i], v2[i], v3[i], v4[i], v5[i], v6[i]) = ReadValue<T1, T2, T3, T4, T5, T6>(); }  return (v1, v2, v3, v4, v5, v6); }  public (T1[], T2[], T3[], T4[], T5[], T6[], T7[]) ReadValueArray<T1, T2, T3, T4, T5, T6, T7>(int n) { var(v1, v2, v3, v4, v5, v6, v7) = (new T1[n], new T2[n], new T3[n], new T4[n], new T5[n], new T6[n], new T7[n]); for (int i = 0; i < n; i++) { (v1[i], v2[i], v3[i], v4[i], v5[i], v6[i], v7[i]) = ReadValue<T1, T2, T3, T4, T5, T6, T7>(); }  return (v1, v2, v3, v4, v5, v6, v7); } } }
namespace CpLibrary { public interface ISolver { public void Solve(); public void Run(); }  public abstract class SolverBase : ISolver { public abstract void Solve(); public abstract void Run(); public bool YesNo(bool condition) { Console.WriteLine(condition ? "Yes" : "No"); return condition; }  public bool YESNO(bool condition) { Console.WriteLine(condition ? "YES" : "NO"); return condition; }  public bool yesno(bool condition) { Console.WriteLine(condition ? "yes" : "no"); return condition; } } }
namespace SourceExpander{public class Expander{[Conditional("EXP")]public static void Expand(string inputFilePath=null,string outputFilePath=null,bool ignoreAnyError=true){}public static string ExpandString(string inputFilePath=null,bool ignoreAnyError=true){return "";}}}
namespace AtCoder{public interface IAdditionOperator<T>{T Add(T x,T y);T Subtract(T x,T y);}public interface IMultiplicationOperator<T>{T Multiply(T x,T y);T MultiplyIdentity{get;}}public interface IDivisionOperator<T>:IMultiplicationOperator<T>{T Divide(T x,T y);T Modulo(T x,T y);}public interface IUnaryNumOperator<T>{T Minus(T x);T Increment(T x);T Decrement(T x);}public interface IArithmeticOperator<T>:IAdditionOperator<T>,IMultiplicationOperator<T>,IDivisionOperator<T>,IUnaryNumOperator<T>{}public interface ICompareOperator<T>:IComparer<T>{bool GreaterThan(T x,T y);bool GreaterThanOrEqual(T x,T y);bool LessThan(T x,T y);bool LessThanOrEqual(T x,T y);}public interface INumOperator<T>:IArithmeticOperator<T>,ICompareOperator<T>{T MinValue{get;}T MaxValue{get;}}public interface IShiftOperator<T>{T LeftShift(T x,int y);T RightShift(T x,int y);}}
namespace AtCoder{using static MethodImplOptions;public readonly struct IntOperator:INumOperator<int>,IShiftOperator<int>{public int MinValue=>int.MinValue;public int MaxValue=>int.MaxValue;public int MultiplyIdentity=>1;[MethodImpl(AggressiveInlining)]public int Add(int x,int y)=>x+y;[MethodImpl(AggressiveInlining)]public int Subtract(int x,int y)=>x-y;[MethodImpl(AggressiveInlining)]public int Multiply(int x,int y)=>x*y;[MethodImpl(AggressiveInlining)]public int Divide(int x,int y)=>x/y;[MethodImpl(AggressiveInlining)]public int Modulo(int x,int y)=>x%y;[MethodImpl(AggressiveInlining)]public int Minus(int x)=>-x;[MethodImpl(AggressiveInlining)]public int Increment(int x)=> ++x;[MethodImpl(AggressiveInlining)]public int Decrement(int x)=> --x;[MethodImpl(AggressiveInlining)]public bool GreaterThan(int x,int y)=>x>y;[MethodImpl(AggressiveInlining)]public bool GreaterThanOrEqual(int x,int y)=>x>=y;[MethodImpl(AggressiveInlining)]public bool LessThan(int x,int y)=>x<y;[MethodImpl(AggressiveInlining)]public bool LessThanOrEqual(int x,int y)=>x<=y;[MethodImpl(AggressiveInlining)]public int Compare(int x,int y)=>x.CompareTo(y);[MethodImpl(AggressiveInlining)]public int LeftShift(int x,int y)=>x<<y;[MethodImpl(AggressiveInlining)]public int RightShift(int x,int y)=>x>>y;}}
namespace AtCoder{using static MethodImplOptions;public readonly struct LongOperator:INumOperator<long>,IShiftOperator<long>{public long MinValue=>long.MinValue;public long MaxValue=>long.MaxValue;public long MultiplyIdentity=>1L;[MethodImpl(AggressiveInlining)]public long Add(long x,long y)=>x+y;[MethodImpl(AggressiveInlining)]public long Subtract(long x,long y)=>x-y;[MethodImpl(AggressiveInlining)]public long Multiply(long x,long y)=>x*y;[MethodImpl(AggressiveInlining)]public long Divide(long x,long y)=>x/y;[MethodImpl(AggressiveInlining)]public long Modulo(long x,long y)=>x%y;[MethodImpl(AggressiveInlining)]public long Minus(long x)=>-x;[MethodImpl(AggressiveInlining)]public long Increment(long x)=> ++x;[MethodImpl(AggressiveInlining)]public long Decrement(long x)=> --x;[MethodImpl(AggressiveInlining)]public bool GreaterThan(long x,long y)=>x>y;[MethodImpl(AggressiveInlining)]public bool GreaterThanOrEqual(long x,long y)=>x>=y;[MethodImpl(AggressiveInlining)]public bool LessThan(long x,long y)=>x<y;[MethodImpl(AggressiveInlining)]public bool LessThanOrEqual(long x,long y)=>x<=y;[MethodImpl(AggressiveInlining)]public int Compare(long x,long y)=>x.CompareTo(y);[MethodImpl(AggressiveInlining)]public long LeftShift(long x,int y)=>x<<y;[MethodImpl(AggressiveInlining)]public long RightShift(long x,int y)=>x>>y;}}
#endregion Expanded by https://github.com/naminodarie/SourceExpander
0