Submission #903740
Source Code Expand
import java.io.IOException;
import java.io.InputStream;
import java.util.*;
public class Main {
private static final long INF = Integer.MAX_VALUE;
private static final int R = 0;
private static final int C = 1;
private Graph g = new Graph();
private Map<Integer, Long> nodeCost = new HashMap<>();
private boolean ans = true;
private void solve() {
FastScanner sc = new FastScanner();
int rMax = sc.nextInt();
int cMax = sc.nextInt();
int n = sc.nextInt();
for (int i = 0; i < n; i++) {
g.addEdgeUndirected(R, sc.nextInt(), C, sc.nextInt(), sc.nextInt());
}
for (Map.Entry<Long, Integer> e : g.getIdMap().entrySet()) {
int i = e.getValue();
if (nodeCost.containsKey(i)) continue;
int rc = Graph.getFst(e.getKey());
Pair p = dfs(i, 0, rc);
ans &= p.fst + p.snd >= 0;
}
System.out.println(ans ? "Yes" : "No");
}
private Pair dfs(int i, long c, int rc) {
nodeCost.put(i, c);
Pair p = new Pair(rc == R ? c : INF, rc == C ? c : INF);
for (long next : g.getNext(i)) {
int j = Graph.getFst(next);
long a = Graph.getSnd(next);
if (nodeCost.containsKey(j)) {
long d = nodeCost.get(j);
ans &= d + c == a;
} else {
Pair q = dfs(j, a - c, 1 - rc);
p.fst = Math.min(p.fst, q.fst);
p.snd = Math.min(p.snd, q.snd);
}
}
ans &= true;
return p;
}
public static void main(String[] args) {
new Main().solve();
}
}
class FastScanner {
private final InputStream in = System.in;
private final byte[] buffer = new byte[1024];
private int ptr = 0;
private int buflen = 0;
private boolean hasNextByte() {
if (ptr < buflen) {
return true;
} else {
ptr = 0;
try {
buflen = in.read(buffer);
} catch (IOException e) {
e.printStackTrace();
}
if (buflen <= 0) {
return false;
}
}
return true;
}
private int readByte() {
if (hasNextByte()) return buffer[ptr++];
else return -1;
}
private static boolean isPrintableChar(int c) {
return 33 <= c && c <= 126;
}
public boolean hasNext() {
while (hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++;
return hasNextByte();
}
public String next() {
if (!hasNext()) throw new NoSuchElementException();
StringBuilder sb = new StringBuilder();
int b = readByte();
while (isPrintableChar(b)) {
sb.appendCodePoint(b);
b = readByte();
}
return sb.toString();
}
public long nextLong() {
if (!hasNext()) throw new NoSuchElementException();
long n = 0;
boolean minus = false;
int b = readByte();
if (b == '-') {
minus = true;
b = readByte();
}
if (b < '0' || '9' < b) {
throw new NumberFormatException();
}
while (true) {
if ('0' <= b && b <= '9') {
n *= 10;
n += b - '0';
} else if (b == -1 || !isPrintableChar(b)) {
return minus ? -n : n;
} else {
throw new NumberFormatException();
}
b = readByte();
}
}
public int nextInt() {
long nl = nextLong();
if (nl < Integer.MIN_VALUE || nl > Integer.MAX_VALUE) throw new NumberFormatException();
return (int) nl;
}
public double nextDouble() {
return Double.parseDouble(next());
}
}
class Pair {
long fst;
long snd;
Pair(long fst, long snd) {
this.fst = fst;
this.snd = snd;
}
}
class Graph {
private Map<Long, Integer> idMap;
private List<List<Long>> assoc;
public static class Builder {
List<List<Long>> assoc;
List<List<Integer>> edges;
int v;
boolean undirected = false;
public Builder(int v, List<List<Integer>> edges) {
this.v = v;
this.edges = edges;
}
public Builder toUndirected() {
undirected = true;
return this;
}
public Graph build() {
assoc = new ArrayList<>();
for (int i = 0; i < v; i++) assoc.add(new ArrayList<>());
for (List<Integer> edge : edges) {
assoc.get(edge.get(0)).add(iToL(edge.get(1), edge.get(2)));
if (undirected) assoc.get(edge.get(1)).add(iToL(edge.get(0), edge.get(2)));
}
return new Graph(assoc, new HashMap<>());
}
}
public Graph() {
idMap = new HashMap<>();
assoc = new ArrayList<>();
}
public Graph(List<List<Long>> assoc, Map<Long, Integer> idMap) {
this.assoc = assoc;
this.idMap = idMap;
}
public List<Long> getNext(int s) {
return assoc.get(s);
}
public void addEdge(int s, int t, int w) {
assoc.get(s).add(iToL(t, w));
}
public void addEdgeUndirected(int s, int t, int w) {
assoc.get(s).add(iToL(t, w));
assoc.get(t).add(iToL(s, w));
}
public void addEdge(int s1, int s2, int t1, int t2, int w) {
assoc.get(toId(s1, s2)).add(iToL(toId(t1, t2), w));
}
public void addEdgeUndirected(int s1, int s2, int t1, int t2, int w) {
int s = toId(s1, s2);
int t = toId(t1, t2);
addEdgeUndirected(s, t, w);
}
private int toId(int fst, int snd) {
long lid = iToL(fst, snd);
Integer id = idMap.get(lid);
if (id == null) {
idMap.put(lid, id = idMap.size());
assoc.add(new ArrayList<>());
}
return id;
}
public static long iToL(int fst, int snd) {
return ((long) (fst) << 32) | (long) snd & 0xffffffffL;
}
public static int getElem(long edge) {
return (int) (edge >> 32);
}
public static int getCost(long edge) {
return (int) (edge & 0xffffffffL);
}
public static int getFst(long intPair) {
return (int) (intPair >> 32);
}
public static int getSnd(long intPair) {
return (int) (intPair & 0xffffffffL);
}
public Map<Long, Integer> getIdMap() {
return idMap;
}
public List<List<Long>> getAssoc() {
return assoc;
}
public String toString() {
StringBuilder sb = new StringBuilder("assoc: [\n");
for (int i = 0; i < assoc.size(); i++) {
sb.append(" ").append(i).append(": [ ");
for (long e : assoc.get(i)) {
sb.append("(").append(getElem(e)).append(", ").append(getCost(e)).append(") ");
}
sb.append("]\n");
}
sb.append("]");
return sb.toString();
}
}
Submission Info
| Submission Time | |
|---|---|
| Task | D - Grid and Integers |
| User | nes_in_it |
| Language | Java8 (OpenJDK 1.8.0) |
| Score | 800 |
| Code Size | 7362 Byte |
| Status | AC |
| Exec Time | 879 ms |
| Memory | 92960 KiB |
Judge Result
| Set Name | Sample | All | ||||
|---|---|---|---|---|---|---|
| Score / Max Score | 0 / 0 | 800 / 800 | ||||
| Status |
|
|
| Set Name | Test Cases |
|---|---|
| Sample | 0_00.txt, 0_01.txt, 0_02.txt, 0_03.txt, 0_04.txt |
| All | 0_00.txt, 0_01.txt, 0_02.txt, 0_03.txt, 0_04.txt, 1_00.txt, 1_01.txt, 1_02.txt, 1_03.txt, 1_04.txt, 1_05.txt, 1_06.txt, 1_07.txt, 1_08.txt, 1_09.txt, 1_10.txt, 1_11.txt, 1_12.txt, 1_13.txt, 1_14.txt, 1_15.txt, 1_16.txt, 1_17.txt, 1_18.txt, 1_19.txt, 1_20.txt, 1_21.txt, 1_22.txt, 1_23.txt, 1_24.txt, 1_25.txt, 1_26.txt, 1_27.txt, 1_28.txt, 1_29.txt, 1_30.txt, 1_31.txt, 1_32.txt, 1_33.txt, 1_34.txt, 1_35.txt, 1_36.txt, 1_37.txt, 1_38.txt, 1_39.txt, 1_40.txt, 1_41.txt, 1_42.txt, 1_43.txt, 1_44.txt, 1_45.txt, 1_46.txt, 1_47.txt, 1_48.txt, 1_49.txt, 1_50.txt, 1_51.txt, 1_52.txt, 1_53.txt, 1_54.txt, 1_55.txt, 1_56.txt, 1_57.txt, 1_58.txt, 1_59.txt, 1_60.txt, 1_61.txt, 1_62.txt, 1_63.txt |
| Case Name | Status | Exec Time | Memory |
|---|---|---|---|
| 0_00.txt | AC | 98 ms | 8396 KiB |
| 0_01.txt | AC | 96 ms | 8400 KiB |
| 0_02.txt | AC | 112 ms | 8276 KiB |
| 0_03.txt | AC | 98 ms | 8404 KiB |
| 0_04.txt | AC | 98 ms | 8268 KiB |
| 1_00.txt | AC | 97 ms | 8400 KiB |
| 1_01.txt | AC | 99 ms | 8276 KiB |
| 1_02.txt | AC | 100 ms | 8396 KiB |
| 1_03.txt | AC | 98 ms | 8268 KiB |
| 1_04.txt | AC | 96 ms | 8400 KiB |
| 1_05.txt | AC | 96 ms | 8276 KiB |
| 1_06.txt | AC | 879 ms | 91152 KiB |
| 1_07.txt | AC | 842 ms | 92960 KiB |
| 1_08.txt | AC | 489 ms | 63836 KiB |
| 1_09.txt | AC | 459 ms | 62788 KiB |
| 1_10.txt | AC | 425 ms | 53680 KiB |
| 1_11.txt | AC | 446 ms | 53260 KiB |
| 1_12.txt | AC | 506 ms | 77424 KiB |
| 1_13.txt | AC | 520 ms | 77984 KiB |
| 1_14.txt | AC | 518 ms | 77992 KiB |
| 1_15.txt | AC | 510 ms | 78424 KiB |
| 1_16.txt | AC | 469 ms | 68304 KiB |
| 1_17.txt | AC | 566 ms | 77968 KiB |
| 1_18.txt | AC | 469 ms | 74592 KiB |
| 1_19.txt | AC | 499 ms | 71308 KiB |
| 1_20.txt | AC | 323 ms | 43260 KiB |
| 1_21.txt | AC | 289 ms | 39008 KiB |
| 1_22.txt | AC | 145 ms | 12212 KiB |
| 1_23.txt | AC | 371 ms | 51520 KiB |
| 1_24.txt | AC | 380 ms | 53568 KiB |
| 1_25.txt | AC | 340 ms | 42100 KiB |
| 1_26.txt | AC | 162 ms | 13112 KiB |
| 1_27.txt | AC | 191 ms | 17792 KiB |
| 1_28.txt | AC | 463 ms | 58116 KiB |
| 1_29.txt | AC | 294 ms | 38424 KiB |
| 1_30.txt | AC | 443 ms | 55776 KiB |
| 1_31.txt | AC | 308 ms | 36644 KiB |
| 1_32.txt | AC | 356 ms | 43332 KiB |
| 1_33.txt | AC | 419 ms | 56436 KiB |
| 1_34.txt | AC | 198 ms | 17928 KiB |
| 1_35.txt | AC | 352 ms | 38732 KiB |
| 1_36.txt | AC | 208 ms | 19512 KiB |
| 1_37.txt | AC | 305 ms | 39480 KiB |
| 1_38.txt | AC | 409 ms | 52188 KiB |
| 1_39.txt | AC | 236 ms | 33924 KiB |
| 1_40.txt | AC | 426 ms | 51416 KiB |
| 1_41.txt | AC | 294 ms | 36364 KiB |
| 1_42.txt | AC | 392 ms | 52892 KiB |
| 1_43.txt | AC | 380 ms | 50696 KiB |
| 1_44.txt | AC | 389 ms | 52252 KiB |
| 1_45.txt | AC | 227 ms | 23028 KiB |
| 1_46.txt | AC | 406 ms | 54968 KiB |
| 1_47.txt | AC | 267 ms | 35924 KiB |
| 1_48.txt | AC | 250 ms | 37192 KiB |
| 1_49.txt | AC | 190 ms | 16516 KiB |
| 1_50.txt | AC | 324 ms | 40704 KiB |
| 1_51.txt | AC | 431 ms | 58232 KiB |
| 1_52.txt | AC | 160 ms | 13752 KiB |
| 1_53.txt | AC | 207 ms | 19796 KiB |
| 1_54.txt | AC | 296 ms | 37268 KiB |
| 1_55.txt | AC | 280 ms | 37180 KiB |
| 1_56.txt | AC | 359 ms | 48888 KiB |
| 1_57.txt | AC | 444 ms | 60080 KiB |
| 1_58.txt | AC | 406 ms | 54980 KiB |
| 1_59.txt | AC | 154 ms | 12980 KiB |
| 1_60.txt | AC | 98 ms | 8276 KiB |
| 1_61.txt | AC | 99 ms | 8268 KiB |
| 1_62.txt | AC | 98 ms | 8400 KiB |
| 1_63.txt | AC | 96 ms | 8272 KiB |