답안 #470184

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
470184 2021-09-03T07:52:31 Z AmirrezaM Subtree (INOI20_subtree) C++14
100 / 100
171 ms 23888 KB
#pragma GCC optimize("Ofast")

#include <bits/stdc++.h>

using namespace std;

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

#define vc vector
#define pb push_back

const int MAXN = 1e5 + 15 , mod = 1e9 + 7;

int n , m, go[MAXN] , lw[MAXN] , h[MAXN] , par[MAXN] , mark[MAXN];
vc<int> adj[MAXN];

int dp[MAXN] , chMul[MAXN] , ps[MAXN];

void dfs(int v , int p = -1){
  par[v] = p;
  mark[v] = 1;
  go[v] = -1 , lw[v] = MAXN;
  for(int u : adj[v]){
	if(mark[u] == 0){
	  h[u] = h[v] + 1;
	  dfs(u , v);
	  if(lw[u] <= h[v]) go[v] = u;
	  lw[v] = min(lw[v] , lw[u]);
	}
	else if(u != p and mark[u] == 1) lw[v] = h[u];
  }
  mark[v] = 2;
}
void dfsCalc(int v){
  mark[v] = 0;
  for(int u : adj[v]){
	if(mark[u] == 2) dfsCalc(u);
  }
  if(lw[v] == h[v]){
	vc<int> c;
	for(int u = v ; u > -1 ; u = go[u]){
	  c.pb(u);
	}
	
	int ml = 1 , sm = 0;
	for(int u : c){
	  chMul[u] = 1;
	  for(int k : adj[u]){
		if(par[k] == u and k != go[u]){
		  chMul[u] = (1ll * chMul[u] * (dp[k] + 1)) % mod;
		}
	  }
	  ml = (1ll * ml * chMul[u]) % mod;
	  sm += ml;
	  if(sm >= mod) sm -= mod;
	  ps[u] = sm;
	}
	
	ml = 1 , sm = 0;
	for(int i = c.size()-1 ; i >= 1 ; i--){
	  int u = c[i];
	  dp[v] = (dp[v] + 1ll * ps[par[u]] * ml) % mod;
	  ml = (1ll * ml * chMul[u]) % mod;
	}
  }
  else{
	dp[v] = 1;
	for(int u : adj[v]){
	  if(par[u] != v) continue;
	  dp[v] = (1ll * dp[v] * (dp[u] + 1)) % mod;
	}
  }
}

int main(){
  cin >> n >> m;
  for(int i = 0 ; i < m ; i++){
	int s , e;
	cin >> s >> e;
	s-- , e--;
	adj[s].pb(e) , adj[e].pb(s);
  }
  dfs(0);
  dfsCalc(0);
  int ans = 0;
  for(int i = 0 ; i < n ; i++){
	ans += dp[i];
	if(ans >= mod) ans -= mod;
  }
  cout << ans << endl;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2636 KB Output is correct
2 Correct 2 ms 2636 KB Output is correct
3 Correct 2 ms 2612 KB Output is correct
4 Correct 2 ms 2636 KB Output is correct
5 Correct 2 ms 2636 KB Output is correct
6 Correct 2 ms 2660 KB Output is correct
7 Correct 2 ms 2636 KB Output is correct
8 Correct 2 ms 2636 KB Output is correct
9 Correct 2 ms 2636 KB Output is correct
10 Correct 2 ms 2652 KB Output is correct
11 Correct 2 ms 2636 KB Output is correct
12 Correct 2 ms 2636 KB Output is correct
13 Correct 2 ms 2656 KB Output is correct
14 Correct 2 ms 2636 KB Output is correct
15 Correct 2 ms 2636 KB Output is correct
16 Correct 2 ms 2636 KB Output is correct
17 Correct 2 ms 2636 KB Output is correct
18 Correct 2 ms 2636 KB Output is correct
19 Correct 2 ms 2636 KB Output is correct
20 Correct 2 ms 2636 KB Output is correct
21 Correct 2 ms 2664 KB Output is correct
22 Correct 2 ms 2644 KB Output is correct
23 Correct 2 ms 2716 KB Output is correct
24 Correct 2 ms 2636 KB Output is correct
25 Correct 2 ms 2636 KB Output is correct
26 Correct 2 ms 2636 KB Output is correct
27 Correct 2 ms 2636 KB Output is correct
28 Correct 2 ms 2664 KB Output is correct
29 Correct 2 ms 2660 KB Output is correct
30 Correct 2 ms 2636 KB Output is correct
31 Correct 2 ms 2636 KB Output is correct
32 Correct 2 ms 2636 KB Output is correct
33 Correct 2 ms 2636 KB Output is correct
34 Correct 2 ms 2636 KB Output is correct
35 Correct 2 ms 2636 KB Output is correct
36 Correct 2 ms 2636 KB Output is correct
37 Correct 2 ms 2636 KB Output is correct
38 Correct 2 ms 2636 KB Output is correct
39 Correct 2 ms 2636 KB Output is correct
40 Correct 3 ms 2660 KB Output is correct
41 Correct 2 ms 2664 KB Output is correct
42 Correct 2 ms 2636 KB Output is correct
43 Correct 2 ms 2636 KB Output is correct
44 Correct 2 ms 2636 KB Output is correct
45 Correct 2 ms 2636 KB Output is correct
46 Correct 2 ms 2636 KB Output is correct
47 Correct 2 ms 2636 KB Output is correct
48 Correct 2 ms 2636 KB Output is correct
49 Correct 2 ms 2636 KB Output is correct
50 Correct 2 ms 2636 KB Output is correct
51 Correct 2 ms 2764 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2636 KB Output is correct
2 Correct 11 ms 3304 KB Output is correct
3 Correct 123 ms 9284 KB Output is correct
4 Correct 110 ms 9268 KB Output is correct
5 Correct 126 ms 9364 KB Output is correct
6 Correct 105 ms 9284 KB Output is correct
7 Correct 130 ms 20664 KB Output is correct
8 Correct 124 ms 16320 KB Output is correct
9 Correct 132 ms 16444 KB Output is correct
10 Correct 110 ms 9888 KB Output is correct
11 Correct 109 ms 9476 KB Output is correct
12 Correct 127 ms 9216 KB Output is correct
13 Correct 109 ms 9228 KB Output is correct
14 Correct 109 ms 14152 KB Output is correct
15 Correct 135 ms 22272 KB Output is correct
16 Correct 148 ms 23888 KB Output is correct
17 Correct 122 ms 9260 KB Output is correct
18 Correct 110 ms 9280 KB Output is correct
19 Correct 111 ms 9356 KB Output is correct
20 Correct 91 ms 9536 KB Output is correct
21 Correct 109 ms 9668 KB Output is correct
22 Correct 97 ms 9628 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2636 KB Output is correct
2 Correct 2 ms 2636 KB Output is correct
3 Correct 2 ms 2612 KB Output is correct
4 Correct 2 ms 2636 KB Output is correct
5 Correct 2 ms 2636 KB Output is correct
6 Correct 2 ms 2660 KB Output is correct
7 Correct 2 ms 2636 KB Output is correct
8 Correct 2 ms 2636 KB Output is correct
9 Correct 2 ms 2636 KB Output is correct
10 Correct 2 ms 2652 KB Output is correct
11 Correct 2 ms 2636 KB Output is correct
12 Correct 2 ms 2636 KB Output is correct
13 Correct 2 ms 2656 KB Output is correct
14 Correct 2 ms 2636 KB Output is correct
15 Correct 2 ms 2636 KB Output is correct
16 Correct 2 ms 2636 KB Output is correct
17 Correct 2 ms 2636 KB Output is correct
18 Correct 2 ms 2636 KB Output is correct
19 Correct 2 ms 2636 KB Output is correct
20 Correct 2 ms 2636 KB Output is correct
21 Correct 2 ms 2664 KB Output is correct
22 Correct 2 ms 2644 KB Output is correct
23 Correct 2 ms 2716 KB Output is correct
24 Correct 2 ms 2636 KB Output is correct
25 Correct 2 ms 2636 KB Output is correct
26 Correct 2 ms 2636 KB Output is correct
27 Correct 2 ms 2636 KB Output is correct
28 Correct 2 ms 2664 KB Output is correct
29 Correct 2 ms 2660 KB Output is correct
30 Correct 2 ms 2636 KB Output is correct
31 Correct 2 ms 2636 KB Output is correct
32 Correct 2 ms 2636 KB Output is correct
33 Correct 2 ms 2636 KB Output is correct
34 Correct 2 ms 2636 KB Output is correct
35 Correct 2 ms 2636 KB Output is correct
36 Correct 2 ms 2636 KB Output is correct
37 Correct 2 ms 2636 KB Output is correct
38 Correct 2 ms 2636 KB Output is correct
39 Correct 2 ms 2636 KB Output is correct
40 Correct 3 ms 2660 KB Output is correct
41 Correct 2 ms 2664 KB Output is correct
42 Correct 2 ms 2636 KB Output is correct
43 Correct 2 ms 2636 KB Output is correct
44 Correct 2 ms 2636 KB Output is correct
45 Correct 2 ms 2636 KB Output is correct
46 Correct 2 ms 2636 KB Output is correct
47 Correct 2 ms 2636 KB Output is correct
48 Correct 2 ms 2636 KB Output is correct
49 Correct 2 ms 2636 KB Output is correct
50 Correct 2 ms 2636 KB Output is correct
51 Correct 2 ms 2764 KB Output is correct
52 Correct 6 ms 3020 KB Output is correct
53 Correct 6 ms 3020 KB Output is correct
54 Correct 7 ms 3020 KB Output is correct
55 Correct 6 ms 3020 KB Output is correct
56 Correct 9 ms 3576 KB Output is correct
57 Correct 6 ms 3020 KB Output is correct
58 Correct 6 ms 3020 KB Output is correct
59 Correct 6 ms 3276 KB Output is correct
60 Correct 5 ms 3020 KB Output is correct
61 Correct 7 ms 3148 KB Output is correct
62 Correct 7 ms 3020 KB Output is correct
63 Correct 6 ms 3020 KB Output is correct
64 Correct 5 ms 3020 KB Output is correct
65 Correct 6 ms 3020 KB Output is correct
66 Correct 6 ms 3020 KB Output is correct
67 Correct 6 ms 3020 KB Output is correct
68 Correct 7 ms 3404 KB Output is correct
69 Correct 8 ms 3020 KB Output is correct
70 Correct 5 ms 3020 KB Output is correct
71 Correct 6 ms 2948 KB Output is correct
72 Correct 7 ms 3020 KB Output is correct
73 Correct 5 ms 3020 KB Output is correct
74 Correct 6 ms 3020 KB Output is correct
75 Correct 7 ms 3276 KB Output is correct
76 Correct 8 ms 3020 KB Output is correct
77 Correct 7 ms 3568 KB Output is correct
78 Correct 6 ms 3132 KB Output is correct
79 Correct 7 ms 3452 KB Output is correct
80 Correct 7 ms 3020 KB Output is correct
81 Correct 6 ms 3020 KB Output is correct
82 Correct 6 ms 3020 KB Output is correct
83 Correct 6 ms 3020 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2636 KB Output is correct
2 Correct 2 ms 2636 KB Output is correct
3 Correct 2 ms 2612 KB Output is correct
4 Correct 2 ms 2636 KB Output is correct
5 Correct 2 ms 2636 KB Output is correct
6 Correct 2 ms 2660 KB Output is correct
7 Correct 2 ms 2636 KB Output is correct
8 Correct 2 ms 2636 KB Output is correct
9 Correct 2 ms 2636 KB Output is correct
10 Correct 2 ms 2652 KB Output is correct
11 Correct 2 ms 2636 KB Output is correct
12 Correct 2 ms 2636 KB Output is correct
13 Correct 2 ms 2656 KB Output is correct
14 Correct 2 ms 2636 KB Output is correct
15 Correct 2 ms 2636 KB Output is correct
16 Correct 2 ms 2636 KB Output is correct
17 Correct 2 ms 2636 KB Output is correct
18 Correct 2 ms 2636 KB Output is correct
19 Correct 2 ms 2636 KB Output is correct
20 Correct 2 ms 2636 KB Output is correct
21 Correct 2 ms 2664 KB Output is correct
22 Correct 2 ms 2644 KB Output is correct
23 Correct 2 ms 2716 KB Output is correct
24 Correct 2 ms 2636 KB Output is correct
25 Correct 2 ms 2636 KB Output is correct
26 Correct 2 ms 2636 KB Output is correct
27 Correct 2 ms 2636 KB Output is correct
28 Correct 2 ms 2664 KB Output is correct
29 Correct 2 ms 2660 KB Output is correct
30 Correct 2 ms 2636 KB Output is correct
31 Correct 2 ms 2636 KB Output is correct
32 Correct 2 ms 2636 KB Output is correct
33 Correct 2 ms 2636 KB Output is correct
34 Correct 2 ms 2636 KB Output is correct
35 Correct 2 ms 2636 KB Output is correct
36 Correct 2 ms 2636 KB Output is correct
37 Correct 2 ms 2636 KB Output is correct
38 Correct 2 ms 2636 KB Output is correct
39 Correct 2 ms 2636 KB Output is correct
40 Correct 3 ms 2660 KB Output is correct
41 Correct 2 ms 2664 KB Output is correct
42 Correct 2 ms 2636 KB Output is correct
43 Correct 2 ms 2636 KB Output is correct
44 Correct 2 ms 2636 KB Output is correct
45 Correct 2 ms 2636 KB Output is correct
46 Correct 2 ms 2636 KB Output is correct
47 Correct 2 ms 2636 KB Output is correct
48 Correct 2 ms 2636 KB Output is correct
49 Correct 2 ms 2636 KB Output is correct
50 Correct 2 ms 2636 KB Output is correct
51 Correct 2 ms 2764 KB Output is correct
52 Correct 2 ms 2636 KB Output is correct
53 Correct 11 ms 3304 KB Output is correct
54 Correct 123 ms 9284 KB Output is correct
55 Correct 110 ms 9268 KB Output is correct
56 Correct 126 ms 9364 KB Output is correct
57 Correct 105 ms 9284 KB Output is correct
58 Correct 130 ms 20664 KB Output is correct
59 Correct 124 ms 16320 KB Output is correct
60 Correct 132 ms 16444 KB Output is correct
61 Correct 110 ms 9888 KB Output is correct
62 Correct 109 ms 9476 KB Output is correct
63 Correct 127 ms 9216 KB Output is correct
64 Correct 109 ms 9228 KB Output is correct
65 Correct 109 ms 14152 KB Output is correct
66 Correct 135 ms 22272 KB Output is correct
67 Correct 148 ms 23888 KB Output is correct
68 Correct 122 ms 9260 KB Output is correct
69 Correct 110 ms 9280 KB Output is correct
70 Correct 111 ms 9356 KB Output is correct
71 Correct 91 ms 9536 KB Output is correct
72 Correct 109 ms 9668 KB Output is correct
73 Correct 97 ms 9628 KB Output is correct
74 Correct 6 ms 3020 KB Output is correct
75 Correct 6 ms 3020 KB Output is correct
76 Correct 7 ms 3020 KB Output is correct
77 Correct 6 ms 3020 KB Output is correct
78 Correct 9 ms 3576 KB Output is correct
79 Correct 6 ms 3020 KB Output is correct
80 Correct 6 ms 3020 KB Output is correct
81 Correct 6 ms 3276 KB Output is correct
82 Correct 5 ms 3020 KB Output is correct
83 Correct 7 ms 3148 KB Output is correct
84 Correct 7 ms 3020 KB Output is correct
85 Correct 6 ms 3020 KB Output is correct
86 Correct 5 ms 3020 KB Output is correct
87 Correct 6 ms 3020 KB Output is correct
88 Correct 6 ms 3020 KB Output is correct
89 Correct 6 ms 3020 KB Output is correct
90 Correct 7 ms 3404 KB Output is correct
91 Correct 8 ms 3020 KB Output is correct
92 Correct 5 ms 3020 KB Output is correct
93 Correct 6 ms 2948 KB Output is correct
94 Correct 7 ms 3020 KB Output is correct
95 Correct 5 ms 3020 KB Output is correct
96 Correct 6 ms 3020 KB Output is correct
97 Correct 7 ms 3276 KB Output is correct
98 Correct 8 ms 3020 KB Output is correct
99 Correct 7 ms 3568 KB Output is correct
100 Correct 6 ms 3132 KB Output is correct
101 Correct 7 ms 3452 KB Output is correct
102 Correct 7 ms 3020 KB Output is correct
103 Correct 6 ms 3020 KB Output is correct
104 Correct 6 ms 3020 KB Output is correct
105 Correct 6 ms 3020 KB Output is correct
106 Correct 114 ms 10208 KB Output is correct
107 Correct 122 ms 10184 KB Output is correct
108 Correct 126 ms 10240 KB Output is correct
109 Correct 125 ms 10256 KB Output is correct
110 Correct 96 ms 10424 KB Output is correct
111 Correct 120 ms 11844 KB Output is correct
112 Correct 151 ms 18980 KB Output is correct
113 Correct 128 ms 10180 KB Output is correct
114 Correct 126 ms 10240 KB Output is correct
115 Correct 92 ms 9552 KB Output is correct
116 Correct 142 ms 20424 KB Output is correct
117 Correct 147 ms 18788 KB Output is correct
118 Correct 92 ms 11656 KB Output is correct
119 Correct 128 ms 10240 KB Output is correct
120 Correct 130 ms 10164 KB Output is correct
121 Correct 143 ms 10360 KB Output is correct
122 Correct 143 ms 10460 KB Output is correct
123 Correct 101 ms 9976 KB Output is correct
124 Correct 91 ms 9736 KB Output is correct
125 Correct 86 ms 9704 KB Output is correct
126 Correct 114 ms 10176 KB Output is correct
127 Correct 118 ms 10256 KB Output is correct
128 Correct 129 ms 10304 KB Output is correct
129 Correct 123 ms 10280 KB Output is correct
130 Correct 95 ms 10388 KB Output is correct
131 Correct 111 ms 11844 KB Output is correct
132 Correct 148 ms 18728 KB Output is correct
133 Correct 121 ms 10188 KB Output is correct
134 Correct 130 ms 10108 KB Output is correct
135 Correct 94 ms 9720 KB Output is correct
136 Correct 137 ms 18004 KB Output is correct
137 Correct 140 ms 18388 KB Output is correct
138 Correct 93 ms 11504 KB Output is correct
139 Correct 125 ms 10180 KB Output is correct
140 Correct 134 ms 10316 KB Output is correct
141 Correct 137 ms 10296 KB Output is correct
142 Correct 137 ms 10268 KB Output is correct
143 Correct 91 ms 9924 KB Output is correct
144 Correct 91 ms 9820 KB Output is correct
145 Correct 86 ms 9808 KB Output is correct
146 Correct 126 ms 10088 KB Output is correct
147 Correct 121 ms 10172 KB Output is correct
148 Correct 126 ms 10788 KB Output is correct
149 Correct 131 ms 12372 KB Output is correct
150 Correct 151 ms 21836 KB Output is correct
151 Correct 160 ms 10300 KB Output is correct
152 Correct 149 ms 10460 KB Output is correct
153 Correct 171 ms 10708 KB Output is correct
154 Correct 135 ms 12188 KB Output is correct
155 Correct 101 ms 10692 KB Output is correct
156 Correct 139 ms 19524 KB Output is correct
157 Correct 143 ms 18372 KB Output is correct
158 Correct 136 ms 16980 KB Output is correct
159 Correct 125 ms 10120 KB Output is correct
160 Correct 126 ms 20488 KB Output is correct
161 Correct 137 ms 10216 KB Output is correct
162 Correct 99 ms 10564 KB Output is correct