Submission #993556


Source Code Expand

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

public class Main {

	BufferedReader br;
	PrintWriter out;
	StringTokenizer st;
	boolean eof;

	static class Query implements Comparable<Query> {
		int from, to, cost;

		public Query(int from, int to, int cost) {
			this.from = from;
			this.to = to;
			this.cost = cost;
		}

		@Override
		public int compareTo(Query o) {
			return Integer.compare(cost, o.cost);
		}

	}

	int[] p;

	int get(int v) {
		return p[v] == v ? v : (p[v] = get(p[v]));
	}

	boolean unite(int a, int b) {
		a = get(a);
		b = get(b);

		if (a != b) {
			if (((a + b) & 1) == 1) {
				p[a] = b;
			} else {
				p[b] = a;
			}
		}

		return a != b;
	}

	void solve() throws IOException {
		int n = nextInt();
		int q = nextInt();

		PriorityQueue<Query> que = new PriorityQueue<>();

		for (int i = 0; i < q; i++) {
			int from = nextInt();
			int to = nextInt();
			int cost = nextInt();
			que.add(new Query(from, to, cost));
			que.add(new Query(to, (from + 1) % n, cost + 1));
		}

		p = new int[n];
		for (int i = 0; i < n; i++) {
			p[i] = i;
		}

		long ans = 0;

		while (!que.isEmpty()) {
			Query top = que.poll();

			// System.err.println(top.from + " " + top.to + " " + top.cost);

			if (unite(top.from, top.to)) {
				ans += top.cost;

				top.from = (top.from + 1) % n;
				top.to = (top.to + 1) % n;
				top.cost += 2;

				que.add(top);
			}
		}

		out.println(ans);
	}

	Main() throws IOException {
		br = new BufferedReader(new InputStreamReader(System.in));
		out = new PrintWriter(System.out);
		solve();
		out.close();
	}

	public static void main(String[] args) throws IOException {
		new Main();
	}

	String nextToken() {
		while (st == null || !st.hasMoreTokens()) {
			try {
				st = new StringTokenizer(br.readLine());
			} catch (Exception e) {
				eof = true;
				return null;
			}
		}
		return st.nextToken();
	}

	String nextString() {
		try {
			return br.readLine();
		} catch (IOException e) {
			eof = true;
			return null;
		}
	}

	int nextInt() throws IOException {
		return Integer.parseInt(nextToken());
	}

	long nextLong() throws IOException {
		return Long.parseLong(nextToken());
	}

	double nextDouble() throws IOException {
		return Double.parseDouble(nextToken());
	}
}

Submission Info

Submission Time
Task G - Zigzag MST
User mmaxio
Language Java8 (OpenJDK 1.8.0)
Score 1300
Code Size 2360 Byte
Status AC
Exec Time 654 ms
Memory 61752 KB

Judge Result

Set Name sample all
Score / Max Score 0 / 0 1300 / 1300
Status
AC × 3
AC × 33
Set Name Test Cases
sample sample-01.txt, sample-02.txt, sample-03.txt
all sample-01.txt, sample-02.txt, sample-03.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, 01-11.txt, 01-12.txt, 01-13.txt, 01-14.txt, 01-15.txt, 01-16.txt, 01-17.txt, 01-18.txt, 01-19.txt, 01-20.txt, 01-21.txt, 01-22.txt, 01-23.txt, 01-24.txt, 01-25.txt, 01-26.txt, 01-27.txt, 01-28.txt, 01-29.txt, 01-30.txt
Case Name Status Exec Time Memory
01-01.txt AC 98 ms 8276 KB
01-02.txt AC 562 ms 56168 KB
01-03.txt AC 654 ms 55848 KB
01-04.txt AC 152 ms 12640 KB
01-05.txt AC 173 ms 12332 KB
01-06.txt AC 195 ms 11592 KB
01-07.txt AC 198 ms 11688 KB
01-08.txt AC 204 ms 12064 KB
01-09.txt AC 268 ms 16832 KB
01-10.txt AC 426 ms 34764 KB
01-11.txt AC 604 ms 55116 KB
01-12.txt AC 627 ms 54960 KB
01-13.txt AC 609 ms 54728 KB
01-14.txt AC 610 ms 55124 KB
01-15.txt AC 614 ms 55248 KB
01-16.txt AC 614 ms 54792 KB
01-17.txt AC 610 ms 54948 KB
01-18.txt AC 533 ms 61752 KB
01-19.txt AC 177 ms 11168 KB
01-20.txt AC 219 ms 12188 KB
01-21.txt AC 395 ms 29868 KB
01-22.txt AC 563 ms 55784 KB
01-23.txt AC 558 ms 56376 KB
01-24.txt AC 246 ms 31060 KB
01-25.txt AC 564 ms 60736 KB
01-26.txt AC 137 ms 13392 KB
01-27.txt AC 274 ms 16580 KB
01-28.txt AC 509 ms 36080 KB
01-29.txt AC 547 ms 55168 KB
01-30.txt AC 592 ms 55260 KB
sample-01.txt AC 96 ms 8148 KB
sample-02.txt AC 100 ms 8144 KB
sample-03.txt AC 98 ms 8140 KB