結果

問題 No.1207 グラフX
ユーザー CuriousFairy315CuriousFairy315
提出日時 2020-08-30 15:15:50
言語 Java19
(openjdk 21)
結果
AC  
実行時間 956 ms / 2,000 ms
コード長 10,609 bytes
コンパイル時間 5,695 ms
コンパイル使用メモリ 79,292 KB
実行使用メモリ 112,828 KB
最終ジャッジ日時 2023-08-09 13:09:12
合計ジャッジ時間 36,355 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 758 ms
112,388 KB
testcase_01 AC 901 ms
105,688 KB
testcase_02 AC 880 ms
105,352 KB
testcase_03 AC 755 ms
104,916 KB
testcase_04 AC 702 ms
100,968 KB
testcase_05 AC 704 ms
99,524 KB
testcase_06 AC 682 ms
98,912 KB
testcase_07 AC 609 ms
94,176 KB
testcase_08 AC 627 ms
88,420 KB
testcase_09 AC 790 ms
94,184 KB
testcase_10 AC 756 ms
99,152 KB
testcase_11 AC 690 ms
99,068 KB
testcase_12 AC 673 ms
89,904 KB
testcase_13 AC 304 ms
63,736 KB
testcase_14 AC 817 ms
101,884 KB
testcase_15 AC 833 ms
95,148 KB
testcase_16 AC 338 ms
68,452 KB
testcase_17 AC 616 ms
88,804 KB
testcase_18 AC 526 ms
85,812 KB
testcase_19 AC 484 ms
75,720 KB
testcase_20 AC 956 ms
102,448 KB
testcase_21 AC 115 ms
53,468 KB
testcase_22 AC 609 ms
85,864 KB
testcase_23 AC 666 ms
91,576 KB
testcase_24 AC 497 ms
87,080 KB
testcase_25 AC 921 ms
103,284 KB
testcase_26 AC 672 ms
93,724 KB
testcase_27 AC 873 ms
96,724 KB
testcase_28 AC 841 ms
96,016 KB
testcase_29 AC 821 ms
98,160 KB
testcase_30 AC 422 ms
68,484 KB
testcase_31 AC 237 ms
59,148 KB
testcase_32 AC 403 ms
74,176 KB
testcase_33 AC 420 ms
75,600 KB
testcase_34 AC 801 ms
94,500 KB
testcase_35 AC 123 ms
52,684 KB
testcase_36 AC 802 ms
94,856 KB
testcase_37 AC 638 ms
86,032 KB
testcase_38 AC 232 ms
58,884 KB
testcase_39 AC 423 ms
78,544 KB
testcase_40 AC 174 ms
54,396 KB
testcase_41 AC 486 ms
76,080 KB
testcase_42 AC 44 ms
49,552 KB
testcase_43 AC 43 ms
49,428 KB
testcase_44 AC 44 ms
49,384 KB
testcase_45 AC 775 ms
111,196 KB
testcase_46 AC 746 ms
111,192 KB
testcase_47 AC 744 ms
112,828 KB
testcase_48 AC 741 ms
110,864 KB
権限があれば一括ダウンロードができます

ソースコード

diff #


import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.BitSet;
import java.util.NoSuchElementException;
import java.util.Queue;

public class Main {

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

	public Main() {
		InputChecker ic = new InputChecker(System.in);
		java.io.PrintWriter out = new java.io.PrintWriter(System.out);
		solve(ic, out);
		out.flush();
	}

	public void solve(InputChecker ic, java.io.PrintWriter out) {
		int N = ic.nextInt(2, exponent10(2, 5));
		ic.nextChar(' ');
		int M = ic.nextInt(N - 1, exponent10(2, 5));
		ic.nextChar(' ');
		int X = ic.nextInt(2, exponent10(1, 9));
		ic.readNewLine();
		UnionFind uf = new UnionFind(N);
		final int MOD = 1_000_000_007;
		ArrayList<ArrayList<Edge>> line = new ArrayList<>(N);
		for (int i = 0;i < N;++ i) line.add(new ArrayList<>());
		int lastZ = -1;
		for (int i = 0;i < M;++ i) {
			int x = ic.nextInt(1, N) - 1;
			ic.nextChar(' ');
			int y = ic.nextInt(1, N) - 1;
			if (x == y) throw new AssertionError();
			ic.nextChar(' ');
			int z = ic.nextInt(lastZ + 1, exponent10(1, 9));
			lastZ = z;
			ic.readNewLine();
			if (uf.unite(x, y)) {
				int weight = pow(X, z, MOD);
				line.get(x).add(new Edge(y, weight));
				line.get(y).add(new Edge(x, weight));
			}
		}
		ic.checkEOF();
		if (uf.size(0) != N) throw new AssertionError();

		Queue<Integer> bfs = new ArrayDeque<>();
		for (int i = 0;i < N;++ i) if (line.get(i).size() == 1) bfs.add(i);
		int[] count = new int[N];
		uf = new UnionFind(N);
		int ans = 0;
		while(!bfs.isEmpty()) {
			int tmp = bfs.poll();
			for (Edge e : line.get(tmp)) {
				if (uf.isUnite(tmp, e.target)) continue;
				if (++ count[e.target] == line.get(e.target).size() - 1) bfs.add(e.target);
				ans = add(ans, mul(mul(uf.size(tmp), N - uf.size(tmp), MOD), e.weight, MOD), MOD);
				uf.unite(tmp, e.target);
			}
		}
		out.println(ans);
	}

	class Edge {
		final int target, weight;
		Edge(int target, int weight) {
			this.target = target;
			this.weight = weight;
		}
	}

	class UnionFind {
		final int[] parent;
		public UnionFind(int n) {
			parent = new int[n];
			java.util.Arrays.fill(parent, -1);
		}
		private int parent(int n) {
			if (parent[n] < 0) return n;
			return parent[n] = parent(parent[n]);
		}
		public int size(int n) {
			return -parent[parent(n)];
		}
		public boolean unite(int l, int r) {
			l = parent(l);
			r = parent(r);
			if (l == r) return false;
			if (parent[l] < parent[r]) {
				parent[l] += parent[r];
				parent[r] = l;
			} else {
				parent[r] += parent[l];
				parent[l] = r;
			}
			return true;
		}
		public boolean isUnite(int l, int r) {
			return parent(l) == parent(r);
		}
	}

	private int add(int a, int b, int mod) {
		return (a + b) % mod;
	}

	private int sub(int a, int b, int mod) {
		return (a - b + mod) % mod;
	}

	private int mul(int a, int b, int mod) {
		return (int)((long)a * b % mod);
	}

	private int div(int a, int b, int mod) {
		return mul(a, pow(b, mod - 2, mod), mod);
	}

	private int pow(int a, int b, int mod) {
		int ans = 1;
		for (int mul = a;b > 0;b >>= 1, mul = mul(mul, mul, mod)) if ((b & 1) != 0) ans = mul(ans, mul, mod);
		return ans;
	}

	public static int exponent10(int n, int e) {
		return n * pow(10, e);
	}

	public static long exponent10L(int n, int e) {
		return n * pow(10L, e);
	}

	public static int pow(int a, int b) {
		int ans = 1;
		for (int mul = a;b > 0;b >>= 1, mul *= mul) if ((b & 1) != 0) ans *= mul;
		return ans;
	}

	public static long pow(long a, long b) {
		long ans = 1;
		for (long mul = a;b > 0;b >>= 1, mul *= mul) if ((b & 1) != 0) ans *= mul;
		return ans;
	}

	public static class CharSet {
		private final BitSet set = new BitSet(256);

		public void add(char... c) {
			for (char i : c) set.set(i);
		}

		public void add(CharSet... s) {
			for (CharSet i : s) set.or(i.set);
		}

		public boolean contains(char... c) {
			for (char i : c) if (!set.get(i)) return false;
			return true;
	 	}

		public boolean contains(String s) {
			return contains(s.toCharArray());
	 	}

		private static final class Chars extends CharSet {
			public Chars(char... c) {
				super.add(c);
			}
			public Chars(CharSet... s) {
				super.add(s);
			}
			@Override
			public void add(char... c) {
				throw new UnsupportedOperationException();
			}
			@Override
			public void add(CharSet... s) {
				throw new UnsupportedOperationException();
			}
		}

		public static final CharSet NUMBER = new Chars('0','1','2','3','4','5','6','7','8','9');
		public static final CharSet LOWER = new Chars('a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z');
		public static final CharSet UPPER = new Chars('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z');
		public static final CharSet ALPHABET = new Chars(LOWER, UPPER);

	}
	public static class InputChecker {
		private InputStream in;
		private final byte[] buffer = new byte[1024];
		private final byte[] undo = new byte[1024];
		private int undoSize = 0;
		private int read = 0;
		private int length = 0;

		public InputChecker(InputStream in) {
			this.in = in;
		}

		public final void setInputStream(InputStream in) {
			this.in = in;
		}

		public final void setInputStream(File in) {
			try {
				this.in = new FileInputStream(in);
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			}
		}

		private boolean hasNextByte() {
			if (undoSize > 0) return true;
			if (read < length) return true;
			read = 0;
			try {
				length = in.read(buffer);
			} catch (IOException e) {
				e.printStackTrace();
			}
			return length > 0;
		}

		private byte readByte() {
			if (hasNextByte()) return undoSize > 0 ? undo[--undoSize] : buffer[read++];
			throw new NoSuchElementException();
		}

		private void undo(byte b) {
			undo[undoSize ++] = b;
		}

		private void undo(char c) {
			if ((c & 0xFF80) == 0) {
				undo((byte)c);
				return;
			}
			undo((byte)(c & 0x3F | 0x80));
			if ((c & 0xF800) == 0) {
				undo((byte)(c >> 6 & 0x1F | 0xC0));
			} else {
				undo((byte)(c >> 6 & 0x3F | 0x80));
				undo((byte)(c >> 12 | 0xE0));
			}
		}

		public final boolean hasNext() {
			return hasNextByte();
		}

		public final char nextChar() {
			byte b = readByte();
			if ((b & 0x80) == 0) return (char)b;
			if ((b & 0x20) == 0) return (char)((b & 0x1F) << 6 | (readByte() & 0x3F));
			return (char)((b & 0xF) << 12 | (readByte() & 0x3F) << 6 | (readByte() & 0x3F));
		}

		public final char nextChar(char estimate) {
			char c = nextChar();
			if (estimate == c) return estimate;
			undo(c);
			throw new AssertionError();
		}

		public final char nextChar(CharSet estimate) {
			char c = nextChar();
			if (estimate.contains(c)) return c;
			undo(c);
			throw new AssertionError();
		}

		public final void readNewLine() {
			char c = nextChar();
			if (c == '\r') {
				c = nextChar();
				if (c != '\n') undo(c);
				return;
			} else if (c == '\n') return;
			undo(c);
			throw new AssertionError();
		}

		public final void checkEOF() {
			if (hasNextByte()) throw new AssertionError();
		}

		public final String next(CharSet contains) {
			StringBuilder sb = new StringBuilder();
			try {
				do {
					char c = nextChar();
					if (!contains.contains(c)) {
						undo(c);
						return sb.toString();
					}
					sb.append(c);
				} while(true);
			} catch (NoSuchElementException e) {
				if (sb.length() <= 0) throw new AssertionError();
				return sb.toString();
			}
		}

		public final int nextInt() {
			byte b = readByte();
			int n = 0;
			if (b == '-') {
				if (!isNumber(b = readByte())) {
					undo(b);
					throw new NumberFormatException();
				}
				try {
					if (b == '0') {
						if (!isNumber(b = readByte())) {
							undo(b);
							return 0;
						}
						throw new NumberFormatException();
					}
					do n = Math.addExact(Math.multiplyExact(n, 10), '0' - b); while(isNumber(b = readByte()));
					undo(b);
				} catch (NoSuchElementException e) {
				}
				return n;
			}
			if (!isNumber(b)) {
				undo(b);
				throw new NumberFormatException();
			}
			try {
				if (b == '0') {
					if (!isNumber(b = readByte())) {
						undo(b);
						return 0;
					}
					throw new NumberFormatException();
				}
				do n = Math.addExact(Math.multiplyExact(n, 10), b - '0'); while(isNumber(b = readByte()));
				undo(b);
			} catch (NoSuchElementException e) {
			}
			return n;
		}

		public final int nextInt(int min, int max) {
			int n = nextInt();
			if (min <= n && n <= max) return n;
			throw new NumberFormatException();
		}

		private static boolean isNumber(byte c) {
			return '0' <= c && c <= '9';
		}

		public final long nextLong() {
			byte b = readByte();
			long n = 0;
			if (b == '-') {
				if (!isNumber(b = readByte())) {
					undo(b);
					throw new NumberFormatException();
				}
				try {
					if (b == '0') {
						if (!isNumber(b = readByte())) {
							undo(b);
							return 0;
						}
						throw new NumberFormatException();
					}
					do n = Math.addExact(Math.multiplyExact(n, 10), '0' - b); while(isNumber(b = readByte()));
					undo(b);
				} catch (NoSuchElementException e) {
				}
				return n;
			}
			if (!isNumber(b)) {
				undo(b);
				throw new NumberFormatException();
			}
			try {
				if (b == '0') {
					if (!isNumber(b = readByte())) {
						undo(b);
						return 0;
					}
					throw new NumberFormatException();
				}
				do n = Math.addExact(Math.multiplyExact(n, 10), b - '0'); while(isNumber(b = readByte()));
				undo(b);
			} catch (NoSuchElementException e) {
			}
			return n;
		}

		public final long nextLong(long min, long max) {
			long n = nextLong();
			if (min <= n && n <= max) return n;
			throw new NumberFormatException();
		}

		public final double nextDouble() {
			StringBuilder sb = new StringBuilder();
			byte b = readByte();
			if (b == '-') {
				sb.append(b);
				b = readByte();
			}
			if (b == '0') {
				sb.append(b);
				b = readByte();
			} else {
				while(isNumber(b)) {
					sb.append(b);
					b = readByte();
				}
			}
			if (b == '.') {
				sb.append(b);
				b = readByte();
				while(isNumber(b)) {
					sb.append(b);
					b = readByte();
				}
			}
			if (b == 'e' || b == 'E') {
				sb.append(b);
				b = readByte();
				if (b == '-' || b == '+') {
					sb.append(b);
					b = readByte();
				}
				while(isNumber(b)) {
					sb.append(b);
					b = readByte();
				}
			}
			undo(b);
			return Double.parseDouble(sb.toString());
		}
	}
}
0