Submission #832381

#TimeUsernameProblemLanguageResultExecution timeMemory
832381rainboySubtree (INOI20_subtree)C11
100 / 100
43 ms15348 KiB
#include <stdio.h>
#include <stdlib.h>

#define N	100000
#define MD	1000000007

int *ej[N], eo[N];

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

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

char visited[N]; int pp[N];

void dfs1(int p, int i) {
	int o;

	if (visited[i])
		return;
	visited[i] = 1, pp[i] = p;
	for (o = eo[i]; o--; ) {
		int j = ej[i][o];

		if (j != p)
			dfs1(i, j);
	}
}

int dp[N], ans;

void dfs2(int i) {
	static int qu[N], xx[N];
	int cnt, h, o, j, j_, k, q, x, y;

	j_ = -1;
	for (o = eo[i]; o--; ) {
		j = ej[i][o];
		if (j != pp[i] && pp[j] != i) {
			j_ = j;
			break;
		}
	}
	if (j_ == -1) {
		dp[i] = 1;
		for (o = eo[i]; o--; ) {
			j = ej[i][o];
			if (j != pp[i]) {
				dfs2(j);
				dp[i] = (long long) dp[i] * (dp[j] + 1) % MD;
			}
		}
	} else {
		j = j_, q = i;
		while (1) {
			dp[j] = 1;
			for (o = eo[j]; o--; ) {
				k = ej[j][o];
				if (k != pp[j] && k != j_ && k != q) {
					dfs2(k);
					dp[j] = (long long) dp[j] * (dp[k] + 1) % MD;
				}
			}
			if (j == i)
				break;
			q = j, j = pp[j];
		}
		cnt = 0;
		for (j = j_; j != i; j = pp[j])
			qu[cnt++] = j;
		qu[cnt++] = i;
		x = 0;
		for (h = 0; h < cnt - 1; h++) {
			x = (long long) (x + 1) * dp[qu[h]] % MD;
			ans = (ans + x) % MD;
		}
		x = 1, y = 0;
		for (h = 0; h < cnt - 1; h++) {
			xx[h] = x;
			y = (y + x) % MD;
			x = (long long) x * dp[qu[h]] % MD;
		}
		x = dp[i], dp[i] = 0;
		for (h = cnt - 2; h >= 0; h--) {
			dp[i] = (dp[i] + (long long) x * y) % MD;
			x = (long long) x * dp[qu[h]] % MD;
			y = (y - xx[h] + MD) % MD;
		}
	}
	ans = (ans + dp[i]) % MD;
}

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

	scanf("%d%d", &n, &m);
	for (i = 0; i < n; i++)
		ej[i] = (int *) malloc(2 * sizeof *ej[i]);
	for (h = 0; h < m; h++) {
		scanf("%d%d", &i, &j), i--, j--;
		append(i, j), append(j, i);
	}
	dfs1(-1, 0);
	ans = 0, dfs2(0);
	printf("%d\n", ans);
	return 0;
}

Compilation message (stderr)

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