結果

問題 No.1123 Afforestation
ユーザー uwiuwi
提出日時 2020-07-28 19:39:18
言語 Java21
(openjdk 21)
結果
TLE  
実行時間 -
コード長 9,322 bytes
コンパイル時間 4,165 ms
コンパイル使用メモリ 92,448 KB
実行使用メモリ 196,708 KB
最終ジャッジ日時 2024-06-28 20:54:12
合計ジャッジ時間 9,791 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 53 ms
57,196 KB
testcase_01 AC 54 ms
49,756 KB
testcase_02 AC 55 ms
50,244 KB
testcase_03 AC 107 ms
52,280 KB
testcase_04 AC 519 ms
69,176 KB
testcase_05 TLE -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
testcase_41 -- -
testcase_42 -- -
testcase_43 -- -
testcase_44 -- -
testcase_45 -- -
testcase_46 -- -
testcase_47 -- -
testcase_48 -- -
testcase_49 -- -
testcase_50 -- -
testcase_51 -- -
testcase_52 -- -
testcase_53 -- -
testcase_54 -- -
testcase_55 -- -
testcase_56 -- -
testcase_57 -- -
testcase_58 -- -
testcase_59 -- -
testcase_60 -- -
testcase_61 -- -
testcase_62 -- -
testcase_63 -- -
testcase_64 -- -
testcase_65 -- -
testcase_66 -- -
testcase_67 -- -
testcase_68 -- -
testcase_69 -- -
testcase_70 -- -
testcase_71 -- -
testcase_72 -- -
testcase_73 -- -
testcase_74 -- -
testcase_75 -- -
testcase_76 -- -
testcase_77 -- -
testcase_78 -- -
testcase_79 -- -
testcase_80 -- -
testcase_81 -- -
testcase_82 -- -
testcase_83 -- -
testcase_84 -- -
testcase_85 -- -
testcase_86 -- -
testcase_87 -- -
testcase_88 -- -
testcase_89 -- -
testcase_90 -- -
testcase_91 -- -
testcase_92 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

package contest200722;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.InputMismatchException;
import java.util.List;

public class E6 {
	InputStream is;
	PrintWriter out;
	String INPUT = "";
	
	void solve()
	{
		int n = ni(), m = ni();
		int[] a = na(n);
		int[] b = na(m);
		int K = ni();
		boolean[][] house = new boolean[n][m];
		for(int i = 0;i < K;i++){
			int r = ni()-1, c = ni()-1;
			house[r][c] = true;
		}
		List<Edge> es = new ArrayList<>();
		int src = n+m, sink = src+1;
		for(int i = 0;i < n;i++){
			for(int j = 0;j < m;j++){
				if(house[i][j])continue;
				es.add(new Edge(i, j+n, 1));
			}
			es.add(new Edge(src, i, a[i]));
		}
		for(int j = 0;j < m;j++){
			es.add(new Edge(j+n, sink, b[j]));
		}
		long F = hlpp(compileWD(sink+1, es), src, sink);
		long sa = 0, sb = 0;
		for(int v : a)sa += v;
		for(int v : b)sb += v;
		if(F != sa || F != sb){
			out.println(":(");
			return;
		}
		
		out.println("Yay!");
		char[][] ret = new char[n][m];
		for(int i = 0;i < n;i++)Arrays.fill(ret[i], '.');
		for(int i = 0;i < n;i++){
			for(int j = 0;j < m;j++){
				if(house[i][j]){
					ret[i][j] = 'x';
				}
			}
		}
		for(Edge e : es){
			if(!e.cloned && e.from < n && e.flow == 1){
				ret[e.from][e.to-n] = 'o';
			}
		}
		for(char[] line : ret){
			out.println(new String(line));
		}
	}
	
	public static class Edge
	{
		public int from, to;
		public long capacity;
		public long flow;
		public Edge complement;
		public boolean cloned;
		
		public Edge(int from, int to, long capacity) {
			this.from = from;
			this.to = to;
			this.capacity = capacity;
		}
	}
	
	public static void reset(Edge[][] g)
	{
		for(Edge[] row : g){
			for(Edge e : row){
				e.flow = e.cloned ? e.capacity : 0;
			}
		}
	}
	
	public static Edge[][] compileWD(int n, List<Edge> edges)
	{
		Edge[][] g = new Edge[n][];
		// cloning
		for(int i = edges.size()-1;i >= 0;i--){
			Edge origin = edges.get(i);
			Edge clone = new Edge(origin.to, origin.from, origin.capacity);
			clone.flow = origin.capacity;
			clone.complement = origin;
			clone.cloned = true;
			origin.complement = clone;
			edges.add(clone);
		}
		
		int[] p = new int[n];
		for(Edge e : edges)p[e.from]++;
		for(int i = 0;i < n;i++)g[i] = new Edge[p[i]];
		for(Edge e : edges)g[e.from][--p[e.from]] = e;
		return g;
	}
	
	public static Edge[][] compileWU(int n, List<Edge> edges)
	{
		Edge[][] g = new Edge[n][];
		// cloning
		for(int i = edges.size()-1;i >= 0;i--){
			Edge origin = edges.get(i);
			Edge clone = new Edge(origin.to, origin.from, origin.capacity*2);
			origin.flow = origin.capacity;
			clone.flow = origin.capacity;
			clone.complement = origin;
			clone.cloned = true;
			origin.complement = clone;
			origin.capacity *= 2;
			edges.add(clone);
		}
		
		int[] p = new int[n];
		for(Edge e : edges)p[e.from]++;
		for(int i = 0;i < n;i++)g[i] = new Edge[p[i]];
		for(Edge e : edges)g[e.from][--p[e.from]] = e;
		return g;
	}	
	
	static void add(int[] first, int[] next, int level, int x)
	{
		next[x] = first[level];
		first[level] = x;
	}
	
	static int poll(int[] first, int[] next, int level)
	{
		int ret = first[level];
		if(ret != -1){
			first[level] = next[ret];
			next[ret] = -1;
		}
		return ret;
	}

	static void add(int[] first, int[] next, int[] prev, int level, int x)
	{
		next[x] = first[level];
		if(next[x] != -1)prev[next[x]] = x;
		first[level] = x;
	}
	
	static void remove(int[] first, int[] next, int[] prev, int level, int x)
	{
		int p = prev[x];
		int n = next[x];
		if(p == -1)first[level] = n;
		if(p != -1)next[p] = n;
		if(n != -1)prev[n] = p;
		prev[x] = next[x] = -1;
	}
	
	static int[] labelGrobally(Edge[][] g, int sink, int[] label)
	{
		int n = g.length;
		Arrays.fill(label, n);
		label[sink] = 0;
		int[] q = new int[n];
		int p = 0;
		q[p++] = sink;
		for(int z = 0;z < p;z++){
			int cur = q[z];
			for(Edge e : g[cur]){
				if(e.flow == e.capacity && label[e.to] > label[e.from] + 1){
					label[e.to] = label[e.from] + 1;
					q[p++] = e.to;
				}
			}
		}
		return label;
	}
	
	static long hlpp(Edge[][] g, int source, int sink)
	{
		int n = g.length;
		int[] label = new int[n];
		long[] excess = new long[n];
		label[source] = n;
		
		// flow (src, *)
		for(Edge se : g[source]){
			long f = se.capacity - se.flow;
			se.flow += f;
			se.complement.flow -= f;
			excess[se.to] += f;
		}
		
		labelGrobally(g, sink, label);
		
		int[] activeFirst = new int[2*n+1];
		Arrays.fill(activeFirst, -1);
		
		int[] activeNext = new int[n];
		Arrays.fill(activeNext, -1);
		
		int[] gapFirst = new int[2*n+1];
		Arrays.fill(gapFirst, -1);
		
		int[] gapNext = new int[n];
		Arrays.fill(gapNext, -1);
		
		int[] gapPrev = new int[n];
		Arrays.fill(gapPrev, -1);
		
		int highest = 0;
		for(int i = 0;i < n;i++){
			if(i != source && i != sink){
				if(excess[i] > 0){
					highest = Math.max(highest, label[i]);
					add(activeFirst, activeNext, label[i], i);
				}
				add(gapFirst, gapNext, gapPrev, label[i], i);
			}
		}
		
		int nUpdate = 0;
		while(highest >= 0){
			while(activeFirst[highest] != -1){
				int cur = poll(activeFirst, activeNext, highest);
				assert cur != source;
				for(Edge e : g[cur]){
					if(excess[cur] == 0)break;
					if(label[e.from] == label[e.to] + 1 && e.flow < e.capacity){
						if(e.to != source && e.to != sink && excess[e.to] == 0)add(activeFirst, activeNext, label[e.to], e.to);
						push(e, excess);
					}
				}
				if(excess[cur] > 0){
					int oldh = label[cur];
					remove(gapFirst, gapNext, gapPrev, label[cur], cur);
					
					relabel(cur, g, label);
					add(gapFirst, gapNext, gapPrev, label[cur], cur);
					highest = label[cur];
					
					if(gapFirst[oldh] == -1){
						for(int h = oldh+1;h <= highest;h++){
							for(int y = gapFirst[h];y != -1;y = gapNext[y]){
								label[y] = n;
							}
							gapFirst[h] = -1;
						}
						highest = oldh-1;
					}else{
						nUpdate++;
						add(activeFirst, activeNext, highest, cur);
					}
				}
				if(nUpdate > 4*n){
					nUpdate = 0;
					labelGrobally(g, sink, label);
				}
			}
			highest--;
		}
		return excess[sink];
	}
	
	static void push(Edge e, long[] excess)
	{
		long f = Math.min(excess[e.from], e.capacity - e.flow);
		assert f > 0;
		e.flow += f;
		e.complement.flow -= f;
		excess[e.from] -= f;
		excess[e.to] += f;
	}
	
	static void relabel(int cur, Edge[][] g, int[] label)
	{
		int min = Integer.MAX_VALUE;
		for(Edge e : g[cur]){
			if(e.flow < e.capacity)min = Math.min(min, label[e.to]);
		}
		label[cur] = min + 1;
	}

	
	void run() throws Exception
	{
		is = INPUT.isEmpty() ? System.in : new ByteArrayInputStream(INPUT.getBytes());
		out = new PrintWriter(System.out);
		
		long s = System.currentTimeMillis();
		solve();
		out.flush();
		if(!INPUT.isEmpty())tr(System.currentTimeMillis()-s+"ms");
//		Thread t = new Thread(null, null, "~", Runtime.getRuntime().maxMemory()){
//			@Override
//			public void run() {
//				long s = System.currentTimeMillis();
//				solve();
//				out.flush();
//				if(!INPUT.isEmpty())tr(System.currentTimeMillis()-s+"ms");
//			}
//		};
//		t.start();
//		t.join();
	}
	
	public static void main(String[] args) throws Exception { new E6().run(); }
	
	private byte[] inbuf = new byte[1024];
	public int lenbuf = 0, ptrbuf = 0;
	
	private int readByte()
	{
		if(lenbuf == -1)throw new InputMismatchException();
		if(ptrbuf >= lenbuf){
			ptrbuf = 0;
			try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); }
			if(lenbuf <= 0)return -1;
		}
		return inbuf[ptrbuf++];
	}
	
	private boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); }
	private int skip() { int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; }
	
	private double nd() { return Double.parseDouble(ns()); }
	private char nc() { return (char)skip(); }
	
	private String ns()
	{
		int b = skip();
		StringBuilder sb = new StringBuilder();
		while(!(isSpaceChar(b))){ // when nextLine, (isSpaceChar(b) && b != ' ')
			sb.appendCodePoint(b);
			b = readByte();
		}
		return sb.toString();
	}
	
	private char[] ns(int n)
	{
		char[] buf = new char[n];
		int b = skip(), p = 0;
		while(p < n && !(isSpaceChar(b))){
			buf[p++] = (char)b;
			b = readByte();
		}
		return n == p ? buf : Arrays.copyOf(buf, p);
	}
	
	private int[] na(int n)
	{
		int[] a = new int[n];
		for(int i = 0;i < n;i++)a[i] = ni();
		return a;
	}
	
	private long[] nal(int n)
	{
		long[] a = new long[n];
		for(int i = 0;i < n;i++)a[i] = nl();
		return a;
	}
	
	private char[][] nm(int n, int m) {
		char[][] map = new char[n][];
		for(int i = 0;i < n;i++)map[i] = ns(m);
		return map;
	}
	
	private int[][] nmi(int n, int m) {
		int[][] map = new int[n][];
		for(int i = 0;i < n;i++)map[i] = na(m);
		return map;
	}
	
	private int ni() { return (int)nl(); }
	
	private long nl()
	{
		long num = 0;
		int b;
		boolean minus = false;
		while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'));
		if(b == '-'){
			minus = true;
			b = readByte();
		}
		
		while(true){
			if(b >= '0' && b <= '9'){
				num = num * 10 + (b - '0');
			}else{
				return minus ? -num : num;
			}
			b = readByte();
		}
	}
	
	private static void tr(Object... o) { System.out.println(Arrays.deepToString(o)); }
}
0