結果

問題 No.901 K-ary εxtrεεmε
ユーザー uwiuwi
提出日時 2019-10-04 22:05:11
言語 Java19
(openjdk 21)
結果
AC  
実行時間 661 ms / 3,000 ms
コード長 10,488 bytes
コンパイル時間 7,200 ms
コンパイル使用メモリ 80,244 KB
実行使用メモリ 100,200 KB
最終ジャッジ日時 2023-07-27 11:55:06
合計ジャッジ時間 21,662 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 402 ms
81,764 KB
testcase_01 AC 46 ms
49,508 KB
testcase_02 AC 72 ms
52,016 KB
testcase_03 AC 72 ms
51,572 KB
testcase_04 AC 73 ms
50,944 KB
testcase_05 AC 65 ms
52,432 KB
testcase_06 AC 74 ms
52,180 KB
testcase_07 AC 567 ms
84,072 KB
testcase_08 AC 574 ms
84,144 KB
testcase_09 AC 567 ms
83,964 KB
testcase_10 AC 582 ms
83,528 KB
testcase_11 AC 568 ms
84,112 KB
testcase_12 AC 539 ms
83,820 KB
testcase_13 AC 542 ms
83,644 KB
testcase_14 AC 547 ms
84,072 KB
testcase_15 AC 585 ms
83,480 KB
testcase_16 AC 551 ms
81,392 KB
testcase_17 AC 624 ms
88,660 KB
testcase_18 AC 657 ms
91,020 KB
testcase_19 AC 639 ms
90,984 KB
testcase_20 AC 627 ms
90,624 KB
testcase_21 AC 615 ms
90,760 KB
testcase_22 AC 573 ms
81,076 KB
testcase_23 AC 589 ms
81,572 KB
testcase_24 AC 564 ms
80,976 KB
testcase_25 AC 608 ms
82,552 KB
testcase_26 AC 578 ms
82,392 KB
testcase_27 AC 658 ms
100,200 KB
testcase_28 AC 649 ms
100,168 KB
testcase_29 AC 661 ms
100,108 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package contest191004;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.InputMismatchException;

public class E {
	InputStream is;
	PrintWriter out;
	String INPUT = "";
	
	void solve()
	{
		int n = ni();
		int[] from = new int[n - 1];
		int[] to = new int[n - 1];
		int[] ws = new int[n-1];
		for (int i = 0; i < n - 1; i++) {
			from[i] = ni();
			to[i] = ni();
			ws[i] = ni();
		}
		int[][][] g = packWU(n, from, to, ws);
		int[][] pars = parents(g, 0);
		int[] par = pars[0], ord = pars[1], dep = pars[2], pw = pars[4];
		int[][] spar = logstepParents(par);
		
		long[] dp = new long[n];
		for(int i = 1;i < n;i++) {
			int cur = ord[i];
			dp[cur] = dp[par[cur]] + pw[cur];
		}
		
		NaturalSubtreeWBuilder nsb = new NaturalSubtreeWBuilder(g);
		for(int Q = ni();Q > 0;Q--) {
			int K = ni();
			int[] x = na(K);
			int[] ords = nsb.build(x);
			int pre = 0;
			long ans = 0;
			int flca = ords[0];
			for(int i = 0;i < ords.length;i++) {
				int lca = lca2(pre, ords[i], spar, dep);
				flca = lca2(flca, ords[i], spar, dep);
				ans += dp[ords[i]] - dp[lca];
				pre = ords[i];
			}
			ans -= dp[flca];
			out.println(ans);
		}
	}
	
	public static class NaturalSubtreeWBuilder {
		public int[] ord, iord; // preorder
		public int[] par, dep, pw;
		public int[][] spar;
		public long[] dw;
		
		public NaturalSubtreeWBuilder(int[][][] g)
		{
			int n = g.length;
			int[][] pars = parents(g, 0);
			par = pars[0];
			ord = pars[1];
			dep = pars[2];
			pw = pars[4];
			dw = new long[n];
			for(int i = 1;i < n;i++){
				dw[ord[i]] = dw[par[ord[i]]] + pw[ord[i]];
			}
			spar = logstepParents(par);
			ord = sortByPreorder(g, 0);
			iord = new int[n];
			for(int i = 0;i < n;i++)iord[ord[i]] = i;
		}
		
		public int[] build(int[] stands)
		{
			int m = stands.length;
			int[] inds = new int[m];
			for(int i = 0;i < m;i++){
				inds[i] = iord[stands[i]];
			}
			Arrays.sort(inds);
			int[] ret = new int[m];
			for(int i = 0;i < m;i++) {
				ret[i] = ord[inds[i]];
			}
			return ret;
		}
		
		public static GraphW parentToG(int[] par, long[] pw)
		{
			int n = par.length;
			int[] ct = new int[n];
			for(int i = 0;i < n;i++){
				if(par[i] >= 0){
					ct[i]++;
					ct[par[i]]++;
				}
			}
			int[][] g = new int[n][];
			long[][] wg = new long[n][];
			for(int i = 0;i < n;i++){
				g[i] = new int[ct[i]];
				wg[i] = new long[ct[i]];
			}
			for(int i = 0;i < n;i++){
				if(par[i] >= 0){
					--ct[par[i]];
					g[par[i]][ct[par[i]]] = i;
					wg[par[i]][ct[par[i]]] = pw[i];
					
					--ct[i];
					g[i][ct[i]] = par[i];
					wg[i][ct[i]] = pw[i];
				}
			}
			return new GraphW(g, wg);
		}
		
		public static class NaturalSubtreeW
		{
			GraphW gw;
			int[] map;
			
			public NaturalSubtreeW(GraphW gw, int[] map) {
				this.gw = gw;
				this.map = map;
			}
		}
		
		public static class GraphW
		{
			public int[][] g;
			public long[][] wg;
			
			public GraphW(int[][] g, long[][] wg) {
				this.g = g;
				this.wg = wg;
			}
		}

		
		static class FilterResult
		{
			int[][] g;
			int[] map;
		}
		
		public static int[] uniq(int[] a)
		{
			int n = a.length;
			int p = 0;
			for(int i = 0;i < n;i++) {
				if(i == 0 || a[i] != a[i-1])a[p++] = a[i];
			}
			return Arrays.copyOf(a, p);
		}
		
		public static int[][] parents(int[][][] g, int root)
		{
			int n = g.length;
			int[] par = new int[n];
			Arrays.fill(par, -1);
			int[] dw = new int[n];
			int[] pw = new int[n];
			int[] dep = new int[n];
			
			int[] q = new int[n];
			q[0] = root;
			for(int p = 0, r = 1;p < r;p++) {
				int cur = q[p];
				for(int[] nex : g[cur]){
					if(par[cur] != nex[0]){
						q[r++] = nex[0];
						par[nex[0]] = cur;
						dep[nex[0]] = dep[cur] + 1;
						dw[nex[0]] = dw[cur] + nex[1];
						pw[nex[0]] = nex[1];
					}
				}
			}
			return new int[][] {par, q, dep, dw, pw};
		}

		
		public static int lca2(int a, int b, int[][] spar, int[] depth) {
			if (depth[a] < depth[b]) {
				b = ancestor(b, depth[b] - depth[a], spar);
			} else if (depth[a] > depth[b]) {
				a = ancestor(a, depth[a] - depth[b], spar);
			}

			if (a == b)
				return a;
			for(int d = 31-Integer.numberOfLeadingZeros(depth[a]);d >= 0;d--){
				if(spar[d][a] != spar[d][b]){
					a = spar[d][a];
					b = spar[d][b];
				}
			}
			return spar[0][a];
		}

		public static int ancestor(int a, int m, int[][] spar) {
			for(;m > 0 && a != -1;m &= m-1)a = spar[Integer.numberOfTrailingZeros(m)][a];
			return a;
		}
		
		// Is shal ancestor of deep?
		public static boolean isAncestor(int shal, int deep, int[][] spar, int[] dep)
		{
			return dep[shal] <= dep[deep] && ancestor(deep, dep[deep] - dep[shal], spar) == shal;
		}
		
		public static int[][] logstepParents(int[] par)
		{
			int n = par.length;
			int m = Integer.numberOfTrailingZeros(Integer.highestOneBit(n-1))+1;
			int[][] pars = new int[m][n];
			pars[0] = par;
			for(int j = 1;j < m;j++){
				for(int i = 0;i < n;i++){
					pars[j][i] = pars[j-1][i] == -1 ? -1 : pars[j-1][pars[j-1][i]];
				}
			}
			return pars;
		}

		
		public static int[] sortByPreorder(int[][][] g, int root){
			int n = g.length;
			int[] stack = new int[n];
			int[] ord = new int[n];
			boolean[] ved = new boolean[n];
			stack[0] = root;
			int p = 1;
			int r = 0;
			ved[root] = true;
			while(p > 0){
				int cur = stack[p-1];
				ord[r++] = cur;
				p--;
				for(int[] e : g[cur]){
					if(!ved[e[0]]){
						ved[e[0]] = true;
						stack[p++] = e[0];
					}
				}
			}
			return ord;
		}
	}

	
	public static int[][] parents(int[][][] g, int root) {
		int n = g.length;
		int[] par = new int[n];
		Arrays.fill(par, -1);
		int[] dw = new int[n];
		int[] pw = new int[n];
		int[] dep = new int[n];

		int[] q = new int[n];
		q[0] = root;
		for (int p = 0, r = 1; p < r; p++) {
			int cur = q[p];
			for (int[] nex : g[cur]) {
				if (par[cur] != nex[0]) {
					q[r++] = nex[0];
					par[nex[0]] = cur;
					dep[nex[0]] = dep[cur] + 1;
					dw[nex[0]] = dw[cur] + nex[1];
					pw[nex[0]] = nex[1];
				}
			}
		}
		return new int[][] { par, q, dep, dw, pw };
	}

	
	public static int[][][] packWU(int n, int[] from, int[] to, int[] w) {
		int[][][] g = new int[n][][];
		int[] p = new int[n];
		for (int f : from)
			p[f]++;
		for (int t : to)
			p[t]++;
		for (int i = 0; i < n; i++)
			g[i] = new int[p[i]][2];
		for (int i = 0; i < from.length; i++) {
			--p[from[i]];
			g[from[i]][p[from[i]]][0] = to[i];
			g[from[i]][p[from[i]]][1] = w[i];
			--p[to[i]];
			g[to[i]][p[to[i]]][0] = from[i];
			g[to[i]][p[to[i]]][1] = w[i];
		}
		return g;
	}

	
	public static int lca2(int a, int b, int[][] spar, int[] depth) {
		if (depth[a] < depth[b]) {
			b = ancestor(b, depth[b] - depth[a], spar);
		} else if (depth[a] > depth[b]) {
			a = ancestor(a, depth[a] - depth[b], spar);
		}

		if (a == b)
			return a;
		int sa = a, sb = b;
		for (int low = 0, high = depth[a], t = Integer.highestOneBit(high), k = Integer
				.numberOfTrailingZeros(t); t > 0; t >>>= 1, k--) {
			if ((low ^ high) >= t) {
				if (spar[k][sa] != spar[k][sb]) {
					low |= t;
					sa = spar[k][sa];
					sb = spar[k][sb];
				} else {
					high = low | t - 1;
				}
			}
		}
		return spar[0][sa];
	}

	protected static int ancestor(int a, int m, int[][] spar) {
		for (int i = 0; m > 0 && a != -1; m >>>= 1, i++) {
			if ((m & 1) == 1)
				a = spar[i][a];
		}
		return a;
	}

	public static int[][] logstepParents(int[] par) {
		int n = par.length;
		int m = Integer.numberOfTrailingZeros(Integer.highestOneBit(n - 1)) + 1;
		int[][] pars = new int[m][n];
		pars[0] = par;
		for (int j = 1; j < m; j++) {
			for (int i = 0; i < n; i++) {
				pars[j][i] = pars[j - 1][i] == -1 ? -1 : pars[j - 1][pars[j - 1][i]];
			}
		}
		return pars;
	}

	
	void run() throws Exception
	{
		is = INPUT.isEmpty() ? System.in : new ByteArrayInputStream(INPUT.getBytes());
		out = new PrintWriter(System.out);
		
		long s = System.currentTimeMillis();
		solve();
		out.flush();
		if(!INPUT.isEmpty())tr(System.currentTimeMillis()-s+"ms");
//		Thread t = new Thread(null, null, "~", Runtime.getRuntime().maxMemory()){
//			@Override
//			public void run() {
//				long s = System.currentTimeMillis();
//				solve();
//				out.flush();
//				if(!INPUT.isEmpty())tr(System.currentTimeMillis()-s+"ms");
//			}
//		};
//		t.start();
//		t.join();
	}
	
	public static void main(String[] args) throws Exception { new E().run(); }
	
	private byte[] inbuf = new byte[1024];
	public int lenbuf = 0, ptrbuf = 0;
	
	private int readByte()
	{
		if(lenbuf == -1)throw new InputMismatchException();
		if(ptrbuf >= lenbuf){
			ptrbuf = 0;
			try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); }
			if(lenbuf <= 0)return -1;
		}
		return inbuf[ptrbuf++];
	}
	
	private boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); }
	private int skip() { int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; }
	
	private double nd() { return Double.parseDouble(ns()); }
	private char nc() { return (char)skip(); }
	
	private String ns()
	{
		int b = skip();
		StringBuilder sb = new StringBuilder();
		while(!(isSpaceChar(b))){ // when nextLine, (isSpaceChar(b) && b != ' ')
			sb.appendCodePoint(b);
			b = readByte();
		}
		return sb.toString();
	}
	
	private char[] ns(int n)
	{
		char[] buf = new char[n];
		int b = skip(), p = 0;
		while(p < n && !(isSpaceChar(b))){
			buf[p++] = (char)b;
			b = readByte();
		}
		return n == p ? buf : Arrays.copyOf(buf, p);
	}
	
	private int[] na(int n)
	{
		int[] a = new int[n];
		for(int i = 0;i < n;i++)a[i] = ni();
		return a;
	}
	
	private long[] nal(int n)
	{
		long[] a = new long[n];
		for(int i = 0;i < n;i++)a[i] = nl();
		return a;
	}
	
	private char[][] nm(int n, int m) {
		char[][] map = new char[n][];
		for(int i = 0;i < n;i++)map[i] = ns(m);
		return map;
	}
	
	private int[][] nmi(int n, int m) {
		int[][] map = new int[n][];
		for(int i = 0;i < n;i++)map[i] = na(m);
		return map;
	}
	
	private int ni() { return (int)nl(); }
	
	private long nl()
	{
		long num = 0;
		int b;
		boolean minus = false;
		while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'));
		if(b == '-'){
			minus = true;
			b = readByte();
		}
		
		while(true){
			if(b >= '0' && b <= '9'){
				num = num * 10 + (b - '0');
			}else{
				return minus ? -num : num;
			}
			b = readByte();
		}
	}
	
	private static void tr(Object... o) { System.out.println(Arrays.deepToString(o)); }
}
0