Submission #832381

# Submission time Handle Problem Language Result Execution time Memory
832381 2023-08-21T09:51:11 Z rainboy Subtree (INOI20_subtree) C
100 / 100
43 ms 15348 KB
#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

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 time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 296 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 296 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 1 ms 296 KB Output is correct
16 Correct 0 ms 296 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 0 ms 292 KB Output is correct
19 Correct 0 ms 292 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
21 Correct 0 ms 296 KB Output is correct
22 Correct 0 ms 296 KB Output is correct
23 Correct 0 ms 212 KB Output is correct
24 Correct 0 ms 292 KB Output is correct
25 Correct 0 ms 212 KB Output is correct
26 Correct 0 ms 212 KB Output is correct
27 Correct 0 ms 212 KB Output is correct
28 Correct 0 ms 212 KB Output is correct
29 Correct 0 ms 212 KB Output is correct
30 Correct 0 ms 212 KB Output is correct
31 Correct 0 ms 212 KB Output is correct
32 Correct 1 ms 212 KB Output is correct
33 Correct 0 ms 212 KB Output is correct
34 Correct 0 ms 212 KB Output is correct
35 Correct 0 ms 212 KB Output is correct
36 Correct 1 ms 212 KB Output is correct
37 Correct 0 ms 212 KB Output is correct
38 Correct 0 ms 212 KB Output is correct
39 Correct 0 ms 212 KB Output is correct
40 Correct 0 ms 296 KB Output is correct
41 Correct 0 ms 212 KB Output is correct
42 Correct 0 ms 212 KB Output is correct
43 Correct 0 ms 212 KB Output is correct
44 Correct 0 ms 292 KB Output is correct
45 Correct 0 ms 212 KB Output is correct
46 Correct 0 ms 296 KB Output is correct
47 Correct 0 ms 212 KB Output is correct
48 Correct 0 ms 212 KB Output is correct
49 Correct 0 ms 212 KB Output is correct
50 Correct 0 ms 212 KB Output is correct
51 Correct 0 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 288 KB Output is correct
2 Correct 3 ms 956 KB Output is correct
3 Correct 28 ms 6888 KB Output is correct
4 Correct 28 ms 6900 KB Output is correct
5 Correct 27 ms 6892 KB Output is correct
6 Correct 27 ms 6844 KB Output is correct
7 Correct 32 ms 13280 KB Output is correct
8 Correct 30 ms 10828 KB Output is correct
9 Correct 31 ms 11036 KB Output is correct
10 Correct 21 ms 7328 KB Output is correct
11 Correct 22 ms 6968 KB Output is correct
12 Correct 26 ms 6652 KB Output is correct
13 Correct 26 ms 6604 KB Output is correct
14 Correct 23 ms 9740 KB Output is correct
15 Correct 30 ms 14360 KB Output is correct
16 Correct 32 ms 15348 KB Output is correct
17 Correct 26 ms 6628 KB Output is correct
18 Correct 26 ms 6628 KB Output is correct
19 Correct 26 ms 6624 KB Output is correct
20 Correct 22 ms 6952 KB Output is correct
21 Correct 21 ms 7264 KB Output is correct
22 Correct 21 ms 7104 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 296 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 296 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 1 ms 296 KB Output is correct
16 Correct 0 ms 296 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 0 ms 292 KB Output is correct
19 Correct 0 ms 292 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
21 Correct 0 ms 296 KB Output is correct
22 Correct 0 ms 296 KB Output is correct
23 Correct 0 ms 212 KB Output is correct
24 Correct 0 ms 292 KB Output is correct
25 Correct 0 ms 212 KB Output is correct
26 Correct 0 ms 212 KB Output is correct
27 Correct 0 ms 212 KB Output is correct
28 Correct 0 ms 212 KB Output is correct
29 Correct 0 ms 212 KB Output is correct
30 Correct 0 ms 212 KB Output is correct
31 Correct 0 ms 212 KB Output is correct
32 Correct 1 ms 212 KB Output is correct
33 Correct 0 ms 212 KB Output is correct
34 Correct 0 ms 212 KB Output is correct
35 Correct 0 ms 212 KB Output is correct
36 Correct 1 ms 212 KB Output is correct
37 Correct 0 ms 212 KB Output is correct
38 Correct 0 ms 212 KB Output is correct
39 Correct 0 ms 212 KB Output is correct
40 Correct 0 ms 296 KB Output is correct
41 Correct 0 ms 212 KB Output is correct
42 Correct 0 ms 212 KB Output is correct
43 Correct 0 ms 212 KB Output is correct
44 Correct 0 ms 292 KB Output is correct
45 Correct 0 ms 212 KB Output is correct
46 Correct 0 ms 296 KB Output is correct
47 Correct 0 ms 212 KB Output is correct
48 Correct 0 ms 212 KB Output is correct
49 Correct 0 ms 212 KB Output is correct
50 Correct 0 ms 212 KB Output is correct
51 Correct 0 ms 212 KB Output is correct
52 Correct 2 ms 596 KB Output is correct
53 Correct 2 ms 564 KB Output is correct
54 Correct 2 ms 564 KB Output is correct
55 Correct 1 ms 596 KB Output is correct
56 Correct 2 ms 820 KB Output is correct
57 Correct 1 ms 596 KB Output is correct
58 Correct 1 ms 596 KB Output is correct
59 Correct 1 ms 724 KB Output is correct
60 Correct 1 ms 596 KB Output is correct
61 Correct 1 ms 564 KB Output is correct
62 Correct 2 ms 564 KB Output is correct
63 Correct 1 ms 564 KB Output is correct
64 Correct 1 ms 596 KB Output is correct
65 Correct 1 ms 564 KB Output is correct
66 Correct 2 ms 596 KB Output is correct
67 Correct 1 ms 596 KB Output is correct
68 Correct 2 ms 724 KB Output is correct
69 Correct 1 ms 596 KB Output is correct
70 Correct 2 ms 596 KB Output is correct
71 Correct 2 ms 572 KB Output is correct
72 Correct 2 ms 564 KB Output is correct
73 Correct 1 ms 572 KB Output is correct
74 Correct 2 ms 596 KB Output is correct
75 Correct 2 ms 696 KB Output is correct
76 Correct 2 ms 564 KB Output is correct
77 Correct 2 ms 824 KB Output is correct
78 Correct 1 ms 596 KB Output is correct
79 Correct 2 ms 724 KB Output is correct
80 Correct 1 ms 596 KB Output is correct
81 Correct 1 ms 596 KB Output is correct
82 Correct 2 ms 596 KB Output is correct
83 Correct 1 ms 596 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 296 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 296 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 1 ms 296 KB Output is correct
16 Correct 0 ms 296 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 0 ms 292 KB Output is correct
19 Correct 0 ms 292 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
21 Correct 0 ms 296 KB Output is correct
22 Correct 0 ms 296 KB Output is correct
23 Correct 0 ms 212 KB Output is correct
24 Correct 0 ms 292 KB Output is correct
25 Correct 0 ms 212 KB Output is correct
26 Correct 0 ms 212 KB Output is correct
27 Correct 0 ms 212 KB Output is correct
28 Correct 0 ms 212 KB Output is correct
29 Correct 0 ms 212 KB Output is correct
30 Correct 0 ms 212 KB Output is correct
31 Correct 0 ms 212 KB Output is correct
32 Correct 1 ms 212 KB Output is correct
33 Correct 0 ms 212 KB Output is correct
34 Correct 0 ms 212 KB Output is correct
35 Correct 0 ms 212 KB Output is correct
36 Correct 1 ms 212 KB Output is correct
37 Correct 0 ms 212 KB Output is correct
38 Correct 0 ms 212 KB Output is correct
39 Correct 0 ms 212 KB Output is correct
40 Correct 0 ms 296 KB Output is correct
41 Correct 0 ms 212 KB Output is correct
42 Correct 0 ms 212 KB Output is correct
43 Correct 0 ms 212 KB Output is correct
44 Correct 0 ms 292 KB Output is correct
45 Correct 0 ms 212 KB Output is correct
46 Correct 0 ms 296 KB Output is correct
47 Correct 0 ms 212 KB Output is correct
48 Correct 0 ms 212 KB Output is correct
49 Correct 0 ms 212 KB Output is correct
50 Correct 0 ms 212 KB Output is correct
51 Correct 0 ms 212 KB Output is correct
52 Correct 0 ms 288 KB Output is correct
53 Correct 3 ms 956 KB Output is correct
54 Correct 28 ms 6888 KB Output is correct
55 Correct 28 ms 6900 KB Output is correct
56 Correct 27 ms 6892 KB Output is correct
57 Correct 27 ms 6844 KB Output is correct
58 Correct 32 ms 13280 KB Output is correct
59 Correct 30 ms 10828 KB Output is correct
60 Correct 31 ms 11036 KB Output is correct
61 Correct 21 ms 7328 KB Output is correct
62 Correct 22 ms 6968 KB Output is correct
63 Correct 26 ms 6652 KB Output is correct
64 Correct 26 ms 6604 KB Output is correct
65 Correct 23 ms 9740 KB Output is correct
66 Correct 30 ms 14360 KB Output is correct
67 Correct 32 ms 15348 KB Output is correct
68 Correct 26 ms 6628 KB Output is correct
69 Correct 26 ms 6628 KB Output is correct
70 Correct 26 ms 6624 KB Output is correct
71 Correct 22 ms 6952 KB Output is correct
72 Correct 21 ms 7264 KB Output is correct
73 Correct 21 ms 7104 KB Output is correct
74 Correct 2 ms 596 KB Output is correct
75 Correct 2 ms 564 KB Output is correct
76 Correct 2 ms 564 KB Output is correct
77 Correct 1 ms 596 KB Output is correct
78 Correct 2 ms 820 KB Output is correct
79 Correct 1 ms 596 KB Output is correct
80 Correct 1 ms 596 KB Output is correct
81 Correct 1 ms 724 KB Output is correct
82 Correct 1 ms 596 KB Output is correct
83 Correct 1 ms 564 KB Output is correct
84 Correct 2 ms 564 KB Output is correct
85 Correct 1 ms 564 KB Output is correct
86 Correct 1 ms 596 KB Output is correct
87 Correct 1 ms 564 KB Output is correct
88 Correct 2 ms 596 KB Output is correct
89 Correct 1 ms 596 KB Output is correct
90 Correct 2 ms 724 KB Output is correct
91 Correct 1 ms 596 KB Output is correct
92 Correct 2 ms 596 KB Output is correct
93 Correct 2 ms 572 KB Output is correct
94 Correct 2 ms 564 KB Output is correct
95 Correct 1 ms 572 KB Output is correct
96 Correct 2 ms 596 KB Output is correct
97 Correct 2 ms 696 KB Output is correct
98 Correct 2 ms 564 KB Output is correct
99 Correct 2 ms 824 KB Output is correct
100 Correct 1 ms 596 KB Output is correct
101 Correct 2 ms 724 KB Output is correct
102 Correct 1 ms 596 KB Output is correct
103 Correct 1 ms 596 KB Output is correct
104 Correct 2 ms 596 KB Output is correct
105 Correct 1 ms 596 KB Output is correct
106 Correct 43 ms 6880 KB Output is correct
107 Correct 30 ms 7064 KB Output is correct
108 Correct 30 ms 7024 KB Output is correct
109 Correct 30 ms 7124 KB Output is correct
110 Correct 21 ms 7132 KB Output is correct
111 Correct 26 ms 7980 KB Output is correct
112 Correct 35 ms 10324 KB Output is correct
113 Correct 28 ms 6632 KB Output is correct
114 Correct 30 ms 6764 KB Output is correct
115 Correct 21 ms 7104 KB Output is correct
116 Correct 30 ms 10852 KB Output is correct
117 Correct 31 ms 10132 KB Output is correct
118 Correct 23 ms 7636 KB Output is correct
119 Correct 29 ms 6716 KB Output is correct
120 Correct 29 ms 6704 KB Output is correct
121 Correct 31 ms 6840 KB Output is correct
122 Correct 33 ms 6860 KB Output is correct
123 Correct 31 ms 7252 KB Output is correct
124 Correct 21 ms 7176 KB Output is correct
125 Correct 20 ms 7120 KB Output is correct
126 Correct 27 ms 6996 KB Output is correct
127 Correct 29 ms 7048 KB Output is correct
128 Correct 38 ms 7116 KB Output is correct
129 Correct 30 ms 7152 KB Output is correct
130 Correct 22 ms 7152 KB Output is correct
131 Correct 28 ms 7884 KB Output is correct
132 Correct 36 ms 10212 KB Output is correct
133 Correct 28 ms 6628 KB Output is correct
134 Correct 32 ms 6776 KB Output is correct
135 Correct 21 ms 7076 KB Output is correct
136 Correct 41 ms 9896 KB Output is correct
137 Correct 32 ms 10048 KB Output is correct
138 Correct 28 ms 7512 KB Output is correct
139 Correct 35 ms 6688 KB Output is correct
140 Correct 30 ms 6752 KB Output is correct
141 Correct 31 ms 6744 KB Output is correct
142 Correct 32 ms 6860 KB Output is correct
143 Correct 22 ms 7096 KB Output is correct
144 Correct 26 ms 7228 KB Output is correct
145 Correct 22 ms 7028 KB Output is correct
146 Correct 29 ms 6732 KB Output is correct
147 Correct 27 ms 6628 KB Output is correct
148 Correct 28 ms 6884 KB Output is correct
149 Correct 26 ms 7628 KB Output is correct
150 Correct 28 ms 11748 KB Output is correct
151 Correct 29 ms 6760 KB Output is correct
152 Correct 34 ms 7032 KB Output is correct
153 Correct 32 ms 7152 KB Output is correct
154 Correct 30 ms 7288 KB Output is correct
155 Correct 22 ms 7300 KB Output is correct
156 Correct 28 ms 9648 KB Output is correct
157 Correct 28 ms 9692 KB Output is correct
158 Correct 28 ms 10124 KB Output is correct
159 Correct 27 ms 6540 KB Output is correct
160 Correct 29 ms 12388 KB Output is correct
161 Correct 27 ms 6644 KB Output is correct
162 Correct 23 ms 7172 KB Output is correct