Submission #483435

#TimeUsernameProblemLanguageResultExecution timeMemory
483435rainboyJanjetina (COCI21_janjetina)C11
110 / 110
281 ms17016 KiB
#include <stdio.h>
#include <stdlib.h>

#define N	100000
#define W	100000

int max(int a, int b) { return a > b ? a : b; }

unsigned int X = 12345;

int rand_() {
	return (X *= 3) >> 1;
}

int n, k;
int ij[N - 1], ww[N - 1];
int *eh[N], eo[N];

void append(int i, int h) {
	int o = eo[i]++;

	if (o >= 2 && (o & o - 1) == 0)
		eh[i] = (int *) realloc(eh[i], o * 2 * sizeof *eh[i]);
	eh[i][o] = h;
}

int sz[N], c;

void dfs(int p, int i, int n) {
	int o, centroid;

	sz[i] = 1, centroid = 1;
	for (o = eo[i]; o--; ) {
		int h = eh[i][o], j = i ^ ij[h];

		if (j != p) {
			dfs(i, j, n);
			if (sz[j] * 2 > n)
				centroid = 0;
			sz[i] += sz[j];
		}
	}
	if ((n - sz[i]) * 2 > n)
		centroid = 0;
	if (centroid)
		c = i;
}

void remove_(int i, int h) {
	int o;
	
	for (o = eo[i]; o--; )
		if (eh[i][o] == h) {
			eo[i]--;
			while (o < eo[i])
				eh[i][o] = eh[i][o + 1], o++;
			return;
		}
}

int ft[W + 1];

void update(int i, int x) {
	while (i <= W) {
		ft[i] += x;
		i |= i + 1;
	}
}

int query(int i) {
	int x = 0;

	while (i >= 0) {
		x += ft[i];
		i &= i + 1, i--;
	}
	return x;
}

int ww_[N], ll[N], qu[N], cnt;

void dfs2(int p, int i, int w, int l) {
	int o;

	ww_[i] = w, ll[i] = l, qu[cnt++] = i;
	for (o = eo[i]; o--; ) {
		int h = eh[i][o], j = i ^ ij[h];

		if (j != p)
			dfs2(i, j, max(w, ww[h]), l + 1);
	}
}

void sort(int *ii, int l, int r) {
	while (l < r) {
		int i = l, j = l, k = r, i_ = ii[l + rand_() % (r - l)], tmp;

		while (j < k)
			if (ww_[ii[j]] == ww_[i_])
				j++;
			else if (ww_[ii[j]] < ww_[i_]) {
				tmp = ii[i], ii[i] = ii[j], ii[j] = tmp;
				i++, j++;
			} else {
				k--;
				tmp = ii[j], ii[j] = ii[k], ii[k] = tmp;
			}
		sort(ii, l, i);
		l = k;
	}
}

long long solve(int *ii, int n) {
	int h;
	long long ans;

	sort(ii, 0, n);
	ans = 0;
	for (h = 0; h < n; h++) {
		ans += query(ww_[ii[h]] - ll[ii[h]] - k);
		update(ll[ii[h]], 1);
	}
	for (h = 0; h < n; h++)
		update(ll[ii[h]], -1);
	return ans;
}

long long ans;

void cd(int i, int n) {
	static int qu_[N];
	int o, cnt_;

	dfs(-1, i, n), i = c;
	cnt_ = 0;
	ww_[i] = 0, ll[i] = 0, qu_[cnt_++] = i;
	for (o = eo[i]; o--; ) {
		int h = eh[i][o], j = i ^ ij[h];

		cnt = 0, dfs2(i, j, ww[h], 1);
		ans -= solve(qu, cnt);
		while (cnt--)
			qu_[cnt_++] = qu[cnt];
	}
	ans += solve(qu_, cnt_);
	for (o = eo[i]; o--; ) {
		int h = eh[i][o], j = i ^ ij[h];

		remove_(j, h);
		cd(j, sz[j] < sz[i] ? sz[j] : n - sz[i]);
	}
}

int main() {
	int h, i, j;

	scanf("%d%d", &n, &k);
	for (i = 0; i < n; i++)
		eh[i] = (int *) malloc(2 * sizeof *eh[i]);
	for (h = 0; h < n - 1; h++) {
		scanf("%d%d%d", &i, &j, &ww[h]), i--, j--;
		ij[h] = i ^ j;
		append(i, h), append(j, h);
	}
	cd(0, n);
	ans *= 2;
	printf("%lld\n", ans);
	return 0;
}

Compilation message (stderr)

Main.c: In function 'append':
Main.c:22:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
   22 |  if (o >= 2 && (o & o - 1) == 0)
      |                     ~~^~~
Main.c: In function 'main':
Main.c:157:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  157 |  scanf("%d%d", &n, &k);
      |  ^~~~~~~~~~~~~~~~~~~~~
Main.c:161:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  161 |   scanf("%d%d%d", &i, &j, &ww[h]), i--, j--;
      |   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...