Submission #1668379


Source Code Expand

Copy
import java.io.*;
import java.util.*;

public class Main {
    private FastScanner in;
    private PrintWriter out;

    ArrayList<Integer>[] g;
    int[] color;

    boolean dfs(int v, int curColor) {
        if (color[v] == 0) {
            color[v] = curColor;
        } else {
            return color[v] == curColor;
        }
        for (int to : g[v]) {
            if (!dfs(to, 3 - curColor)) {
                return false;
            }
        }
        return true;
    }

    void solve() {
        int n = in.nextInt();
        int m = in.nextInt();
        g = new ArrayList[n];
        for (int i = 0; i < n; i++) {
            g[i] = new ArrayList<>();
        }
        for (int i = 0; i < m; i++) {
            int fr = in.nextInt() - 1;
            int to = in.nextInt() - 1;
            g[fr].add(to);
            g[to].add(fr);
        }
        color = new int[n];
        long total = 0;
        if (dfs(0, 1)) {
            int s1 = 0;
            for (int i = 0; i < n; i++) {
                if (color[i] == 1) {
                    s1++;
                }
            }
            total = s1 * 1L * (n - s1);
        } else {
            total = n * 1L * (n - 1) / 2;
        }
        out.println(total - m);
    }

    private void run() {
        try {
            in = new FastScanner(new File("Main.in"));
            out = new PrintWriter(new File("Main.out"));

            solve();

            out.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

    private void runIO() {
        in = new FastScanner(System.in);
        out = new PrintWriter(System.out);

        solve();

        out.close();
    }

    private class FastScanner {
        BufferedReader br;
        StringTokenizer st;

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

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

        String next() {
            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();
        }

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

        int nextInt() {
            return Integer.parseInt(next());
        }

        long nextLong() {
            return Long.parseLong(next());
        }

        double nextDouble() {
            return Double.parseDouble(next());
        }
    }

    public static void main(String[] args) {
        new Main().runIO();
    }
}

Submission Info

Submission Time
Task C - 3 Steps
User qwerty787788
Language Java8 (OpenJDK 1.8.0)
Score 500
Code Size 3475 Byte
Status
Exec Time 288 ms
Memory 57836 KB

Compile Error

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

Test Cases

Set Name Score / Max Score Test Cases
sample 0 / 0 sample-01.txt, sample-02.txt
all 500 / 500 sample-01.txt, sample-02.txt, 01-01.txt, 01-02.txt, 01-03.txt, 01-04.txt, 01-05.txt, 01-06.txt, 01-07.txt, 01-08.txt, 01-09.txt, 01-10.txt, 02-01.txt, 02-02.txt, 02-03.txt, 02-04.txt, 02-05.txt, 02-06.txt, 02-07.txt, 02-08.txt, 02-09.txt, 02-10.txt, 02-11.txt, 02-12.txt, 02-13.txt, 02-14.txt, sample-01.txt, sample-02.txt
Case Name Status Exec Time Memory
01-01.txt 72 ms 19284 KB
01-02.txt 72 ms 21588 KB
01-03.txt 72 ms 19412 KB
01-04.txt 73 ms 21588 KB
01-05.txt 85 ms 23508 KB
01-06.txt 93 ms 20948 KB
01-07.txt 73 ms 19412 KB
01-08.txt 88 ms 19668 KB
01-09.txt 95 ms 19924 KB
01-10.txt 105 ms 20820 KB
02-01.txt 247 ms 53556 KB
02-02.txt 269 ms 56428 KB
02-03.txt 270 ms 52604 KB
02-04.txt 275 ms 53752 KB
02-05.txt 288 ms 53316 KB
02-06.txt 268 ms 52660 KB
02-07.txt 223 ms 46972 KB
02-08.txt 252 ms 52324 KB
02-09.txt 256 ms 56532 KB
02-10.txt 245 ms 39052 KB
02-11.txt 235 ms 45604 KB
02-12.txt 262 ms 56184 KB
02-13.txt 269 ms 55600 KB
02-14.txt 288 ms 57836 KB
sample-01.txt 83 ms 21332 KB
sample-02.txt 68 ms 18132 KB