Submission #633523

# Submission time Handle Problem Language Result Execution time Memory
633523 2022-08-22T16:43:02 Z MatBad Subtree (INOI20_subtree) C++14
100 / 100
103 ms 21504 KB
#include<bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for(int i=a; i<=b; i++)
#define FORR(i, a, b) for(int i=a; i>=b; i--)
#define pb push_back
#define ppb pop_back
#define lc 2*u
#define rc 2*u+1
#define mid ((l+r)/2)
#define F first
#define S second
#define debug(x) cerr<<"$ "<<#x<<" : "<<x<<" $\n"
#define wall() cerr<<"\n----------------------\n"

typedef long long ll;
typedef pair<ll, int> pii;

const ll MX=1e5+5, M=1e9+5, LG=19, inf=1e9+5, MOD=1e9+7;

ll n, m, dp[2][MX], N, beg[MX], p[MX];
vector<int> adj[MX], adjt[MX];
pii e[MX];
bool mark[MX], m1[MX];

void dd(int u, int par=0){
	m1[u]=mark[u]=true;
	adj[par].pb(u), p[u]=par;
	for(int v:adjt[u]) if(v!=par){
		if(!m1[v]) dd(v, u);
		else if(mark[v]){
			N++;
			beg[v]=N;
			e[N] = pii(v, u);
		}
	}
	mark[u]=false;
}

pii solve(int k){
	pii res;
	vector<int> vec;
	for(int v=e[k].S; v!=e[k].F; v=p[v]){
		vec.pb(v);
	}
	int t=(int)vec.size();
	//FOR(i, 0, t-1) debug(vec[i]);
	res.S = vec[t-1];
	ll f[3][t+5];
	f[0][0]=0;
	f[1][0]=1;
	f[2][0]=dp[1][vec[0]];
	FOR(i, 1, t-1){
		f[1][i] = (f[1][i-1]+f[2][i-1])%MOD;
		ll d=1;
		for(int v:adj[vec[i]]) if(v!=vec[i-1]) d=d*(dp[1][v]+1)%MOD;
		f[0][i] = d*(f[0][i-1]+f[1][i-1])%MOD;
		f[2][i] = d*f[2][i-1]%MOD;
		//debug(vec[i]);
		//FOR(j, 0, 2) debug(f[j][i]);
	}
	FOR(i, 0, 1) res.F = (res.F+f[i][t-1]);
	return res;
}

void dfs(int u){
	for(int v:adj[u]){
		dfs(v);
		dp[0][u] = (dp[0][u]+dp[0][v]+dp[1][v])%MOD;
		
	}
	
	dp[1][u] = 1;
	pii g = pii(0, 0);
	if(beg[u]){
		g = solve(beg[u]);
		dp[1][u] = g.F;
	}
	for(int v:adj[u]) if(v!=g.S){
		dp[1][u] = dp[1][u]*(dp[1][v]+1)%MOD;
	}
	//debug(u<<' '<<dp[0][u]<<' '<<dp[1][u]);
}

int main(){
	ios::sync_with_stdio(0); cin.tie(0);
	cin>>n>>m;
	FOR(i, 1, m) {
		int u, v;
		cin>>u>>v;
		adjt[u].pb(v);
		adjt[v].pb(u);
	}
	dd(1);
	dfs(1);
	cout<<(dp[0][1]+dp[1][1])%MOD<<'\n';
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4948 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 3 ms 4948 KB Output is correct
4 Correct 4 ms 4948 KB Output is correct
5 Correct 4 ms 4948 KB Output is correct
6 Correct 3 ms 4948 KB Output is correct
7 Correct 3 ms 4948 KB Output is correct
8 Correct 3 ms 4948 KB Output is correct
9 Correct 3 ms 4948 KB Output is correct
10 Correct 3 ms 4948 KB Output is correct
11 Correct 2 ms 4948 KB Output is correct
12 Correct 3 ms 4948 KB Output is correct
13 Correct 2 ms 4948 KB Output is correct
14 Correct 4 ms 5008 KB Output is correct
15 Correct 3 ms 4948 KB Output is correct
16 Correct 3 ms 5036 KB Output is correct
17 Correct 3 ms 4948 KB Output is correct
18 Correct 3 ms 4948 KB Output is correct
19 Correct 3 ms 4948 KB Output is correct
20 Correct 3 ms 4948 KB Output is correct
21 Correct 3 ms 4948 KB Output is correct
22 Correct 3 ms 4948 KB Output is correct
23 Correct 3 ms 4948 KB Output is correct
24 Correct 3 ms 5036 KB Output is correct
25 Correct 3 ms 5076 KB Output is correct
26 Correct 3 ms 4948 KB Output is correct
27 Correct 3 ms 5040 KB Output is correct
28 Correct 3 ms 4948 KB Output is correct
29 Correct 2 ms 4948 KB Output is correct
30 Correct 2 ms 5040 KB Output is correct
31 Correct 2 ms 4948 KB Output is correct
32 Correct 3 ms 5036 KB Output is correct
33 Correct 3 ms 5012 KB Output is correct
34 Correct 2 ms 5036 KB Output is correct
35 Correct 3 ms 4948 KB Output is correct
36 Correct 3 ms 4948 KB Output is correct
37 Correct 3 ms 4948 KB Output is correct
38 Correct 3 ms 5032 KB Output is correct
39 Correct 3 ms 4948 KB Output is correct
40 Correct 3 ms 5040 KB Output is correct
41 Correct 3 ms 4948 KB Output is correct
42 Correct 3 ms 4948 KB Output is correct
43 Correct 3 ms 4948 KB Output is correct
44 Correct 2 ms 5032 KB Output is correct
45 Correct 3 ms 5032 KB Output is correct
46 Correct 3 ms 4948 KB Output is correct
47 Correct 4 ms 5036 KB Output is correct
48 Correct 3 ms 5036 KB Output is correct
49 Correct 4 ms 4948 KB Output is correct
50 Correct 3 ms 4948 KB Output is correct
51 Correct 3 ms 4948 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5036 KB Output is correct
2 Correct 7 ms 5864 KB Output is correct
3 Correct 64 ms 12676 KB Output is correct
4 Correct 58 ms 12796 KB Output is correct
5 Correct 56 ms 12708 KB Output is correct
6 Correct 71 ms 12748 KB Output is correct
7 Correct 76 ms 19652 KB Output is correct
8 Correct 72 ms 16780 KB Output is correct
9 Correct 63 ms 16256 KB Output is correct
10 Correct 38 ms 11756 KB Output is correct
11 Correct 47 ms 12740 KB Output is correct
12 Correct 68 ms 13956 KB Output is correct
13 Correct 77 ms 14232 KB Output is correct
14 Correct 45 ms 15096 KB Output is correct
15 Correct 67 ms 20460 KB Output is correct
16 Correct 73 ms 21504 KB Output is correct
17 Correct 79 ms 14236 KB Output is correct
18 Correct 74 ms 14256 KB Output is correct
19 Correct 62 ms 14184 KB Output is correct
20 Correct 37 ms 11588 KB Output is correct
21 Correct 38 ms 11340 KB Output is correct
22 Correct 37 ms 11212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4948 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 3 ms 4948 KB Output is correct
4 Correct 4 ms 4948 KB Output is correct
5 Correct 4 ms 4948 KB Output is correct
6 Correct 3 ms 4948 KB Output is correct
7 Correct 3 ms 4948 KB Output is correct
8 Correct 3 ms 4948 KB Output is correct
9 Correct 3 ms 4948 KB Output is correct
10 Correct 3 ms 4948 KB Output is correct
11 Correct 2 ms 4948 KB Output is correct
12 Correct 3 ms 4948 KB Output is correct
13 Correct 2 ms 4948 KB Output is correct
14 Correct 4 ms 5008 KB Output is correct
15 Correct 3 ms 4948 KB Output is correct
16 Correct 3 ms 5036 KB Output is correct
17 Correct 3 ms 4948 KB Output is correct
18 Correct 3 ms 4948 KB Output is correct
19 Correct 3 ms 4948 KB Output is correct
20 Correct 3 ms 4948 KB Output is correct
21 Correct 3 ms 4948 KB Output is correct
22 Correct 3 ms 4948 KB Output is correct
23 Correct 3 ms 4948 KB Output is correct
24 Correct 3 ms 5036 KB Output is correct
25 Correct 3 ms 5076 KB Output is correct
26 Correct 3 ms 4948 KB Output is correct
27 Correct 3 ms 5040 KB Output is correct
28 Correct 3 ms 4948 KB Output is correct
29 Correct 2 ms 4948 KB Output is correct
30 Correct 2 ms 5040 KB Output is correct
31 Correct 2 ms 4948 KB Output is correct
32 Correct 3 ms 5036 KB Output is correct
33 Correct 3 ms 5012 KB Output is correct
34 Correct 2 ms 5036 KB Output is correct
35 Correct 3 ms 4948 KB Output is correct
36 Correct 3 ms 4948 KB Output is correct
37 Correct 3 ms 4948 KB Output is correct
38 Correct 3 ms 5032 KB Output is correct
39 Correct 3 ms 4948 KB Output is correct
40 Correct 3 ms 5040 KB Output is correct
41 Correct 3 ms 4948 KB Output is correct
42 Correct 3 ms 4948 KB Output is correct
43 Correct 3 ms 4948 KB Output is correct
44 Correct 2 ms 5032 KB Output is correct
45 Correct 3 ms 5032 KB Output is correct
46 Correct 3 ms 4948 KB Output is correct
47 Correct 4 ms 5036 KB Output is correct
48 Correct 3 ms 5036 KB Output is correct
49 Correct 4 ms 4948 KB Output is correct
50 Correct 3 ms 4948 KB Output is correct
51 Correct 3 ms 4948 KB Output is correct
52 Correct 4 ms 5460 KB Output is correct
53 Correct 5 ms 5460 KB Output is correct
54 Correct 7 ms 5436 KB Output is correct
55 Correct 5 ms 5500 KB Output is correct
56 Correct 5 ms 5788 KB Output is correct
57 Correct 4 ms 5376 KB Output is correct
58 Correct 5 ms 5460 KB Output is correct
59 Correct 5 ms 5628 KB Output is correct
60 Correct 4 ms 5460 KB Output is correct
61 Correct 4 ms 5588 KB Output is correct
62 Correct 5 ms 5588 KB Output is correct
63 Correct 4 ms 5332 KB Output is correct
64 Correct 4 ms 5332 KB Output is correct
65 Correct 5 ms 5432 KB Output is correct
66 Correct 5 ms 5460 KB Output is correct
67 Correct 4 ms 5432 KB Output is correct
68 Correct 5 ms 5716 KB Output is correct
69 Correct 5 ms 5588 KB Output is correct
70 Correct 4 ms 5428 KB Output is correct
71 Correct 6 ms 5580 KB Output is correct
72 Correct 6 ms 5556 KB Output is correct
73 Correct 4 ms 5428 KB Output is correct
74 Correct 5 ms 5556 KB Output is correct
75 Correct 5 ms 5688 KB Output is correct
76 Correct 6 ms 5460 KB Output is correct
77 Correct 5 ms 5844 KB Output is correct
78 Correct 5 ms 5588 KB Output is correct
79 Correct 5 ms 5816 KB Output is correct
80 Correct 5 ms 5588 KB Output is correct
81 Correct 5 ms 5460 KB Output is correct
82 Correct 5 ms 5588 KB Output is correct
83 Correct 4 ms 5460 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4948 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 3 ms 4948 KB Output is correct
4 Correct 4 ms 4948 KB Output is correct
5 Correct 4 ms 4948 KB Output is correct
6 Correct 3 ms 4948 KB Output is correct
7 Correct 3 ms 4948 KB Output is correct
8 Correct 3 ms 4948 KB Output is correct
9 Correct 3 ms 4948 KB Output is correct
10 Correct 3 ms 4948 KB Output is correct
11 Correct 2 ms 4948 KB Output is correct
12 Correct 3 ms 4948 KB Output is correct
13 Correct 2 ms 4948 KB Output is correct
14 Correct 4 ms 5008 KB Output is correct
15 Correct 3 ms 4948 KB Output is correct
16 Correct 3 ms 5036 KB Output is correct
17 Correct 3 ms 4948 KB Output is correct
18 Correct 3 ms 4948 KB Output is correct
19 Correct 3 ms 4948 KB Output is correct
20 Correct 3 ms 4948 KB Output is correct
21 Correct 3 ms 4948 KB Output is correct
22 Correct 3 ms 4948 KB Output is correct
23 Correct 3 ms 4948 KB Output is correct
24 Correct 3 ms 5036 KB Output is correct
25 Correct 3 ms 5076 KB Output is correct
26 Correct 3 ms 4948 KB Output is correct
27 Correct 3 ms 5040 KB Output is correct
28 Correct 3 ms 4948 KB Output is correct
29 Correct 2 ms 4948 KB Output is correct
30 Correct 2 ms 5040 KB Output is correct
31 Correct 2 ms 4948 KB Output is correct
32 Correct 3 ms 5036 KB Output is correct
33 Correct 3 ms 5012 KB Output is correct
34 Correct 2 ms 5036 KB Output is correct
35 Correct 3 ms 4948 KB Output is correct
36 Correct 3 ms 4948 KB Output is correct
37 Correct 3 ms 4948 KB Output is correct
38 Correct 3 ms 5032 KB Output is correct
39 Correct 3 ms 4948 KB Output is correct
40 Correct 3 ms 5040 KB Output is correct
41 Correct 3 ms 4948 KB Output is correct
42 Correct 3 ms 4948 KB Output is correct
43 Correct 3 ms 4948 KB Output is correct
44 Correct 2 ms 5032 KB Output is correct
45 Correct 3 ms 5032 KB Output is correct
46 Correct 3 ms 4948 KB Output is correct
47 Correct 4 ms 5036 KB Output is correct
48 Correct 3 ms 5036 KB Output is correct
49 Correct 4 ms 4948 KB Output is correct
50 Correct 3 ms 4948 KB Output is correct
51 Correct 3 ms 4948 KB Output is correct
52 Correct 3 ms 5036 KB Output is correct
53 Correct 7 ms 5864 KB Output is correct
54 Correct 64 ms 12676 KB Output is correct
55 Correct 58 ms 12796 KB Output is correct
56 Correct 56 ms 12708 KB Output is correct
57 Correct 71 ms 12748 KB Output is correct
58 Correct 76 ms 19652 KB Output is correct
59 Correct 72 ms 16780 KB Output is correct
60 Correct 63 ms 16256 KB Output is correct
61 Correct 38 ms 11756 KB Output is correct
62 Correct 47 ms 12740 KB Output is correct
63 Correct 68 ms 13956 KB Output is correct
64 Correct 77 ms 14232 KB Output is correct
65 Correct 45 ms 15096 KB Output is correct
66 Correct 67 ms 20460 KB Output is correct
67 Correct 73 ms 21504 KB Output is correct
68 Correct 79 ms 14236 KB Output is correct
69 Correct 74 ms 14256 KB Output is correct
70 Correct 62 ms 14184 KB Output is correct
71 Correct 37 ms 11588 KB Output is correct
72 Correct 38 ms 11340 KB Output is correct
73 Correct 37 ms 11212 KB Output is correct
74 Correct 4 ms 5460 KB Output is correct
75 Correct 5 ms 5460 KB Output is correct
76 Correct 7 ms 5436 KB Output is correct
77 Correct 5 ms 5500 KB Output is correct
78 Correct 5 ms 5788 KB Output is correct
79 Correct 4 ms 5376 KB Output is correct
80 Correct 5 ms 5460 KB Output is correct
81 Correct 5 ms 5628 KB Output is correct
82 Correct 4 ms 5460 KB Output is correct
83 Correct 4 ms 5588 KB Output is correct
84 Correct 5 ms 5588 KB Output is correct
85 Correct 4 ms 5332 KB Output is correct
86 Correct 4 ms 5332 KB Output is correct
87 Correct 5 ms 5432 KB Output is correct
88 Correct 5 ms 5460 KB Output is correct
89 Correct 4 ms 5432 KB Output is correct
90 Correct 5 ms 5716 KB Output is correct
91 Correct 5 ms 5588 KB Output is correct
92 Correct 4 ms 5428 KB Output is correct
93 Correct 6 ms 5580 KB Output is correct
94 Correct 6 ms 5556 KB Output is correct
95 Correct 4 ms 5428 KB Output is correct
96 Correct 5 ms 5556 KB Output is correct
97 Correct 5 ms 5688 KB Output is correct
98 Correct 6 ms 5460 KB Output is correct
99 Correct 5 ms 5844 KB Output is correct
100 Correct 5 ms 5588 KB Output is correct
101 Correct 5 ms 5816 KB Output is correct
102 Correct 5 ms 5588 KB Output is correct
103 Correct 5 ms 5460 KB Output is correct
104 Correct 5 ms 5588 KB Output is correct
105 Correct 4 ms 5460 KB Output is correct
106 Correct 61 ms 14456 KB Output is correct
107 Correct 63 ms 14512 KB Output is correct
108 Correct 62 ms 14620 KB Output is correct
109 Correct 89 ms 14704 KB Output is correct
110 Correct 56 ms 12688 KB Output is correct
111 Correct 49 ms 14636 KB Output is correct
112 Correct 74 ms 19992 KB Output is correct
113 Correct 65 ms 15884 KB Output is correct
114 Correct 102 ms 15652 KB Output is correct
115 Correct 42 ms 12988 KB Output is correct
116 Correct 81 ms 21196 KB Output is correct
117 Correct 74 ms 19972 KB Output is correct
118 Correct 39 ms 14536 KB Output is correct
119 Correct 78 ms 15772 KB Output is correct
120 Correct 103 ms 15908 KB Output is correct
121 Correct 75 ms 15868 KB Output is correct
122 Correct 76 ms 16024 KB Output is correct
123 Correct 40 ms 12932 KB Output is correct
124 Correct 43 ms 12108 KB Output is correct
125 Correct 35 ms 12356 KB Output is correct
126 Correct 84 ms 14484 KB Output is correct
127 Correct 59 ms 14600 KB Output is correct
128 Correct 60 ms 14596 KB Output is correct
129 Correct 71 ms 14740 KB Output is correct
130 Correct 38 ms 12620 KB Output is correct
131 Correct 49 ms 14676 KB Output is correct
132 Correct 82 ms 19916 KB Output is correct
133 Correct 65 ms 15824 KB Output is correct
134 Correct 66 ms 15648 KB Output is correct
135 Correct 35 ms 12996 KB Output is correct
136 Correct 90 ms 19804 KB Output is correct
137 Correct 74 ms 19708 KB Output is correct
138 Correct 37 ms 14668 KB Output is correct
139 Correct 62 ms 15840 KB Output is correct
140 Correct 81 ms 15828 KB Output is correct
141 Correct 78 ms 15920 KB Output is correct
142 Correct 73 ms 16016 KB Output is correct
143 Correct 42 ms 12884 KB Output is correct
144 Correct 35 ms 12080 KB Output is correct
145 Correct 34 ms 12404 KB Output is correct
146 Correct 68 ms 15700 KB Output is correct
147 Correct 62 ms 15788 KB Output is correct
148 Correct 64 ms 15740 KB Output is correct
149 Correct 71 ms 16044 KB Output is correct
150 Correct 64 ms 20832 KB Output is correct
151 Correct 69 ms 15788 KB Output is correct
152 Correct 72 ms 15952 KB Output is correct
153 Correct 75 ms 15968 KB Output is correct
154 Correct 73 ms 15556 KB Output is correct
155 Correct 38 ms 13768 KB Output is correct
156 Correct 69 ms 19812 KB Output is correct
157 Correct 65 ms 19148 KB Output is correct
158 Correct 66 ms 18572 KB Output is correct
159 Correct 62 ms 15680 KB Output is correct
160 Correct 74 ms 20460 KB Output is correct
161 Correct 91 ms 15316 KB Output is correct
162 Correct 59 ms 13260 KB Output is correct