Submission #7983480


Source Code Expand

import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.InputMismatchException;
import java.util.Map;
import java.util.NoSuchElementException;

public class Main {

	static PrintWriter out;
	static InputReader ir;

	static void solve() {
		int n = ir.nextInt();
		long x = ir.nextLong();
		long[] xor = new long[n];
		HashMap<Long, Long> mp = new HashMap<>();
		Graph[] g = new Graph[n];
		for (int i = 0; i < n; i++)
			g[i] = new Graph();
		for (int i = 0; i < n - 1; i++) {
			int u = ir.nextInt() - 1;
			int v = ir.nextInt() - 1;
			int d = ir.nextInt();
			g[u].add(new int[] { v, d });
			g[v].add(new int[] { u, d });
		}
		dfs(0, -1, xor, g, mp);
		if (x == 0) {
			long res = 0;
			for (Map.Entry<Long, Long> e : mp.entrySet()) {
				res += e.getValue() * (e.getValue() - 1) / 2;
			}
			out.println(res);
		} else {
			long res = 0;
			for (Map.Entry<Long, Long> e : mp.entrySet()) {
				long p = x ^ e.getKey();
				if (p > e.getKey())
					continue;
				if (mp.containsKey(p))
					res += e.getValue() * mp.get(p);
			}
			out.println(res);
		}
	}

	static void dfs(int cur, int pre, long[] xor, Graph[] g, HashMap<Long, Long> mp) {
		if (!mp.containsKey(xor[cur])) {
			mp.put(xor[cur], 1L);
		} else {
			mp.put(xor[cur], mp.get(xor[cur]) + 1);
		}
		for (int[] ch : g[cur]) {
			if (ch[0] == pre)
				continue;
			xor[ch[0]] = xor[cur] ^ (long) ch[1];
			dfs(ch[0], cur, xor, g, mp);
		}
	}

	static class Graph extends ArrayList<int[]> {
	}

	public static void main(String[] args) {
		ir = new InputReader(System.in);
		out = new PrintWriter(System.out);
		solve();
		out.flush();
	}

	static class InputReader {

		private InputStream in;
		private byte[] buffer = new byte[1024];
		private int curbuf;
		private int lenbuf;

		public InputReader(InputStream in) {
			this.in = in;
			this.curbuf = this.lenbuf = 0;
		}

		public boolean hasNextByte() {
			if (curbuf >= lenbuf) {
				curbuf = 0;
				try {
					lenbuf = in.read(buffer);
				} catch (IOException e) {
					throw new InputMismatchException();
				}
				if (lenbuf <= 0)
					return false;
			}
			return true;
		}

		private int readByte() {
			if (hasNextByte())
				return buffer[curbuf++];
			else
				return -1;
		}

		private boolean isSpaceChar(int c) {
			return !(c >= 33 && c <= 126);
		}

		private void skip() {
			while (hasNextByte() && isSpaceChar(buffer[curbuf]))
				curbuf++;
		}

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

		public String next() {
			if (!hasNext())
				throw new NoSuchElementException();
			StringBuilder sb = new StringBuilder();
			int b = readByte();
			while (!isSpaceChar(b)) {
				sb.appendCodePoint(b);
				b = readByte();
			}
			return sb.toString();
		}

		public int nextInt() {
			if (!hasNext())
				throw new NoSuchElementException();
			int c = readByte();
			while (isSpaceChar(c))
				c = readByte();
			boolean minus = false;
			if (c == '-') {
				minus = true;
				c = readByte();
			}
			int res = 0;
			do {
				if (c < '0' || c > '9')
					throw new InputMismatchException();
				res = res * 10 + c - '0';
				c = readByte();
			} while (!isSpaceChar(c));
			return (minus) ? -res : res;
		}

		public long nextLong() {
			if (!hasNext())
				throw new NoSuchElementException();
			int c = readByte();
			while (isSpaceChar(c))
				c = readByte();
			boolean minus = false;
			if (c == '-') {
				minus = true;
				c = readByte();
			}
			long res = 0;
			do {
				if (c < '0' || c > '9')
					throw new InputMismatchException();
				res = res * 10 + c - '0';
				c = readByte();
			} while (!isSpaceChar(c));
			return (minus) ? -res : res;
		}

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

		public int[] nextIntArray(int n) {
			int[] a = new int[n];
			for (int i = 0; i < n; i++)
				a[i] = nextInt();
			return a;
		}

		public long[] nextLongArray(int n) {
			long[] a = new long[n];
			for (int i = 0; i < n; i++)
				a[i] = nextLong();
			return a;
		}

		public char[][] nextCharMap(int n, int m) {
			char[][] map = new char[n][m];
			for (int i = 0; i < n; i++)
				map[i] = next().toCharArray();
			return map;
		}
	}

	static void tr(Object... o) {
		out.println(Arrays.deepToString(o));
	}
}

Submission Info

Submission Time
Task C - エックスオア多橋君
User holeguma
Language Java8 (OpenJDK 1.8.0)
Score 100
Code Size 4535 Byte
Status AC
Exec Time 275 ms
Memory 80924 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 100 / 100
Status
AC × 3
AC × 27
Set Name Test Cases
Sample subtask0_sample_01.txt, subtask0_sample_02.txt, subtask0_sample_03.txt
All subtask0_sample_01.txt, subtask0_sample_02.txt, subtask0_sample_03.txt, subtask1_01.txt, subtask1_02.txt, subtask1_03.txt, subtask1_04.txt, subtask1_05.txt, subtask1_06.txt, subtask1_07.txt, subtask1_08.txt, subtask1_09.txt, subtask1_10.txt, subtask1_11.txt, subtask1_12.txt, subtask1_13.txt, subtask1_14.txt, subtask1_15.txt, subtask1_16.txt, subtask1_17.txt, subtask1_18.txt, subtask1_19.txt, subtask1_20.txt, subtask1_21.txt, subtask1_22.txt, subtask1_23.txt, subtask1_24.txt
Case Name Status Exec Time Memory
subtask0_sample_01.txt AC 73 ms 19540 KB
subtask0_sample_02.txt AC 72 ms 18772 KB
subtask0_sample_03.txt AC 70 ms 19412 KB
subtask1_01.txt AC 69 ms 19412 KB
subtask1_02.txt AC 93 ms 23380 KB
subtask1_03.txt AC 275 ms 52548 KB
subtask1_04.txt AC 263 ms 55304 KB
subtask1_05.txt AC 274 ms 55572 KB
subtask1_06.txt AC 214 ms 80924 KB
subtask1_07.txt AC 240 ms 53768 KB
subtask1_08.txt AC 215 ms 53092 KB
subtask1_09.txt AC 241 ms 53652 KB
subtask1_10.txt AC 241 ms 55680 KB
subtask1_11.txt AC 94 ms 19412 KB
subtask1_12.txt AC 84 ms 21076 KB
subtask1_13.txt AC 227 ms 53208 KB
subtask1_14.txt AC 260 ms 56416 KB
subtask1_15.txt AC 119 ms 21844 KB
subtask1_16.txt AC 121 ms 24404 KB
subtask1_17.txt AC 110 ms 20820 KB
subtask1_18.txt AC 121 ms 22356 KB
subtask1_19.txt AC 108 ms 20948 KB
subtask1_20.txt AC 125 ms 23508 KB
subtask1_21.txt AC 128 ms 24148 KB
subtask1_22.txt AC 110 ms 23508 KB
subtask1_23.txt AC 110 ms 20052 KB
subtask1_24.txt AC 119 ms 21588 KB