Submission #634712

# Submission time Handle Problem Language Result Execution time Memory
634712 2022-08-24T17:41:30 Z S2speed Subtree (INOI20_subtree) C++17
100 / 100
127 ms 35456 KB
#include<bits/stdc++.h>

using namespace std;

#pragma GCC optimize ("Ofast")

#define all(x) x.begin() , x.end()
#define sze(x) (ll)(x.size())
#define mp(x , y) make_pair(x , y)
#define wall cerr<<"--------------------------------------"<<endl
typedef long long int ll;
typedef pair<ll , ll> pll;
typedef pair<int , int> pii;
typedef long double db;
typedef pair<pll , ll> plll;
typedef pair<int , pii> piii;
typedef pair<pll , pll> pllll;

const ll maxn = 2e5 + 17 , md = 1e9 + 7 , inf = 2e16;

ll n , m , ans;
bitset<maxn> mark;
vector<ll> adj[maxn] , cc[maxn] , st , del;
vector<pll> cdj[maxn];
ll c[maxn] , o , dp[maxn] , ps[maxn] , pz[maxn];

void cDFS(ll r , ll par){
	st.push_back(r);
	mark[r] = true;
	for(auto i : adj[r]){
		if(i == par) continue;
		if(mark[i]){
			if(c[r] != -1) continue;
			while(st.back() != i){
				c[st.back()] = o;
				cc[o].push_back(st.back());
				del.push_back(st.back());
				st.pop_back();
			}
			c[i] = o;
			cc[o].push_back(i);
			o++;
			while(!del.empty()){
				st.push_back(del.back());
				del.pop_back();
			}
			continue;
		}
		cDFS(i , r);
	}
	st.pop_back();
	return;
}

ll cal(vector<ll> &v , ll l , ll r){
	if(l >= r) return 0;
	if(r - l == 1) return v[l];
	ll m = (r + l) >> 1;
	ll a = 1 , b = 1 , h = 1;
	for(ll i = m - 1 ; i >= l ; i--){
		h *= v[i]; h %= md;
		a += h;
	}
	h = 1;
	for(ll i = m + 1 ; i < r ; i++){
		h *= v[i]; h %= md;
		b += h;
	}
	a %= md; b %= md;
	ll res = cal(v , l , m) + cal(v , m + 1 , r) + a * b % md * v[m];
	return res % md;
}

void dDFS(ll r , ll t , ll par , bool f = false){
//	cout<<r<<' '<<t<<' '<<par<<' '<<f<<endl;
	if(r < n){
		ll h = 1;
		for(auto p : cdj[r]){
			ll i = p.first , u = p.second;
			if(i == par) continue;
			dDFS(i , u , r);
			h *= dp[i] + 1; h %= md;
		}
		dp[r] = h;
		ans += h;
		return;
	}
	swap(r , t);
	if(f){
		ll h = 1;
		for(auto i : adj[r]){
			ll u = (c[i] == -1 ? i : c[i]);
			if(u == t || u == par) continue;
			dDFS(u , i , t);
			h *= dp[u] + 1; h %= md;
		}
		dp[r] = h;
		return;
	}
	ll ind = -1 , sz = sze(cc[t]);
	for(ll i = 0 ; i < sz ; i++){
		if(cc[t][i] == r){
			ind = i;
			break;
		}
	}
	vector<ll> v;
	for(ll i = ind + 1 ; i < sz ; i++){
		dDFS(t , cc[t][i] , par , true);
		v.push_back(dp[cc[t][i]]);
	}
	for(ll i = 0 ; i < ind ; i++){
		dDFS(t , cc[t][i] , par , true);
		v.push_back(dp[cc[t][i]]);
	}
	ll vs = sz - 1 , z;
	dDFS(t , r , par , true);
	z = dp[r];
	ps[0] = v[0] + 1;
	pz[0] = v[0];
	for(ll i = 1 ; i < vs ; i++){
		pz[i] = pz[i - 1] * v[i] % md;
		ps[i] = ps[i - 1] + pz[i]; ps[i] -= (ps[i] >= md) * md;
	}
	ll cur = 1 , res = 0;
	for(ll i = vs - 1 ; i > 0 ; i--){
		ll h = cur * ps[i - 1] % md;
		res += h;
		cur *= v[i]; cur %= md;
	}
	res += cur;
	dp[t] = res % md * z % md;
	ans += dp[t];
	ans += cal(v , 0 , vs);
	return;
}

/*
9 11
1 2 2 3 3 1
4 5 5 6 6 4
7 8 8 9 9 7
1 4 1 7
*/

int main(){
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);

	memset(c , -1 , sizeof(c));
	cin>>n>>m; o = n;
	for(ll i = 0 ; i < m ; i++){
		ll v , u;
		cin>>v>>u; v--; u--;
		adj[v].push_back(u); adj[u].push_back(v);
	}
	cDFS(0 , -1);
	for(ll i = 0 ; i < n ; i++){
		ll v = (c[i] == -1 ? i : c[i]);
		for(auto j : adj[i]){
			ll u = (c[j] == -1 ? j : c[j]);
			if(v == u) continue;
			cdj[v].push_back({u , j});
		}
	}
	dDFS((c[0] == -1 ? 0 : c[0]) , 0 , -1);
	ans %= md;
	cout<<ans<<'\n';
	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 11 ms 15880 KB Output is correct
2 Correct 9 ms 15960 KB Output is correct
3 Correct 11 ms 15956 KB Output is correct
4 Correct 9 ms 15956 KB Output is correct
5 Correct 8 ms 15956 KB Output is correct
6 Correct 8 ms 15976 KB Output is correct
7 Correct 13 ms 15956 KB Output is correct
8 Correct 14 ms 15928 KB Output is correct
9 Correct 8 ms 15956 KB Output is correct
10 Correct 8 ms 15956 KB Output is correct
11 Correct 9 ms 15932 KB Output is correct
12 Correct 9 ms 15956 KB Output is correct
13 Correct 8 ms 15956 KB Output is correct
14 Correct 9 ms 15956 KB Output is correct
15 Correct 8 ms 15992 KB Output is correct
16 Correct 14 ms 15956 KB Output is correct
17 Correct 8 ms 15876 KB Output is correct
18 Correct 10 ms 15956 KB Output is correct
19 Correct 11 ms 15908 KB Output is correct
20 Correct 10 ms 15956 KB Output is correct
21 Correct 10 ms 15956 KB Output is correct
22 Correct 10 ms 15956 KB Output is correct
23 Correct 10 ms 15904 KB Output is correct
24 Correct 9 ms 16000 KB Output is correct
25 Correct 12 ms 15952 KB Output is correct
26 Correct 8 ms 15992 KB Output is correct
27 Correct 8 ms 15956 KB Output is correct
28 Correct 9 ms 15956 KB Output is correct
29 Correct 9 ms 15956 KB Output is correct
30 Correct 9 ms 15944 KB Output is correct
31 Correct 15 ms 15956 KB Output is correct
32 Correct 10 ms 15968 KB Output is correct
33 Correct 11 ms 15956 KB Output is correct
34 Correct 14 ms 15956 KB Output is correct
35 Correct 13 ms 15956 KB Output is correct
36 Correct 8 ms 15956 KB Output is correct
37 Correct 10 ms 15940 KB Output is correct
38 Correct 9 ms 15956 KB Output is correct
39 Correct 13 ms 15956 KB Output is correct
40 Correct 9 ms 15972 KB Output is correct
41 Correct 13 ms 15940 KB Output is correct
42 Correct 8 ms 15956 KB Output is correct
43 Correct 8 ms 15956 KB Output is correct
44 Correct 9 ms 15896 KB Output is correct
45 Correct 9 ms 15956 KB Output is correct
46 Correct 11 ms 15932 KB Output is correct
47 Correct 9 ms 15956 KB Output is correct
48 Correct 12 ms 15992 KB Output is correct
49 Correct 8 ms 15872 KB Output is correct
50 Correct 9 ms 15956 KB Output is correct
51 Correct 9 ms 15956 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 15956 KB Output is correct
2 Correct 13 ms 16852 KB Output is correct
3 Correct 92 ms 25600 KB Output is correct
4 Correct 92 ms 25532 KB Output is correct
5 Correct 88 ms 25660 KB Output is correct
6 Correct 63 ms 25608 KB Output is correct
7 Correct 87 ms 33316 KB Output is correct
8 Correct 103 ms 30684 KB Output is correct
9 Correct 79 ms 30932 KB Output is correct
10 Correct 64 ms 26060 KB Output is correct
11 Correct 64 ms 25040 KB Output is correct
12 Correct 72 ms 24676 KB Output is correct
13 Correct 70 ms 24592 KB Output is correct
14 Correct 74 ms 28648 KB Output is correct
15 Correct 117 ms 34428 KB Output is correct
16 Correct 100 ms 35456 KB Output is correct
17 Correct 76 ms 24688 KB Output is correct
18 Correct 72 ms 24596 KB Output is correct
19 Correct 99 ms 24700 KB Output is correct
20 Correct 63 ms 25292 KB Output is correct
21 Correct 61 ms 25552 KB Output is correct
22 Correct 70 ms 25344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 15880 KB Output is correct
2 Correct 9 ms 15960 KB Output is correct
3 Correct 11 ms 15956 KB Output is correct
4 Correct 9 ms 15956 KB Output is correct
5 Correct 8 ms 15956 KB Output is correct
6 Correct 8 ms 15976 KB Output is correct
7 Correct 13 ms 15956 KB Output is correct
8 Correct 14 ms 15928 KB Output is correct
9 Correct 8 ms 15956 KB Output is correct
10 Correct 8 ms 15956 KB Output is correct
11 Correct 9 ms 15932 KB Output is correct
12 Correct 9 ms 15956 KB Output is correct
13 Correct 8 ms 15956 KB Output is correct
14 Correct 9 ms 15956 KB Output is correct
15 Correct 8 ms 15992 KB Output is correct
16 Correct 14 ms 15956 KB Output is correct
17 Correct 8 ms 15876 KB Output is correct
18 Correct 10 ms 15956 KB Output is correct
19 Correct 11 ms 15908 KB Output is correct
20 Correct 10 ms 15956 KB Output is correct
21 Correct 10 ms 15956 KB Output is correct
22 Correct 10 ms 15956 KB Output is correct
23 Correct 10 ms 15904 KB Output is correct
24 Correct 9 ms 16000 KB Output is correct
25 Correct 12 ms 15952 KB Output is correct
26 Correct 8 ms 15992 KB Output is correct
27 Correct 8 ms 15956 KB Output is correct
28 Correct 9 ms 15956 KB Output is correct
29 Correct 9 ms 15956 KB Output is correct
30 Correct 9 ms 15944 KB Output is correct
31 Correct 15 ms 15956 KB Output is correct
32 Correct 10 ms 15968 KB Output is correct
33 Correct 11 ms 15956 KB Output is correct
34 Correct 14 ms 15956 KB Output is correct
35 Correct 13 ms 15956 KB Output is correct
36 Correct 8 ms 15956 KB Output is correct
37 Correct 10 ms 15940 KB Output is correct
38 Correct 9 ms 15956 KB Output is correct
39 Correct 13 ms 15956 KB Output is correct
40 Correct 9 ms 15972 KB Output is correct
41 Correct 13 ms 15940 KB Output is correct
42 Correct 8 ms 15956 KB Output is correct
43 Correct 8 ms 15956 KB Output is correct
44 Correct 9 ms 15896 KB Output is correct
45 Correct 9 ms 15956 KB Output is correct
46 Correct 11 ms 15932 KB Output is correct
47 Correct 9 ms 15956 KB Output is correct
48 Correct 12 ms 15992 KB Output is correct
49 Correct 8 ms 15872 KB Output is correct
50 Correct 9 ms 15956 KB Output is correct
51 Correct 9 ms 15956 KB Output is correct
52 Correct 12 ms 16468 KB Output is correct
53 Correct 15 ms 16468 KB Output is correct
54 Correct 11 ms 16340 KB Output is correct
55 Correct 11 ms 16500 KB Output is correct
56 Correct 13 ms 16768 KB Output is correct
57 Correct 10 ms 16468 KB Output is correct
58 Correct 16 ms 16492 KB Output is correct
59 Correct 11 ms 16556 KB Output is correct
60 Correct 11 ms 16468 KB Output is correct
61 Correct 17 ms 16352 KB Output is correct
62 Correct 12 ms 16352 KB Output is correct
63 Correct 12 ms 16468 KB Output is correct
64 Correct 11 ms 16468 KB Output is correct
65 Correct 14 ms 16476 KB Output is correct
66 Correct 13 ms 16468 KB Output is correct
67 Correct 10 ms 16468 KB Output is correct
68 Correct 16 ms 16596 KB Output is correct
69 Correct 15 ms 16312 KB Output is correct
70 Correct 10 ms 16468 KB Output is correct
71 Correct 10 ms 16340 KB Output is correct
72 Correct 11 ms 16340 KB Output is correct
73 Correct 15 ms 16596 KB Output is correct
74 Correct 11 ms 16212 KB Output is correct
75 Correct 14 ms 16496 KB Output is correct
76 Correct 14 ms 16340 KB Output is correct
77 Correct 11 ms 16676 KB Output is correct
78 Correct 12 ms 16404 KB Output is correct
79 Correct 14 ms 16644 KB Output is correct
80 Correct 14 ms 16320 KB Output is correct
81 Correct 13 ms 16380 KB Output is correct
82 Correct 14 ms 16412 KB Output is correct
83 Correct 13 ms 16404 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 15880 KB Output is correct
2 Correct 9 ms 15960 KB Output is correct
3 Correct 11 ms 15956 KB Output is correct
4 Correct 9 ms 15956 KB Output is correct
5 Correct 8 ms 15956 KB Output is correct
6 Correct 8 ms 15976 KB Output is correct
7 Correct 13 ms 15956 KB Output is correct
8 Correct 14 ms 15928 KB Output is correct
9 Correct 8 ms 15956 KB Output is correct
10 Correct 8 ms 15956 KB Output is correct
11 Correct 9 ms 15932 KB Output is correct
12 Correct 9 ms 15956 KB Output is correct
13 Correct 8 ms 15956 KB Output is correct
14 Correct 9 ms 15956 KB Output is correct
15 Correct 8 ms 15992 KB Output is correct
16 Correct 14 ms 15956 KB Output is correct
17 Correct 8 ms 15876 KB Output is correct
18 Correct 10 ms 15956 KB Output is correct
19 Correct 11 ms 15908 KB Output is correct
20 Correct 10 ms 15956 KB Output is correct
21 Correct 10 ms 15956 KB Output is correct
22 Correct 10 ms 15956 KB Output is correct
23 Correct 10 ms 15904 KB Output is correct
24 Correct 9 ms 16000 KB Output is correct
25 Correct 12 ms 15952 KB Output is correct
26 Correct 8 ms 15992 KB Output is correct
27 Correct 8 ms 15956 KB Output is correct
28 Correct 9 ms 15956 KB Output is correct
29 Correct 9 ms 15956 KB Output is correct
30 Correct 9 ms 15944 KB Output is correct
31 Correct 15 ms 15956 KB Output is correct
32 Correct 10 ms 15968 KB Output is correct
33 Correct 11 ms 15956 KB Output is correct
34 Correct 14 ms 15956 KB Output is correct
35 Correct 13 ms 15956 KB Output is correct
36 Correct 8 ms 15956 KB Output is correct
37 Correct 10 ms 15940 KB Output is correct
38 Correct 9 ms 15956 KB Output is correct
39 Correct 13 ms 15956 KB Output is correct
40 Correct 9 ms 15972 KB Output is correct
41 Correct 13 ms 15940 KB Output is correct
42 Correct 8 ms 15956 KB Output is correct
43 Correct 8 ms 15956 KB Output is correct
44 Correct 9 ms 15896 KB Output is correct
45 Correct 9 ms 15956 KB Output is correct
46 Correct 11 ms 15932 KB Output is correct
47 Correct 9 ms 15956 KB Output is correct
48 Correct 12 ms 15992 KB Output is correct
49 Correct 8 ms 15872 KB Output is correct
50 Correct 9 ms 15956 KB Output is correct
51 Correct 9 ms 15956 KB Output is correct
52 Correct 10 ms 15956 KB Output is correct
53 Correct 13 ms 16852 KB Output is correct
54 Correct 92 ms 25600 KB Output is correct
55 Correct 92 ms 25532 KB Output is correct
56 Correct 88 ms 25660 KB Output is correct
57 Correct 63 ms 25608 KB Output is correct
58 Correct 87 ms 33316 KB Output is correct
59 Correct 103 ms 30684 KB Output is correct
60 Correct 79 ms 30932 KB Output is correct
61 Correct 64 ms 26060 KB Output is correct
62 Correct 64 ms 25040 KB Output is correct
63 Correct 72 ms 24676 KB Output is correct
64 Correct 70 ms 24592 KB Output is correct
65 Correct 74 ms 28648 KB Output is correct
66 Correct 117 ms 34428 KB Output is correct
67 Correct 100 ms 35456 KB Output is correct
68 Correct 76 ms 24688 KB Output is correct
69 Correct 72 ms 24596 KB Output is correct
70 Correct 99 ms 24700 KB Output is correct
71 Correct 63 ms 25292 KB Output is correct
72 Correct 61 ms 25552 KB Output is correct
73 Correct 70 ms 25344 KB Output is correct
74 Correct 12 ms 16468 KB Output is correct
75 Correct 15 ms 16468 KB Output is correct
76 Correct 11 ms 16340 KB Output is correct
77 Correct 11 ms 16500 KB Output is correct
78 Correct 13 ms 16768 KB Output is correct
79 Correct 10 ms 16468 KB Output is correct
80 Correct 16 ms 16492 KB Output is correct
81 Correct 11 ms 16556 KB Output is correct
82 Correct 11 ms 16468 KB Output is correct
83 Correct 17 ms 16352 KB Output is correct
84 Correct 12 ms 16352 KB Output is correct
85 Correct 12 ms 16468 KB Output is correct
86 Correct 11 ms 16468 KB Output is correct
87 Correct 14 ms 16476 KB Output is correct
88 Correct 13 ms 16468 KB Output is correct
89 Correct 10 ms 16468 KB Output is correct
90 Correct 16 ms 16596 KB Output is correct
91 Correct 15 ms 16312 KB Output is correct
92 Correct 10 ms 16468 KB Output is correct
93 Correct 10 ms 16340 KB Output is correct
94 Correct 11 ms 16340 KB Output is correct
95 Correct 15 ms 16596 KB Output is correct
96 Correct 11 ms 16212 KB Output is correct
97 Correct 14 ms 16496 KB Output is correct
98 Correct 14 ms 16340 KB Output is correct
99 Correct 11 ms 16676 KB Output is correct
100 Correct 12 ms 16404 KB Output is correct
101 Correct 14 ms 16644 KB Output is correct
102 Correct 14 ms 16320 KB Output is correct
103 Correct 13 ms 16380 KB Output is correct
104 Correct 14 ms 16412 KB Output is correct
105 Correct 13 ms 16404 KB Output is correct
106 Correct 100 ms 25408 KB Output is correct
107 Correct 78 ms 25216 KB Output is correct
108 Correct 102 ms 25364 KB Output is correct
109 Correct 105 ms 25268 KB Output is correct
110 Correct 54 ms 25604 KB Output is correct
111 Correct 69 ms 27120 KB Output is correct
112 Correct 106 ms 29824 KB Output is correct
113 Correct 111 ms 23264 KB Output is correct
114 Correct 108 ms 24256 KB Output is correct
115 Correct 62 ms 25304 KB Output is correct
116 Correct 87 ms 30144 KB Output is correct
117 Correct 122 ms 29312 KB Output is correct
118 Correct 80 ms 26008 KB Output is correct
119 Correct 76 ms 23184 KB Output is correct
120 Correct 114 ms 23660 KB Output is correct
121 Correct 106 ms 23952 KB Output is correct
122 Correct 97 ms 24212 KB Output is correct
123 Correct 84 ms 26036 KB Output is correct
124 Correct 56 ms 25564 KB Output is correct
125 Correct 56 ms 25336 KB Output is correct
126 Correct 92 ms 25420 KB Output is correct
127 Correct 119 ms 25292 KB Output is correct
128 Correct 110 ms 25260 KB Output is correct
129 Correct 96 ms 25148 KB Output is correct
130 Correct 58 ms 25572 KB Output is correct
131 Correct 88 ms 27076 KB Output is correct
132 Correct 125 ms 29700 KB Output is correct
133 Correct 92 ms 23116 KB Output is correct
134 Correct 81 ms 24196 KB Output is correct
135 Correct 68 ms 25272 KB Output is correct
136 Correct 117 ms 28436 KB Output is correct
137 Correct 127 ms 29004 KB Output is correct
138 Correct 62 ms 26020 KB Output is correct
139 Correct 83 ms 23272 KB Output is correct
140 Correct 100 ms 23560 KB Output is correct
141 Correct 99 ms 24000 KB Output is correct
142 Correct 106 ms 24120 KB Output is correct
143 Correct 57 ms 26084 KB Output is correct
144 Correct 57 ms 25516 KB Output is correct
145 Correct 54 ms 25304 KB Output is correct
146 Correct 99 ms 22372 KB Output is correct
147 Correct 76 ms 21196 KB Output is correct
148 Correct 63 ms 21552 KB Output is correct
149 Correct 72 ms 22824 KB Output is correct
150 Correct 108 ms 31784 KB Output is correct
151 Correct 87 ms 22312 KB Output is correct
152 Correct 110 ms 24044 KB Output is correct
153 Correct 114 ms 24044 KB Output is correct
154 Correct 80 ms 24228 KB Output is correct
155 Correct 62 ms 25556 KB Output is correct
156 Correct 69 ms 27664 KB Output is correct
157 Correct 70 ms 27024 KB Output is correct
158 Correct 74 ms 26192 KB Output is correct
159 Correct 96 ms 24664 KB Output is correct
160 Correct 110 ms 31748 KB Output is correct
161 Correct 82 ms 24296 KB Output is correct
162 Correct 57 ms 25276 KB Output is correct