結果
| 問題 |
No.1123 Afforestation
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2020-07-28 19:35:55 |
| 言語 | Java (openjdk 23) |
| 結果 |
TLE
|
| 実行時間 | - |
| コード長 | 9,322 bytes |
| コンパイル時間 | 5,205 ms |
| コンパイル使用メモリ | 91,784 KB |
| 実行使用メモリ | 712,576 KB |
| 最終ジャッジ日時 | 2024-06-28 20:54:02 |
| 合計ジャッジ時間 | 11,101 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | -- * 3 |
| other | AC * 5 TLE * 1 MLE * 1 -- * 83 |
ソースコード
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;
}
nUpdate++;
add(activeFirst, activeNext, highest, cur);
}
// if(nUpdate > 8*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)); }
}