Submission #635429

# Submission time Handle Problem Language Result Execution time Memory
635429 2022-08-26T08:57:01 Z NothingXD Subtree (INOI20_subtree) C++17
100 / 100
98 ms 25012 KB
#include<bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

void debug_out() { cerr << endl; }

template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
	cerr << " " << H;
	debug_out(T...);
}

#define debug(...) cerr << "(" << #__VA_ARGS__ << "):", debug_out(__VA_ARGS__)
#define all(x) x.begin(), x.end()
#define MP(x, y) make_pair(x, y)
#define F first
#define S second

const int maxn = 1e5 + 10;
const int mod = 1e9 + 7;

int n, m, dp[maxn][4], h[maxn], par[maxn];
vector<int> g[maxn], ver[maxn];
bool vis[maxn];

int getpar(int v){
	return (par[v] < 0? v: par[v] = getpar(par[v]));
}

void merge(int u, int v){
	if ((u = getpar(u)) == (v = getpar(v))) return;
	if (par[u] > par[v]) swap(u, v);
	par[u] += par[v];
	par[v] = u;
}

int dfs(int v, int p = -1){
	vis[v] = true;
	int mn = h[v];
	for (auto u: g[v]){
		if (!vis[u]){
			h[u] = h[v] + 1;
			mn = min(mn, dfs(u, v));
		}
		if (u != p){
			mn = min(mn, h[u]);
		}
	}
	if (mn < h[v]){
		merge(v, p);
	}
	return mn;
}

int add(int x, int y){
	int res = x + y;
	if (res >= mod) return res - mod;
	if (res < 0) return res + mod;
	return res;
}

int mul(int x, int y){
	return 1ll * x * y % mod;
}

bool cmp(int x, int y){
	return h[x] < h[y];
}

// dp[v][0] = ans
// dp[v][1] = bedone dost
// dp[v][2] = fagat dost khodet nisti
// dp[v][3] = dost hast khodet hasti bagie ham mitunan bashan

void solve(int v, int p = -1){
	vis[v] = true;
	dp[v][0] = dp[v][1] = dp[v][3] = 1;
	bool flg = false;
	for (auto u: g[v]){
		if (!vis[u]){
			solve(u, v);
			if (getpar(u) == getpar(v)){
				dp[v][0] = mul(dp[v][0], dp[u][0] + 1);
				dp[v][3] = mul(dp[v][3], dp[u][3]);
				dp[v][2] = add(dp[u][2], dp[u][3]);
			}
			else{
				dp[v][0] = mul(dp[v][0], dp[u][0] + 1);
				dp[v][1] = mul(dp[v][1], dp[u][0] + 1);
				dp[v][3] = mul(dp[v][3], dp[u][0] + 1);
			}
		}
		else if (u != p && h[u] > h[v]){
			flg = true;
		}
	}
	if (flg){
		int tmp = getpar(v);
		sort(all(ver[tmp]), cmp);
		int k = dp[v][1];
		for (int i = 1; i < ver[tmp].size(); i++){
			dp[v][0] = add(dp[v][0], mul(k, dp[ver[tmp][i]][2]));
			k = mul(k, dp[ver[tmp][i]][1]);
		}
		dp[v][0] = add(dp[v][0], -k);
	}
}

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

	memset(par, -1, sizeof par);

	cin >> n >> m;

	for (int i = 1; i <= m; i++){
		int u, v; cin >> u >> v;
		g[u].push_back(v);
		g[v].push_back(u);
	}

	dfs(1);
	for (int i = 1; i <= n; i++){
		int tmp = getpar(i);
		ver[tmp].push_back(i);
	}
	memset(vis, false, sizeof vis);
	solve(1);

	int ans = 0;
	for (int i = 1; i <= n; i++) ans = add(ans, dp[i][0]);

	cout << ans << '\n';

	return 0;
}

Compilation message

Main.cpp: In function 'void solve(int, int)':
Main.cpp:106:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  106 |   for (int i = 1; i < ver[tmp].size(); i++){
      |                   ~~^~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5460 KB Output is correct
2 Correct 3 ms 5428 KB Output is correct
3 Correct 3 ms 5460 KB Output is correct
4 Correct 3 ms 5460 KB Output is correct
5 Correct 3 ms 5436 KB Output is correct
6 Correct 3 ms 5460 KB Output is correct
7 Correct 3 ms 5460 KB Output is correct
8 Correct 3 ms 5460 KB Output is correct
9 Correct 3 ms 5460 KB Output is correct
10 Correct 3 ms 5460 KB Output is correct
11 Correct 3 ms 5460 KB Output is correct
12 Correct 3 ms 5460 KB Output is correct
13 Correct 3 ms 5460 KB Output is correct
14 Correct 3 ms 5460 KB Output is correct
15 Correct 3 ms 5460 KB Output is correct
16 Correct 3 ms 5436 KB Output is correct
17 Correct 3 ms 5460 KB Output is correct
18 Correct 3 ms 5432 KB Output is correct
19 Correct 3 ms 5460 KB Output is correct
20 Correct 3 ms 5488 KB Output is correct
21 Correct 3 ms 5460 KB Output is correct
22 Correct 3 ms 5460 KB Output is correct
23 Correct 3 ms 5460 KB Output is correct
24 Correct 3 ms 5460 KB Output is correct
25 Correct 4 ms 5456 KB Output is correct
26 Correct 3 ms 5460 KB Output is correct
27 Correct 3 ms 5460 KB Output is correct
28 Correct 3 ms 5460 KB Output is correct
29 Correct 3 ms 5460 KB Output is correct
30 Correct 3 ms 5460 KB Output is correct
31 Correct 3 ms 5460 KB Output is correct
32 Correct 3 ms 5412 KB Output is correct
33 Correct 3 ms 5460 KB Output is correct
34 Correct 3 ms 5460 KB Output is correct
35 Correct 3 ms 5460 KB Output is correct
36 Correct 3 ms 5460 KB Output is correct
37 Correct 3 ms 5460 KB Output is correct
38 Correct 3 ms 5460 KB Output is correct
39 Correct 3 ms 5460 KB Output is correct
40 Correct 3 ms 5460 KB Output is correct
41 Correct 3 ms 5424 KB Output is correct
42 Correct 3 ms 5460 KB Output is correct
43 Correct 3 ms 5460 KB Output is correct
44 Correct 3 ms 5460 KB Output is correct
45 Correct 3 ms 5428 KB Output is correct
46 Correct 3 ms 5464 KB Output is correct
47 Correct 3 ms 5428 KB Output is correct
48 Correct 3 ms 5460 KB Output is correct
49 Correct 3 ms 5460 KB Output is correct
50 Correct 3 ms 5508 KB Output is correct
51 Correct 3 ms 5460 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5460 KB Output is correct
2 Correct 7 ms 6344 KB Output is correct
3 Correct 62 ms 14924 KB Output is correct
4 Correct 54 ms 14924 KB Output is correct
5 Correct 54 ms 14880 KB Output is correct
6 Correct 52 ms 14908 KB Output is correct
7 Correct 77 ms 22784 KB Output is correct
8 Correct 81 ms 19856 KB Output is correct
9 Correct 83 ms 19812 KB Output is correct
10 Correct 47 ms 15472 KB Output is correct
11 Correct 46 ms 15064 KB Output is correct
12 Correct 63 ms 14904 KB Output is correct
13 Correct 56 ms 14796 KB Output is correct
14 Correct 51 ms 18376 KB Output is correct
15 Correct 75 ms 24012 KB Output is correct
16 Correct 81 ms 25012 KB Output is correct
17 Correct 68 ms 14884 KB Output is correct
18 Correct 55 ms 14960 KB Output is correct
19 Correct 57 ms 14836 KB Output is correct
20 Correct 40 ms 15220 KB Output is correct
21 Correct 44 ms 15444 KB Output is correct
22 Correct 50 ms 15288 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5460 KB Output is correct
2 Correct 3 ms 5428 KB Output is correct
3 Correct 3 ms 5460 KB Output is correct
4 Correct 3 ms 5460 KB Output is correct
5 Correct 3 ms 5436 KB Output is correct
6 Correct 3 ms 5460 KB Output is correct
7 Correct 3 ms 5460 KB Output is correct
8 Correct 3 ms 5460 KB Output is correct
9 Correct 3 ms 5460 KB Output is correct
10 Correct 3 ms 5460 KB Output is correct
11 Correct 3 ms 5460 KB Output is correct
12 Correct 3 ms 5460 KB Output is correct
13 Correct 3 ms 5460 KB Output is correct
14 Correct 3 ms 5460 KB Output is correct
15 Correct 3 ms 5460 KB Output is correct
16 Correct 3 ms 5436 KB Output is correct
17 Correct 3 ms 5460 KB Output is correct
18 Correct 3 ms 5432 KB Output is correct
19 Correct 3 ms 5460 KB Output is correct
20 Correct 3 ms 5488 KB Output is correct
21 Correct 3 ms 5460 KB Output is correct
22 Correct 3 ms 5460 KB Output is correct
23 Correct 3 ms 5460 KB Output is correct
24 Correct 3 ms 5460 KB Output is correct
25 Correct 4 ms 5456 KB Output is correct
26 Correct 3 ms 5460 KB Output is correct
27 Correct 3 ms 5460 KB Output is correct
28 Correct 3 ms 5460 KB Output is correct
29 Correct 3 ms 5460 KB Output is correct
30 Correct 3 ms 5460 KB Output is correct
31 Correct 3 ms 5460 KB Output is correct
32 Correct 3 ms 5412 KB Output is correct
33 Correct 3 ms 5460 KB Output is correct
34 Correct 3 ms 5460 KB Output is correct
35 Correct 3 ms 5460 KB Output is correct
36 Correct 3 ms 5460 KB Output is correct
37 Correct 3 ms 5460 KB Output is correct
38 Correct 3 ms 5460 KB Output is correct
39 Correct 3 ms 5460 KB Output is correct
40 Correct 3 ms 5460 KB Output is correct
41 Correct 3 ms 5424 KB Output is correct
42 Correct 3 ms 5460 KB Output is correct
43 Correct 3 ms 5460 KB Output is correct
44 Correct 3 ms 5460 KB Output is correct
45 Correct 3 ms 5428 KB Output is correct
46 Correct 3 ms 5464 KB Output is correct
47 Correct 3 ms 5428 KB Output is correct
48 Correct 3 ms 5460 KB Output is correct
49 Correct 3 ms 5460 KB Output is correct
50 Correct 3 ms 5508 KB Output is correct
51 Correct 3 ms 5460 KB Output is correct
52 Correct 5 ms 5972 KB Output is correct
53 Correct 6 ms 5952 KB Output is correct
54 Correct 5 ms 5868 KB Output is correct
55 Correct 4 ms 5972 KB Output is correct
56 Correct 6 ms 6228 KB Output is correct
57 Correct 4 ms 5972 KB Output is correct
58 Correct 4 ms 5952 KB Output is correct
59 Correct 5 ms 6100 KB Output is correct
60 Correct 5 ms 5956 KB Output is correct
61 Correct 5 ms 5844 KB Output is correct
62 Correct 5 ms 5844 KB Output is correct
63 Correct 4 ms 5972 KB Output is correct
64 Correct 4 ms 5972 KB Output is correct
65 Correct 5 ms 5972 KB Output is correct
66 Correct 5 ms 5952 KB Output is correct
67 Correct 7 ms 5952 KB Output is correct
68 Correct 7 ms 6100 KB Output is correct
69 Correct 6 ms 5952 KB Output is correct
70 Correct 4 ms 5896 KB Output is correct
71 Correct 6 ms 5844 KB Output is correct
72 Correct 5 ms 5844 KB Output is correct
73 Correct 4 ms 5972 KB Output is correct
74 Correct 5 ms 5820 KB Output is correct
75 Correct 5 ms 6100 KB Output is correct
76 Correct 5 ms 5828 KB Output is correct
77 Correct 6 ms 6208 KB Output is correct
78 Correct 5 ms 5844 KB Output is correct
79 Correct 5 ms 6100 KB Output is correct
80 Correct 5 ms 5844 KB Output is correct
81 Correct 7 ms 5952 KB Output is correct
82 Correct 5 ms 5972 KB Output is correct
83 Correct 5 ms 5972 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5460 KB Output is correct
2 Correct 3 ms 5428 KB Output is correct
3 Correct 3 ms 5460 KB Output is correct
4 Correct 3 ms 5460 KB Output is correct
5 Correct 3 ms 5436 KB Output is correct
6 Correct 3 ms 5460 KB Output is correct
7 Correct 3 ms 5460 KB Output is correct
8 Correct 3 ms 5460 KB Output is correct
9 Correct 3 ms 5460 KB Output is correct
10 Correct 3 ms 5460 KB Output is correct
11 Correct 3 ms 5460 KB Output is correct
12 Correct 3 ms 5460 KB Output is correct
13 Correct 3 ms 5460 KB Output is correct
14 Correct 3 ms 5460 KB Output is correct
15 Correct 3 ms 5460 KB Output is correct
16 Correct 3 ms 5436 KB Output is correct
17 Correct 3 ms 5460 KB Output is correct
18 Correct 3 ms 5432 KB Output is correct
19 Correct 3 ms 5460 KB Output is correct
20 Correct 3 ms 5488 KB Output is correct
21 Correct 3 ms 5460 KB Output is correct
22 Correct 3 ms 5460 KB Output is correct
23 Correct 3 ms 5460 KB Output is correct
24 Correct 3 ms 5460 KB Output is correct
25 Correct 4 ms 5456 KB Output is correct
26 Correct 3 ms 5460 KB Output is correct
27 Correct 3 ms 5460 KB Output is correct
28 Correct 3 ms 5460 KB Output is correct
29 Correct 3 ms 5460 KB Output is correct
30 Correct 3 ms 5460 KB Output is correct
31 Correct 3 ms 5460 KB Output is correct
32 Correct 3 ms 5412 KB Output is correct
33 Correct 3 ms 5460 KB Output is correct
34 Correct 3 ms 5460 KB Output is correct
35 Correct 3 ms 5460 KB Output is correct
36 Correct 3 ms 5460 KB Output is correct
37 Correct 3 ms 5460 KB Output is correct
38 Correct 3 ms 5460 KB Output is correct
39 Correct 3 ms 5460 KB Output is correct
40 Correct 3 ms 5460 KB Output is correct
41 Correct 3 ms 5424 KB Output is correct
42 Correct 3 ms 5460 KB Output is correct
43 Correct 3 ms 5460 KB Output is correct
44 Correct 3 ms 5460 KB Output is correct
45 Correct 3 ms 5428 KB Output is correct
46 Correct 3 ms 5464 KB Output is correct
47 Correct 3 ms 5428 KB Output is correct
48 Correct 3 ms 5460 KB Output is correct
49 Correct 3 ms 5460 KB Output is correct
50 Correct 3 ms 5508 KB Output is correct
51 Correct 3 ms 5460 KB Output is correct
52 Correct 3 ms 5460 KB Output is correct
53 Correct 7 ms 6344 KB Output is correct
54 Correct 62 ms 14924 KB Output is correct
55 Correct 54 ms 14924 KB Output is correct
56 Correct 54 ms 14880 KB Output is correct
57 Correct 52 ms 14908 KB Output is correct
58 Correct 77 ms 22784 KB Output is correct
59 Correct 81 ms 19856 KB Output is correct
60 Correct 83 ms 19812 KB Output is correct
61 Correct 47 ms 15472 KB Output is correct
62 Correct 46 ms 15064 KB Output is correct
63 Correct 63 ms 14904 KB Output is correct
64 Correct 56 ms 14796 KB Output is correct
65 Correct 51 ms 18376 KB Output is correct
66 Correct 75 ms 24012 KB Output is correct
67 Correct 81 ms 25012 KB Output is correct
68 Correct 68 ms 14884 KB Output is correct
69 Correct 55 ms 14960 KB Output is correct
70 Correct 57 ms 14836 KB Output is correct
71 Correct 40 ms 15220 KB Output is correct
72 Correct 44 ms 15444 KB Output is correct
73 Correct 50 ms 15288 KB Output is correct
74 Correct 5 ms 5972 KB Output is correct
75 Correct 6 ms 5952 KB Output is correct
76 Correct 5 ms 5868 KB Output is correct
77 Correct 4 ms 5972 KB Output is correct
78 Correct 6 ms 6228 KB Output is correct
79 Correct 4 ms 5972 KB Output is correct
80 Correct 4 ms 5952 KB Output is correct
81 Correct 5 ms 6100 KB Output is correct
82 Correct 5 ms 5956 KB Output is correct
83 Correct 5 ms 5844 KB Output is correct
84 Correct 5 ms 5844 KB Output is correct
85 Correct 4 ms 5972 KB Output is correct
86 Correct 4 ms 5972 KB Output is correct
87 Correct 5 ms 5972 KB Output is correct
88 Correct 5 ms 5952 KB Output is correct
89 Correct 7 ms 5952 KB Output is correct
90 Correct 7 ms 6100 KB Output is correct
91 Correct 6 ms 5952 KB Output is correct
92 Correct 4 ms 5896 KB Output is correct
93 Correct 6 ms 5844 KB Output is correct
94 Correct 5 ms 5844 KB Output is correct
95 Correct 4 ms 5972 KB Output is correct
96 Correct 5 ms 5820 KB Output is correct
97 Correct 5 ms 6100 KB Output is correct
98 Correct 5 ms 5828 KB Output is correct
99 Correct 6 ms 6208 KB Output is correct
100 Correct 5 ms 5844 KB Output is correct
101 Correct 5 ms 6100 KB Output is correct
102 Correct 5 ms 5844 KB Output is correct
103 Correct 7 ms 5952 KB Output is correct
104 Correct 5 ms 5972 KB Output is correct
105 Correct 5 ms 5972 KB Output is correct
106 Correct 69 ms 14668 KB Output is correct
107 Correct 71 ms 14508 KB Output is correct
108 Correct 66 ms 14412 KB Output is correct
109 Correct 62 ms 14388 KB Output is correct
110 Correct 52 ms 15396 KB Output is correct
111 Correct 57 ms 16064 KB Output is correct
112 Correct 82 ms 19544 KB Output is correct
113 Correct 82 ms 13632 KB Output is correct
114 Correct 69 ms 14048 KB Output is correct
115 Correct 39 ms 15156 KB Output is correct
116 Correct 78 ms 20736 KB Output is correct
117 Correct 80 ms 19552 KB Output is correct
118 Correct 42 ms 15940 KB Output is correct
119 Correct 74 ms 13640 KB Output is correct
120 Correct 80 ms 13480 KB Output is correct
121 Correct 84 ms 13372 KB Output is correct
122 Correct 98 ms 13352 KB Output is correct
123 Correct 44 ms 15328 KB Output is correct
124 Correct 42 ms 15356 KB Output is correct
125 Correct 39 ms 15272 KB Output is correct
126 Correct 60 ms 14752 KB Output is correct
127 Correct 58 ms 14516 KB Output is correct
128 Correct 76 ms 14392 KB Output is correct
129 Correct 72 ms 14344 KB Output is correct
130 Correct 46 ms 15308 KB Output is correct
131 Correct 52 ms 15928 KB Output is correct
132 Correct 79 ms 19424 KB Output is correct
133 Correct 66 ms 13632 KB Output is correct
134 Correct 80 ms 14104 KB Output is correct
135 Correct 42 ms 15160 KB Output is correct
136 Correct 82 ms 19096 KB Output is correct
137 Correct 79 ms 19288 KB Output is correct
138 Correct 41 ms 15792 KB Output is correct
139 Correct 67 ms 13684 KB Output is correct
140 Correct 82 ms 13496 KB Output is correct
141 Correct 89 ms 13444 KB Output is correct
142 Correct 73 ms 13360 KB Output is correct
143 Correct 46 ms 15288 KB Output is correct
144 Correct 41 ms 15388 KB Output is correct
145 Correct 38 ms 15276 KB Output is correct
146 Correct 84 ms 12772 KB Output is correct
147 Correct 80 ms 12536 KB Output is correct
148 Correct 62 ms 12880 KB Output is correct
149 Correct 78 ms 13860 KB Output is correct
150 Correct 72 ms 20744 KB Output is correct
151 Correct 76 ms 12748 KB Output is correct
152 Correct 95 ms 13132 KB Output is correct
153 Correct 79 ms 13164 KB Output is correct
154 Correct 66 ms 14672 KB Output is correct
155 Correct 45 ms 15236 KB Output is correct
156 Correct 74 ms 18892 KB Output is correct
157 Correct 94 ms 18180 KB Output is correct
158 Correct 73 ms 17192 KB Output is correct
159 Correct 56 ms 14704 KB Output is correct
160 Correct 70 ms 21880 KB Output is correct
161 Correct 56 ms 14668 KB Output is correct
162 Correct 54 ms 15180 KB Output is correct