답안 #1041983

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1041983 2024-08-02T11:41:15 Z PM1 Subtree (INOI20_subtree) C++17
100 / 100
31 ms 21588 KB
#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int mxn=1e5+5,M=1e9+7;
int n,m,par[mxn],h[mxn],dis[mxn],mark[mxn];
vector<int>v[mxn];
ll dp[mxn][2][2][2],ans=0;
void dfs(int z){
	mark[z]=1;
	ll res=1;
	int c=0;
	for(auto i:v[z]){
		if(par[z]!=i && !mark[i]){
			par[i]=z;
			dis[i]=dis[z]+1;
			dfs(i);
			if(h[i]>dis[z] || h[i]==0){
				res*=(dp[i][1][1][0]+1);
				res%=M;
			}
			else{
				c=i;
				h[z]=h[i];
			}
		}
		else if(mark[i] && par[z]!=i && h[z]==0){
			h[z]=dis[i];
		}
	}
	if(c==0){
		dp[z][1][1][0]=res;
		dp[z][0][0][0]=1;
		ans+=dp[z][1][1][0];
		ans%=M;
		//cout<<"F"<<" "<<z<<" "<<ans<<'\n';
		return ;
	}
	dp[z][0][0][0]=1;
	dp[z][0][1][0]=((dp[c][0][0][0]+dp[c][0][1][0])*res)%M;
	//dp[z][0][0][1]=(dp[c][0][0][1]+dp[0][1][0])%M;
	dp[z][1][1][0]=(dp[c][1][1][0]*res)%M;
	dp[z][1][0][0]=(dp[c][1][1][0]+dp[c][1][0][0])%M;
	dp[z][1][1][1]=((dp[c][1][1][1]+dp[c][1][0][0])*res)%M;
	ans+=dp[z][1][1][0]+dp[z][0][1][0];
	if(h[z]==dis[z]){
		ans+=dp[z][1][1][1];
		ans-=dp[z][1][1][0];
		dp[z][1][1][0]=(dp[z][1][1][1]+dp[z][0][1][0])%M;
	}
	ans%=M;
	//cout<<z<<" "<<ans<<" "<<dp[z][1][0][0]<<h[z]<<'\n';
}
int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	cin>>n>>m;
	for(int i=1;i<=m;i++){
		int x,y;
		cin>>x>>y;
		v[x].push_back(y);
		v[y].push_back(x);
	}
	dis[1]=1;
	dfs(1);
	ans+=M;
	ans%=M;
	cout<<ans;
	return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 4696 KB Output is correct
2 Correct 1 ms 4696 KB Output is correct
3 Correct 1 ms 4700 KB Output is correct
4 Correct 1 ms 4700 KB Output is correct
5 Correct 1 ms 4700 KB Output is correct
6 Correct 1 ms 4700 KB Output is correct
7 Correct 1 ms 4700 KB Output is correct
8 Correct 1 ms 4700 KB Output is correct
9 Correct 1 ms 4700 KB Output is correct
10 Correct 1 ms 4700 KB Output is correct
11 Correct 0 ms 4700 KB Output is correct
12 Correct 1 ms 4700 KB Output is correct
13 Correct 1 ms 4700 KB Output is correct
14 Correct 1 ms 4700 KB Output is correct
15 Correct 1 ms 4700 KB Output is correct
16 Correct 1 ms 4788 KB Output is correct
17 Correct 1 ms 4700 KB Output is correct
18 Correct 1 ms 4700 KB Output is correct
19 Correct 1 ms 4700 KB Output is correct
20 Correct 1 ms 4700 KB Output is correct
21 Correct 1 ms 4700 KB Output is correct
22 Correct 1 ms 4784 KB Output is correct
23 Correct 1 ms 4700 KB Output is correct
24 Correct 1 ms 4700 KB Output is correct
25 Correct 1 ms 4696 KB Output is correct
26 Correct 1 ms 4700 KB Output is correct
27 Correct 1 ms 4700 KB Output is correct
28 Correct 1 ms 4700 KB Output is correct
29 Correct 1 ms 4792 KB Output is correct
30 Correct 1 ms 4700 KB Output is correct
31 Correct 1 ms 4700 KB Output is correct
32 Correct 1 ms 4700 KB Output is correct
33 Correct 1 ms 4700 KB Output is correct
34 Correct 1 ms 4700 KB Output is correct
35 Correct 1 ms 4700 KB Output is correct
36 Correct 0 ms 4700 KB Output is correct
37 Correct 1 ms 4700 KB Output is correct
38 Correct 1 ms 4700 KB Output is correct
39 Correct 1 ms 4700 KB Output is correct
40 Correct 1 ms 4700 KB Output is correct
41 Correct 1 ms 4700 KB Output is correct
42 Correct 1 ms 4700 KB Output is correct
43 Correct 1 ms 4700 KB Output is correct
44 Correct 1 ms 4700 KB Output is correct
45 Correct 1 ms 4700 KB Output is correct
46 Correct 1 ms 4700 KB Output is correct
47 Correct 1 ms 4700 KB Output is correct
48 Correct 1 ms 4700 KB Output is correct
49 Correct 1 ms 4700 KB Output is correct
50 Correct 1 ms 4700 KB Output is correct
51 Correct 1 ms 4700 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 4696 KB Output is correct
2 Correct 4 ms 7512 KB Output is correct
3 Correct 22 ms 14620 KB Output is correct
4 Correct 26 ms 14384 KB Output is correct
5 Correct 21 ms 14428 KB Output is correct
6 Correct 24 ms 14468 KB Output is correct
7 Correct 28 ms 20056 KB Output is correct
8 Correct 23 ms 18008 KB Output is correct
9 Correct 23 ms 18012 KB Output is correct
10 Correct 18 ms 14940 KB Output is correct
11 Correct 21 ms 14804 KB Output is correct
12 Correct 26 ms 14432 KB Output is correct
13 Correct 23 ms 14428 KB Output is correct
14 Correct 21 ms 17148 KB Output is correct
15 Correct 27 ms 20820 KB Output is correct
16 Correct 27 ms 21588 KB Output is correct
17 Correct 23 ms 14428 KB Output is correct
18 Correct 25 ms 14428 KB Output is correct
19 Correct 23 ms 14424 KB Output is correct
20 Correct 20 ms 14804 KB Output is correct
21 Correct 19 ms 15028 KB Output is correct
22 Correct 21 ms 14940 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 4696 KB Output is correct
2 Correct 1 ms 4696 KB Output is correct
3 Correct 1 ms 4700 KB Output is correct
4 Correct 1 ms 4700 KB Output is correct
5 Correct 1 ms 4700 KB Output is correct
6 Correct 1 ms 4700 KB Output is correct
7 Correct 1 ms 4700 KB Output is correct
8 Correct 1 ms 4700 KB Output is correct
9 Correct 1 ms 4700 KB Output is correct
10 Correct 1 ms 4700 KB Output is correct
11 Correct 0 ms 4700 KB Output is correct
12 Correct 1 ms 4700 KB Output is correct
13 Correct 1 ms 4700 KB Output is correct
14 Correct 1 ms 4700 KB Output is correct
15 Correct 1 ms 4700 KB Output is correct
16 Correct 1 ms 4788 KB Output is correct
17 Correct 1 ms 4700 KB Output is correct
18 Correct 1 ms 4700 KB Output is correct
19 Correct 1 ms 4700 KB Output is correct
20 Correct 1 ms 4700 KB Output is correct
21 Correct 1 ms 4700 KB Output is correct
22 Correct 1 ms 4784 KB Output is correct
23 Correct 1 ms 4700 KB Output is correct
24 Correct 1 ms 4700 KB Output is correct
25 Correct 1 ms 4696 KB Output is correct
26 Correct 1 ms 4700 KB Output is correct
27 Correct 1 ms 4700 KB Output is correct
28 Correct 1 ms 4700 KB Output is correct
29 Correct 1 ms 4792 KB Output is correct
30 Correct 1 ms 4700 KB Output is correct
31 Correct 1 ms 4700 KB Output is correct
32 Correct 1 ms 4700 KB Output is correct
33 Correct 1 ms 4700 KB Output is correct
34 Correct 1 ms 4700 KB Output is correct
35 Correct 1 ms 4700 KB Output is correct
36 Correct 0 ms 4700 KB Output is correct
37 Correct 1 ms 4700 KB Output is correct
38 Correct 1 ms 4700 KB Output is correct
39 Correct 1 ms 4700 KB Output is correct
40 Correct 1 ms 4700 KB Output is correct
41 Correct 1 ms 4700 KB Output is correct
42 Correct 1 ms 4700 KB Output is correct
43 Correct 1 ms 4700 KB Output is correct
44 Correct 1 ms 4700 KB Output is correct
45 Correct 1 ms 4700 KB Output is correct
46 Correct 1 ms 4700 KB Output is correct
47 Correct 1 ms 4700 KB Output is correct
48 Correct 1 ms 4700 KB Output is correct
49 Correct 1 ms 4700 KB Output is correct
50 Correct 1 ms 4700 KB Output is correct
51 Correct 1 ms 4700 KB Output is correct
52 Correct 2 ms 7256 KB Output is correct
53 Correct 2 ms 7260 KB Output is correct
54 Correct 2 ms 7260 KB Output is correct
55 Correct 2 ms 7260 KB Output is correct
56 Correct 2 ms 7516 KB Output is correct
57 Correct 2 ms 7200 KB Output is correct
58 Correct 2 ms 7256 KB Output is correct
59 Correct 2 ms 7224 KB Output is correct
60 Correct 1 ms 7260 KB Output is correct
61 Correct 3 ms 7260 KB Output is correct
62 Correct 2 ms 7260 KB Output is correct
63 Correct 1 ms 7260 KB Output is correct
64 Correct 1 ms 7260 KB Output is correct
65 Correct 2 ms 7260 KB Output is correct
66 Correct 2 ms 7260 KB Output is correct
67 Correct 2 ms 7260 KB Output is correct
68 Correct 2 ms 7516 KB Output is correct
69 Correct 2 ms 7284 KB Output is correct
70 Correct 2 ms 7260 KB Output is correct
71 Correct 2 ms 7260 KB Output is correct
72 Correct 2 ms 7260 KB Output is correct
73 Correct 3 ms 7260 KB Output is correct
74 Correct 2 ms 7260 KB Output is correct
75 Correct 2 ms 7260 KB Output is correct
76 Correct 2 ms 7260 KB Output is correct
77 Correct 2 ms 7516 KB Output is correct
78 Correct 2 ms 7268 KB Output is correct
79 Correct 3 ms 7516 KB Output is correct
80 Correct 2 ms 7104 KB Output is correct
81 Correct 2 ms 7348 KB Output is correct
82 Correct 2 ms 7260 KB Output is correct
83 Correct 2 ms 7260 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 4696 KB Output is correct
2 Correct 1 ms 4696 KB Output is correct
3 Correct 1 ms 4700 KB Output is correct
4 Correct 1 ms 4700 KB Output is correct
5 Correct 1 ms 4700 KB Output is correct
6 Correct 1 ms 4700 KB Output is correct
7 Correct 1 ms 4700 KB Output is correct
8 Correct 1 ms 4700 KB Output is correct
9 Correct 1 ms 4700 KB Output is correct
10 Correct 1 ms 4700 KB Output is correct
11 Correct 0 ms 4700 KB Output is correct
12 Correct 1 ms 4700 KB Output is correct
13 Correct 1 ms 4700 KB Output is correct
14 Correct 1 ms 4700 KB Output is correct
15 Correct 1 ms 4700 KB Output is correct
16 Correct 1 ms 4788 KB Output is correct
17 Correct 1 ms 4700 KB Output is correct
18 Correct 1 ms 4700 KB Output is correct
19 Correct 1 ms 4700 KB Output is correct
20 Correct 1 ms 4700 KB Output is correct
21 Correct 1 ms 4700 KB Output is correct
22 Correct 1 ms 4784 KB Output is correct
23 Correct 1 ms 4700 KB Output is correct
24 Correct 1 ms 4700 KB Output is correct
25 Correct 1 ms 4696 KB Output is correct
26 Correct 1 ms 4700 KB Output is correct
27 Correct 1 ms 4700 KB Output is correct
28 Correct 1 ms 4700 KB Output is correct
29 Correct 1 ms 4792 KB Output is correct
30 Correct 1 ms 4700 KB Output is correct
31 Correct 1 ms 4700 KB Output is correct
32 Correct 1 ms 4700 KB Output is correct
33 Correct 1 ms 4700 KB Output is correct
34 Correct 1 ms 4700 KB Output is correct
35 Correct 1 ms 4700 KB Output is correct
36 Correct 0 ms 4700 KB Output is correct
37 Correct 1 ms 4700 KB Output is correct
38 Correct 1 ms 4700 KB Output is correct
39 Correct 1 ms 4700 KB Output is correct
40 Correct 1 ms 4700 KB Output is correct
41 Correct 1 ms 4700 KB Output is correct
42 Correct 1 ms 4700 KB Output is correct
43 Correct 1 ms 4700 KB Output is correct
44 Correct 1 ms 4700 KB Output is correct
45 Correct 1 ms 4700 KB Output is correct
46 Correct 1 ms 4700 KB Output is correct
47 Correct 1 ms 4700 KB Output is correct
48 Correct 1 ms 4700 KB Output is correct
49 Correct 1 ms 4700 KB Output is correct
50 Correct 1 ms 4700 KB Output is correct
51 Correct 1 ms 4700 KB Output is correct
52 Correct 1 ms 4696 KB Output is correct
53 Correct 4 ms 7512 KB Output is correct
54 Correct 22 ms 14620 KB Output is correct
55 Correct 26 ms 14384 KB Output is correct
56 Correct 21 ms 14428 KB Output is correct
57 Correct 24 ms 14468 KB Output is correct
58 Correct 28 ms 20056 KB Output is correct
59 Correct 23 ms 18008 KB Output is correct
60 Correct 23 ms 18012 KB Output is correct
61 Correct 18 ms 14940 KB Output is correct
62 Correct 21 ms 14804 KB Output is correct
63 Correct 26 ms 14432 KB Output is correct
64 Correct 23 ms 14428 KB Output is correct
65 Correct 21 ms 17148 KB Output is correct
66 Correct 27 ms 20820 KB Output is correct
67 Correct 27 ms 21588 KB Output is correct
68 Correct 23 ms 14428 KB Output is correct
69 Correct 25 ms 14428 KB Output is correct
70 Correct 23 ms 14424 KB Output is correct
71 Correct 20 ms 14804 KB Output is correct
72 Correct 19 ms 15028 KB Output is correct
73 Correct 21 ms 14940 KB Output is correct
74 Correct 2 ms 7256 KB Output is correct
75 Correct 2 ms 7260 KB Output is correct
76 Correct 2 ms 7260 KB Output is correct
77 Correct 2 ms 7260 KB Output is correct
78 Correct 2 ms 7516 KB Output is correct
79 Correct 2 ms 7200 KB Output is correct
80 Correct 2 ms 7256 KB Output is correct
81 Correct 2 ms 7224 KB Output is correct
82 Correct 1 ms 7260 KB Output is correct
83 Correct 3 ms 7260 KB Output is correct
84 Correct 2 ms 7260 KB Output is correct
85 Correct 1 ms 7260 KB Output is correct
86 Correct 1 ms 7260 KB Output is correct
87 Correct 2 ms 7260 KB Output is correct
88 Correct 2 ms 7260 KB Output is correct
89 Correct 2 ms 7260 KB Output is correct
90 Correct 2 ms 7516 KB Output is correct
91 Correct 2 ms 7284 KB Output is correct
92 Correct 2 ms 7260 KB Output is correct
93 Correct 2 ms 7260 KB Output is correct
94 Correct 2 ms 7260 KB Output is correct
95 Correct 3 ms 7260 KB Output is correct
96 Correct 2 ms 7260 KB Output is correct
97 Correct 2 ms 7260 KB Output is correct
98 Correct 2 ms 7260 KB Output is correct
99 Correct 2 ms 7516 KB Output is correct
100 Correct 2 ms 7268 KB Output is correct
101 Correct 3 ms 7516 KB Output is correct
102 Correct 2 ms 7104 KB Output is correct
103 Correct 2 ms 7348 KB Output is correct
104 Correct 2 ms 7260 KB Output is correct
105 Correct 2 ms 7260 KB Output is correct
106 Correct 23 ms 15192 KB Output is correct
107 Correct 24 ms 14928 KB Output is correct
108 Correct 22 ms 14940 KB Output is correct
109 Correct 23 ms 15196 KB Output is correct
110 Correct 22 ms 15176 KB Output is correct
111 Correct 23 ms 15964 KB Output is correct
112 Correct 26 ms 19288 KB Output is correct
113 Correct 23 ms 14968 KB Output is correct
114 Correct 26 ms 14940 KB Output is correct
115 Correct 19 ms 14804 KB Output is correct
116 Correct 27 ms 20152 KB Output is correct
117 Correct 28 ms 19536 KB Output is correct
118 Correct 17 ms 15828 KB Output is correct
119 Correct 23 ms 15032 KB Output is correct
120 Correct 26 ms 14936 KB Output is correct
121 Correct 26 ms 14940 KB Output is correct
122 Correct 25 ms 15196 KB Output is correct
123 Correct 17 ms 15156 KB Output is correct
124 Correct 19 ms 14936 KB Output is correct
125 Correct 17 ms 14796 KB Output is correct
126 Correct 21 ms 14936 KB Output is correct
127 Correct 21 ms 14940 KB Output is correct
128 Correct 23 ms 15040 KB Output is correct
129 Correct 30 ms 15184 KB Output is correct
130 Correct 19 ms 15196 KB Output is correct
131 Correct 20 ms 15912 KB Output is correct
132 Correct 29 ms 19292 KB Output is correct
133 Correct 26 ms 15212 KB Output is correct
134 Correct 22 ms 14968 KB Output is correct
135 Correct 17 ms 14804 KB Output is correct
136 Correct 27 ms 18788 KB Output is correct
137 Correct 27 ms 19032 KB Output is correct
138 Correct 20 ms 15828 KB Output is correct
139 Correct 25 ms 15032 KB Output is correct
140 Correct 23 ms 14940 KB Output is correct
141 Correct 25 ms 14932 KB Output is correct
142 Correct 25 ms 14940 KB Output is correct
143 Correct 21 ms 14948 KB Output is correct
144 Correct 21 ms 15072 KB Output is correct
145 Correct 19 ms 14800 KB Output is correct
146 Correct 27 ms 14836 KB Output is correct
147 Correct 21 ms 14940 KB Output is correct
148 Correct 24 ms 15124 KB Output is correct
149 Correct 31 ms 15900 KB Output is correct
150 Correct 28 ms 20564 KB Output is correct
151 Correct 24 ms 14940 KB Output is correct
152 Correct 26 ms 15196 KB Output is correct
153 Correct 28 ms 15196 KB Output is correct
154 Correct 22 ms 15964 KB Output is correct
155 Correct 21 ms 15196 KB Output is correct
156 Correct 24 ms 19540 KB Output is correct
157 Correct 26 ms 18772 KB Output is correct
158 Correct 30 ms 18264 KB Output is correct
159 Correct 21 ms 14936 KB Output is correct
160 Correct 26 ms 20052 KB Output is correct
161 Correct 22 ms 14940 KB Output is correct
162 Correct 25 ms 15388 KB Output is correct