Submission #2035560


Source Code Expand

import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.stream.IntStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;
import java.util.StringTokenizer;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 *
 * @author ShekharN
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        FastScanner in = new FastScanner(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        TaskB solver = new TaskB();
        solver.solve(1, in, out);
        out.close();
    }

    static class TaskB {
        public void solve(int testNumber, FastScanner in, PrintWriter out) {
            int n = in.nextInt(), m = in.nextInt();
            List[] graph = IntStream.rangeClosed(0, n)
                    .mapToObj(i -> new ArrayList<PairIntInt>())
                    .toArray(List[]::new);

            for (int i = 0; i < m; i++) {
                int l = in.nextInt(), r = in.nextInt(), d = in.nextInt();
                graph[l].add(new PairIntInt(r, d));
                graph[r].add(new PairIntInt(l, -d));
            }

            if (check(graph, n)) {
                out.println("Yes");
            } else {
                out.println("No");
            }
        }

        private boolean check(List[] graph, int n) {
            int[] values = IntStream.range(0, n + 1).map(i -> Integer.MIN_VALUE).toArray();
            for (int i = 1; i <= n; i++) {
                if (values[i] == Integer.MIN_VALUE && !dfs(graph, values, i, 0))
                    return false;
            }
            return true;
        }

        private boolean dfs(List[] graph, int[] values, int cur, int curVal) {
            if (values[cur] != Integer.MIN_VALUE && values[cur] != curVal)
                return false;
            if (values[cur] == Integer.MIN_VALUE) {
                values[cur] = curVal;
                for (int i = 0; i < graph[cur].size(); i++) {
                    PairIntInt child = (PairIntInt) graph[cur].get(i);
                    int val = curVal + child.getSecond();
                    if (!dfs(graph, values, child.getFirst(), val))
                        return false;
                }
            }
            return true;
        }

    }

    static class PairIntInt {
        private int first;
        private int second;

        PairIntInt() {
        }

        public PairIntInt(int first, int second) {
            this.first = first;
            this.second = second;
        }

        public int getFirst() {
            return first;
        }

        public int getSecond() {
            return second;
        }


        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;

            PairIntInt that = (PairIntInt) o;

            if (first != that.first) return false;
            return second == that.second;
        }


        public int hashCode() {
            int result = first;
            result = 31 * result + second;
            return result;
        }

    }

    static class FastScanner {
        private BufferedReader br;
        private StringTokenizer st;

        public FastScanner(File f) {
            try {
                br = new BufferedReader(new FileReader(f));
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
        }

        public FastScanner(InputStream f) {
            br = new BufferedReader(new InputStreamReader(f));
        }

        public String nextString() {
            while (st == null || !st.hasMoreTokens()) {
                String s = null;
                try {
                    s = br.readLine();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                if (s == null)
                    return null;
                st = new StringTokenizer(s);
            }
            return st.nextToken();
        }

        public int nextInt() {
            return Integer.parseInt(nextString());
        }

    }
}

Submission Info

Submission Time
Task D - People on a Line
User FLoop
Language Java8 (OpenJDK 1.8.0)
Score 400
Code Size 4650 Byte
Status AC
Exec Time 562 ms
Memory 84044 KB

Compile Error

Note: ./Main.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 400 / 400
Status
AC × 5
AC × 47
Set Name Test Cases
Sample sample01.txt, sample02.txt, sample03.txt, sample04.txt, sample05.txt
All 01.txt, 02.txt, 03.txt, 04.txt, 05.txt, 06.txt, 07.txt, 08.txt, 09.txt, 10.txt, 11.txt, 12.txt, 13.txt, 14.txt, 15.txt, 16.txt, 17.txt, 18.txt, 19.txt, 20.txt, 21.txt, 22.txt, 23.txt, 24.txt, 25.txt, 26.txt, 27.txt, 28.txt, 29.txt, 30.txt, 31.txt, 32.txt, 33.txt, 34.txt, 35.txt, 36.txt, 37.txt, 38.txt, 39.txt, 40.txt, 41.txt, 42.txt, sample01.txt, sample02.txt, sample03.txt, sample04.txt, sample05.txt
Case Name Status Exec Time Memory
01.txt AC 541 ms 75620 KB
02.txt AC 536 ms 75720 KB
03.txt AC 481 ms 75400 KB
04.txt AC 557 ms 80080 KB
05.txt AC 488 ms 75764 KB
06.txt AC 472 ms 65576 KB
07.txt AC 477 ms 75040 KB
08.txt AC 479 ms 64448 KB
09.txt AC 444 ms 60856 KB
10.txt AC 457 ms 58544 KB
11.txt AC 438 ms 59924 KB
12.txt AC 436 ms 63424 KB
13.txt AC 509 ms 76336 KB
14.txt AC 462 ms 59028 KB
15.txt AC 466 ms 62196 KB
16.txt AC 436 ms 64448 KB
17.txt AC 558 ms 83424 KB
18.txt AC 562 ms 72316 KB
19.txt AC 482 ms 65536 KB
20.txt AC 484 ms 64868 KB
21.txt AC 502 ms 75368 KB
22.txt AC 427 ms 63492 KB
23.txt AC 509 ms 78184 KB
24.txt AC 450 ms 61448 KB
25.txt AC 538 ms 79944 KB
26.txt AC 534 ms 82308 KB
27.txt AC 446 ms 62268 KB
28.txt AC 475 ms 65432 KB
29.txt AC 453 ms 67636 KB
30.txt AC 462 ms 63244 KB
31.txt AC 446 ms 63504 KB
32.txt AC 470 ms 64380 KB
33.txt AC 502 ms 84044 KB
34.txt AC 510 ms 77204 KB
35.txt AC 426 ms 64192 KB
36.txt AC 425 ms 62476 KB
37.txt AC 440 ms 66924 KB
38.txt AC 438 ms 61340 KB
39.txt AC 396 ms 54480 KB
40.txt AC 435 ms 62060 KB
41.txt AC 207 ms 27396 KB
42.txt AC 363 ms 45924 KB
sample01.txt AC 151 ms 24276 KB
sample02.txt AC 155 ms 26196 KB
sample03.txt AC 155 ms 26196 KB
sample04.txt AC 163 ms 26580 KB
sample05.txt AC 163 ms 24788 KB