답안 #1046186

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1046186 2024-08-06T11:20:13 Z javotaz Subtree (INOI20_subtree) C++17
100 / 100
57 ms 34904 KB
// In the Name of Allah
 
#include<bits/stdc++.h>
using namespace std;
 
#pragma GCC optimize("Ofast,unroll-loops,fast-math")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4.1,avx,avx2,sse4.2,popcnt,tune=native")
 
typedef long long ll;
 
#define F first
#define S second
#define pii pair<int, int>
#define pb push_back
#define pp pop_back
#define all(x) x.begin(), x.end()

const int mod = 1e9 + 7, N = 1e5 + 12;
vector<int> g[N], e[N];
int n, m, tc, c[N], h[N];
ll ans, dp[N];
bool mrk[N];

void ip() {
	cin >> n >> m;
	for (int i = 0; i < m; i++) {
		int u, v;
		cin >> u >> v;
		u--, v--;
		g[u].pb(v), g[v].pb(u);
	}
}

pii dfs1(int u, int par = -1) {
	mrk[u] = true;
	pii res = {-1, n};
	for (auto i: g[u])
		if (i != par) {
			if (mrk[i]) { 
				if (!c[i])
					res = {++tc, h[i]};
			}
			else {
				h[i] = h[u] + 1;
				pii tmp = dfs1(i, u);
				if (tmp.S <= h[u])
					res = tmp;
			}
		}
	if (res.S > h[u]) 
		c[u] = ++tc;
	else
		c[u] = res.F;
	e[c[u]].pb(u);
	return res;
}

int bp(int x, int y) {
    if (y < 2) return y? x : 1;
    ll tmp = bp(x, y / 2);
    tmp = tmp * tmp % mod;
    return (y & 1)? tmp * x % mod : tmp;
}

void dfs(int u, int par = -1) {
	vector<ll> v;
	int sz = e[u].size(), t = -1;
	for (auto i: e[u]) {
		ll x = 1;
		for (auto j: g[i])
			if (c[j] != par && c[j] != u) {
				dfs(c[j], u);
				x = (x * (dp[c[j]] + 1)) % mod;
			}
			else if (c[j] == par)
				t = v.size();
		v.pb(x);
	}
	if (t != -1) {
		vector<ll> z(sz), p(sz);
		for (int i = t; i < sz; i++)
			z[i - t] = v[i];
		for (int i = 0; i < t; i++)
			z[sz - t + i] = v[i];
		p[0] = 1;
		for (int j = 1; j < sz - 1; ++j) {
			ll i = z[j];
			p[j] = (p[j - 1] * i) % mod;
		}
		for (int j = 1; j < sz - 1; ++j)
			p[j] = (p[j] + p[j - 1]) % mod;
		reverse(z.begin() + 1, z.end());
		ll x = 1;
		for (int j = 0; j < sz - 1; ++j) {
			ll i = z[j];
			x = (x * i) % mod;
			dp[u] = (dp[u] + x * p[sz - j - 2]) % mod;
		}
		if (sz == 1)
			dp[u] = z[0];
	}
	ll val = 0, x = 1;
	for (int i = 0; i < sz - 1; i++) {
		x = (x * v[i]) % mod;
		val += x;
	}
	val %= mod;
	ans += val;
	for (int i = 1; i < sz; i++) {
		int ls = (i - 2 + sz) % sz;
		val = (val * bp(v[i - 1], mod - 2) + mod - 1) % mod;
		x = (x * bp(v[i - 1], mod - 2)) % mod;
		x = (x * v[ls]) % mod;
		val = (val + x) % mod;
		ans += val;
	}
	ans %= mod;
	if (sz == 1)
		ans = (ans + v[0]) % mod;
}

void solve() {
	dfs1(0);
	dfs(1);
	cout << ans;
}

int32_t main() {
	ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	ip(), solve();
	return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 6492 KB Output is correct
2 Correct 1 ms 6492 KB Output is correct
3 Correct 1 ms 6492 KB Output is correct
4 Correct 1 ms 6492 KB Output is correct
5 Correct 1 ms 6492 KB Output is correct
6 Correct 1 ms 6492 KB Output is correct
7 Correct 1 ms 6492 KB Output is correct
8 Correct 1 ms 6492 KB Output is correct
9 Correct 1 ms 6492 KB Output is correct
10 Correct 1 ms 6492 KB Output is correct
11 Correct 1 ms 6492 KB Output is correct
12 Correct 1 ms 6492 KB Output is correct
13 Correct 1 ms 6492 KB Output is correct
14 Correct 1 ms 6492 KB Output is correct
15 Correct 1 ms 6492 KB Output is correct
16 Correct 1 ms 6492 KB Output is correct
17 Correct 1 ms 6492 KB Output is correct
18 Correct 1 ms 6492 KB Output is correct
19 Correct 1 ms 6492 KB Output is correct
20 Correct 1 ms 6492 KB Output is correct
21 Correct 1 ms 6492 KB Output is correct
22 Correct 1 ms 6492 KB Output is correct
23 Correct 1 ms 6492 KB Output is correct
24 Correct 1 ms 6492 KB Output is correct
25 Correct 1 ms 6492 KB Output is correct
26 Correct 1 ms 6492 KB Output is correct
27 Correct 1 ms 6492 KB Output is correct
28 Correct 1 ms 6492 KB Output is correct
29 Correct 1 ms 6656 KB Output is correct
30 Correct 1 ms 6492 KB Output is correct
31 Correct 1 ms 6492 KB Output is correct
32 Correct 1 ms 6492 KB Output is correct
33 Correct 1 ms 6492 KB Output is correct
34 Correct 1 ms 6492 KB Output is correct
35 Correct 1 ms 6488 KB Output is correct
36 Correct 1 ms 6492 KB Output is correct
37 Correct 1 ms 6488 KB Output is correct
38 Correct 1 ms 6492 KB Output is correct
39 Correct 1 ms 6492 KB Output is correct
40 Correct 1 ms 6588 KB Output is correct
41 Correct 1 ms 6492 KB Output is correct
42 Correct 1 ms 6492 KB Output is correct
43 Correct 1 ms 6492 KB Output is correct
44 Correct 1 ms 6492 KB Output is correct
45 Correct 1 ms 6492 KB Output is correct
46 Correct 1 ms 6492 KB Output is correct
47 Correct 1 ms 6488 KB Output is correct
48 Correct 1 ms 6492 KB Output is correct
49 Correct 1 ms 6492 KB Output is correct
50 Correct 1 ms 6492 KB Output is correct
51 Correct 1 ms 6492 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 6492 KB Output is correct
2 Correct 3 ms 7260 KB Output is correct
3 Correct 30 ms 13084 KB Output is correct
4 Correct 36 ms 13148 KB Output is correct
5 Correct 31 ms 13144 KB Output is correct
6 Correct 52 ms 13168 KB Output is correct
7 Correct 43 ms 28876 KB Output is correct
8 Correct 47 ms 22868 KB Output is correct
9 Correct 41 ms 22932 KB Output is correct
10 Correct 29 ms 13528 KB Output is correct
11 Correct 32 ms 13268 KB Output is correct
12 Correct 34 ms 12892 KB Output is correct
13 Correct 40 ms 13140 KB Output is correct
14 Correct 29 ms 19768 KB Output is correct
15 Correct 42 ms 32732 KB Output is correct
16 Correct 44 ms 34904 KB Output is correct
17 Correct 32 ms 13148 KB Output is correct
18 Correct 51 ms 13140 KB Output is correct
19 Correct 37 ms 13136 KB Output is correct
20 Correct 31 ms 13424 KB Output is correct
21 Correct 31 ms 13444 KB Output is correct
22 Correct 35 ms 13368 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 6492 KB Output is correct
2 Correct 1 ms 6492 KB Output is correct
3 Correct 1 ms 6492 KB Output is correct
4 Correct 1 ms 6492 KB Output is correct
5 Correct 1 ms 6492 KB Output is correct
6 Correct 1 ms 6492 KB Output is correct
7 Correct 1 ms 6492 KB Output is correct
8 Correct 1 ms 6492 KB Output is correct
9 Correct 1 ms 6492 KB Output is correct
10 Correct 1 ms 6492 KB Output is correct
11 Correct 1 ms 6492 KB Output is correct
12 Correct 1 ms 6492 KB Output is correct
13 Correct 1 ms 6492 KB Output is correct
14 Correct 1 ms 6492 KB Output is correct
15 Correct 1 ms 6492 KB Output is correct
16 Correct 1 ms 6492 KB Output is correct
17 Correct 1 ms 6492 KB Output is correct
18 Correct 1 ms 6492 KB Output is correct
19 Correct 1 ms 6492 KB Output is correct
20 Correct 1 ms 6492 KB Output is correct
21 Correct 1 ms 6492 KB Output is correct
22 Correct 1 ms 6492 KB Output is correct
23 Correct 1 ms 6492 KB Output is correct
24 Correct 1 ms 6492 KB Output is correct
25 Correct 1 ms 6492 KB Output is correct
26 Correct 1 ms 6492 KB Output is correct
27 Correct 1 ms 6492 KB Output is correct
28 Correct 1 ms 6492 KB Output is correct
29 Correct 1 ms 6656 KB Output is correct
30 Correct 1 ms 6492 KB Output is correct
31 Correct 1 ms 6492 KB Output is correct
32 Correct 1 ms 6492 KB Output is correct
33 Correct 1 ms 6492 KB Output is correct
34 Correct 1 ms 6492 KB Output is correct
35 Correct 1 ms 6488 KB Output is correct
36 Correct 1 ms 6492 KB Output is correct
37 Correct 1 ms 6488 KB Output is correct
38 Correct 1 ms 6492 KB Output is correct
39 Correct 1 ms 6492 KB Output is correct
40 Correct 1 ms 6588 KB Output is correct
41 Correct 1 ms 6492 KB Output is correct
42 Correct 1 ms 6492 KB Output is correct
43 Correct 1 ms 6492 KB Output is correct
44 Correct 1 ms 6492 KB Output is correct
45 Correct 1 ms 6492 KB Output is correct
46 Correct 1 ms 6492 KB Output is correct
47 Correct 1 ms 6488 KB Output is correct
48 Correct 1 ms 6492 KB Output is correct
49 Correct 1 ms 6492 KB Output is correct
50 Correct 1 ms 6492 KB Output is correct
51 Correct 1 ms 6492 KB Output is correct
52 Correct 2 ms 7004 KB Output is correct
53 Correct 2 ms 6748 KB Output is correct
54 Correct 3 ms 6748 KB Output is correct
55 Correct 2 ms 6972 KB Output is correct
56 Correct 3 ms 7256 KB Output is correct
57 Correct 2 ms 7004 KB Output is correct
58 Correct 3 ms 7004 KB Output is correct
59 Correct 3 ms 7172 KB Output is correct
60 Correct 2 ms 7004 KB Output is correct
61 Correct 4 ms 6748 KB Output is correct
62 Correct 3 ms 6932 KB Output is correct
63 Correct 3 ms 7020 KB Output is correct
64 Correct 3 ms 7004 KB Output is correct
65 Correct 2 ms 6748 KB Output is correct
66 Correct 2 ms 6876 KB Output is correct
67 Correct 2 ms 7004 KB Output is correct
68 Correct 3 ms 7004 KB Output is correct
69 Correct 3 ms 6872 KB Output is correct
70 Correct 2 ms 6836 KB Output is correct
71 Correct 2 ms 6748 KB Output is correct
72 Correct 3 ms 6748 KB Output is correct
73 Correct 2 ms 7004 KB Output is correct
74 Correct 2 ms 6748 KB Output is correct
75 Correct 3 ms 7004 KB Output is correct
76 Correct 3 ms 6748 KB Output is correct
77 Correct 3 ms 7260 KB Output is correct
78 Correct 2 ms 6748 KB Output is correct
79 Correct 3 ms 7004 KB Output is correct
80 Correct 2 ms 6748 KB Output is correct
81 Correct 2 ms 7004 KB Output is correct
82 Correct 2 ms 7004 KB Output is correct
83 Correct 3 ms 6984 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 6492 KB Output is correct
2 Correct 1 ms 6492 KB Output is correct
3 Correct 1 ms 6492 KB Output is correct
4 Correct 1 ms 6492 KB Output is correct
5 Correct 1 ms 6492 KB Output is correct
6 Correct 1 ms 6492 KB Output is correct
7 Correct 1 ms 6492 KB Output is correct
8 Correct 1 ms 6492 KB Output is correct
9 Correct 1 ms 6492 KB Output is correct
10 Correct 1 ms 6492 KB Output is correct
11 Correct 1 ms 6492 KB Output is correct
12 Correct 1 ms 6492 KB Output is correct
13 Correct 1 ms 6492 KB Output is correct
14 Correct 1 ms 6492 KB Output is correct
15 Correct 1 ms 6492 KB Output is correct
16 Correct 1 ms 6492 KB Output is correct
17 Correct 1 ms 6492 KB Output is correct
18 Correct 1 ms 6492 KB Output is correct
19 Correct 1 ms 6492 KB Output is correct
20 Correct 1 ms 6492 KB Output is correct
21 Correct 1 ms 6492 KB Output is correct
22 Correct 1 ms 6492 KB Output is correct
23 Correct 1 ms 6492 KB Output is correct
24 Correct 1 ms 6492 KB Output is correct
25 Correct 1 ms 6492 KB Output is correct
26 Correct 1 ms 6492 KB Output is correct
27 Correct 1 ms 6492 KB Output is correct
28 Correct 1 ms 6492 KB Output is correct
29 Correct 1 ms 6656 KB Output is correct
30 Correct 1 ms 6492 KB Output is correct
31 Correct 1 ms 6492 KB Output is correct
32 Correct 1 ms 6492 KB Output is correct
33 Correct 1 ms 6492 KB Output is correct
34 Correct 1 ms 6492 KB Output is correct
35 Correct 1 ms 6488 KB Output is correct
36 Correct 1 ms 6492 KB Output is correct
37 Correct 1 ms 6488 KB Output is correct
38 Correct 1 ms 6492 KB Output is correct
39 Correct 1 ms 6492 KB Output is correct
40 Correct 1 ms 6588 KB Output is correct
41 Correct 1 ms 6492 KB Output is correct
42 Correct 1 ms 6492 KB Output is correct
43 Correct 1 ms 6492 KB Output is correct
44 Correct 1 ms 6492 KB Output is correct
45 Correct 1 ms 6492 KB Output is correct
46 Correct 1 ms 6492 KB Output is correct
47 Correct 1 ms 6488 KB Output is correct
48 Correct 1 ms 6492 KB Output is correct
49 Correct 1 ms 6492 KB Output is correct
50 Correct 1 ms 6492 KB Output is correct
51 Correct 1 ms 6492 KB Output is correct
52 Correct 1 ms 6492 KB Output is correct
53 Correct 3 ms 7260 KB Output is correct
54 Correct 30 ms 13084 KB Output is correct
55 Correct 36 ms 13148 KB Output is correct
56 Correct 31 ms 13144 KB Output is correct
57 Correct 52 ms 13168 KB Output is correct
58 Correct 43 ms 28876 KB Output is correct
59 Correct 47 ms 22868 KB Output is correct
60 Correct 41 ms 22932 KB Output is correct
61 Correct 29 ms 13528 KB Output is correct
62 Correct 32 ms 13268 KB Output is correct
63 Correct 34 ms 12892 KB Output is correct
64 Correct 40 ms 13140 KB Output is correct
65 Correct 29 ms 19768 KB Output is correct
66 Correct 42 ms 32732 KB Output is correct
67 Correct 44 ms 34904 KB Output is correct
68 Correct 32 ms 13148 KB Output is correct
69 Correct 51 ms 13140 KB Output is correct
70 Correct 37 ms 13136 KB Output is correct
71 Correct 31 ms 13424 KB Output is correct
72 Correct 31 ms 13444 KB Output is correct
73 Correct 35 ms 13368 KB Output is correct
74 Correct 2 ms 7004 KB Output is correct
75 Correct 2 ms 6748 KB Output is correct
76 Correct 3 ms 6748 KB Output is correct
77 Correct 2 ms 6972 KB Output is correct
78 Correct 3 ms 7256 KB Output is correct
79 Correct 2 ms 7004 KB Output is correct
80 Correct 3 ms 7004 KB Output is correct
81 Correct 3 ms 7172 KB Output is correct
82 Correct 2 ms 7004 KB Output is correct
83 Correct 4 ms 6748 KB Output is correct
84 Correct 3 ms 6932 KB Output is correct
85 Correct 3 ms 7020 KB Output is correct
86 Correct 3 ms 7004 KB Output is correct
87 Correct 2 ms 6748 KB Output is correct
88 Correct 2 ms 6876 KB Output is correct
89 Correct 2 ms 7004 KB Output is correct
90 Correct 3 ms 7004 KB Output is correct
91 Correct 3 ms 6872 KB Output is correct
92 Correct 2 ms 6836 KB Output is correct
93 Correct 2 ms 6748 KB Output is correct
94 Correct 3 ms 6748 KB Output is correct
95 Correct 2 ms 7004 KB Output is correct
96 Correct 2 ms 6748 KB Output is correct
97 Correct 3 ms 7004 KB Output is correct
98 Correct 3 ms 6748 KB Output is correct
99 Correct 3 ms 7260 KB Output is correct
100 Correct 2 ms 6748 KB Output is correct
101 Correct 3 ms 7004 KB Output is correct
102 Correct 2 ms 6748 KB Output is correct
103 Correct 2 ms 7004 KB Output is correct
104 Correct 2 ms 7004 KB Output is correct
105 Correct 3 ms 6984 KB Output is correct
106 Correct 36 ms 12920 KB Output is correct
107 Correct 37 ms 12628 KB Output is correct
108 Correct 49 ms 12376 KB Output is correct
109 Correct 38 ms 12380 KB Output is correct
110 Correct 30 ms 13400 KB Output is correct
111 Correct 31 ms 14068 KB Output is correct
112 Correct 45 ms 18072 KB Output is correct
113 Correct 41 ms 11856 KB Output is correct
114 Correct 37 ms 12124 KB Output is correct
115 Correct 25 ms 13268 KB Output is correct
116 Correct 52 ms 21240 KB Output is correct
117 Correct 45 ms 18772 KB Output is correct
118 Correct 29 ms 14032 KB Output is correct
119 Correct 38 ms 11860 KB Output is correct
120 Correct 40 ms 11428 KB Output is correct
121 Correct 42 ms 11344 KB Output is correct
122 Correct 46 ms 11340 KB Output is correct
123 Correct 29 ms 13560 KB Output is correct
124 Correct 34 ms 13648 KB Output is correct
125 Correct 30 ms 13428 KB Output is correct
126 Correct 34 ms 12892 KB Output is correct
127 Correct 34 ms 12632 KB Output is correct
128 Correct 35 ms 12544 KB Output is correct
129 Correct 36 ms 12248 KB Output is correct
130 Correct 30 ms 13400 KB Output is correct
131 Correct 57 ms 14128 KB Output is correct
132 Correct 47 ms 17756 KB Output is correct
133 Correct 38 ms 11868 KB Output is correct
134 Correct 40 ms 12088 KB Output is correct
135 Correct 25 ms 13268 KB Output is correct
136 Correct 42 ms 18768 KB Output is correct
137 Correct 52 ms 18256 KB Output is correct
138 Correct 27 ms 14036 KB Output is correct
139 Correct 37 ms 11684 KB Output is correct
140 Correct 40 ms 11608 KB Output is correct
141 Correct 45 ms 11344 KB Output is correct
142 Correct 43 ms 11088 KB Output is correct
143 Correct 27 ms 13384 KB Output is correct
144 Correct 30 ms 13404 KB Output is correct
145 Correct 27 ms 13476 KB Output is correct
146 Correct 42 ms 10588 KB Output is correct
147 Correct 40 ms 10588 KB Output is correct
148 Correct 40 ms 10840 KB Output is correct
149 Correct 44 ms 11996 KB Output is correct
150 Correct 41 ms 19792 KB Output is correct
151 Correct 44 ms 12112 KB Output is correct
152 Correct 54 ms 12456 KB Output is correct
153 Correct 48 ms 12628 KB Output is correct
154 Correct 38 ms 14044 KB Output is correct
155 Correct 29 ms 14428 KB Output is correct
156 Correct 43 ms 17668 KB Output is correct
157 Correct 43 ms 17012 KB Output is correct
158 Correct 42 ms 16312 KB Output is correct
159 Correct 32 ms 13984 KB Output is correct
160 Correct 40 ms 26960 KB Output is correct
161 Correct 34 ms 13916 KB Output is correct
162 Correct 31 ms 14420 KB Output is correct