Submission #636244

# Submission time Handle Problem Language Result Execution time Memory
636244 2022-08-28T15:20:35 Z amirhoseinfar1385 Subtree (INOI20_subtree) C++17
100 / 100
139 ms 25164 KB
#include<bits/stdc++.h>
using namespace std;
long long n,m;
vector<vector<long long>>adj;
vector<long long>par;
vector<long long>vaz;
vector<long long>res,resa[2][2],we;
vector<bool>vis;
long long mod=1e9+7;
void solvef(long long u){
	if(we[u]!=0){
		vaz[u]=1;
	}
	vis[u]=1;
	vector<long long>adja;
	for(int i=0;i<adj[u].size();i++){
		if(vis[adj[u][i]]==0){
			adja.push_back(adj[u][i]);
		}
	}
	adj[u]=adja;
	long long counta=0;
	adja.clear();
	for(int i=0;i<adj[u].size();i++){
		counta++;
		we[adj[u][i]]++;
	}
	for(int i=0;i<adj[u].size();i++){
		if(vis[adj[u][i]]==0){
			we[adj[u][i]]--;
			counta--;
			solvef(adj[u][i]);
			we[u]+=we[adj[u][i]];
			adja.push_back(adj[u][i]);
			if(we[adj[u][i]]>0){
				swap(adja[0],adja[adja.size()-1]);
			}
		}
	}
	if(counta!=0){
		vaz[u]=2;
	}
	we[u]-=counta;
	adj[u]=adja;
}

void solves(long long u){
	vis[u]=1;
	for(int i=0;i<adj[u].size();i++){
		solves(adj[u][i]);
	}
	if(vaz[u]==1){
		for(int i=0;i<adj[u].size();i++){
			res[u]*=res[adj[u][i]]+1;
			res[u]%=mod;
		}
		resa[1][0][u]=res[u];
		resa[0][1][u]=1;
		return ;
	}
	if(vaz[u]==0){
		for(int i=0;i<adj[u].size();i++){
			res[u]*=res[adj[u][i]]+1;
			res[u]%=mod;
		}
		for(int i=0;i<adj[u].size();i++){
			if(i==0){
				resa[1][0][u]+=resa[1][0][adj[u][i]];
				resa[1][1][u]+=resa[1][1][adj[u][i]]+resa[0][1][adj[u][i]];
				resa[0][1][u]+=resa[1][0][adj[u][i]]+resa[0][1][adj[u][i]];
				resa[1][0][u]%=mod;
				resa[1][1][u]%=mod;
				resa[0][1][u]%=mod;
				continue;
			}
			resa[1][1][u]*=res[adj[u][i]]+1;
			resa[1][1][u]%=mod;
			resa[1][0][u]*=res[adj[u][i]]+1;
			resa[1][0][u]%=mod;
		}
		return ;
	}
	if(vaz[u]==2){
		res[u]--;
		for(int i=0;i<adj[u].size();i++){
			if(i==0){
				res[u]+=resa[1][1][adj[u][i]]+resa[0][1][adj[u][i]];
				res[u]%=mod;
				continue;
			}
			res[u]*=res[adj[u][i]]+1;
			res[u]%=mod;
		}
		return ;
	}
}

int main(){
	cin>>n>>m;
	adj.resize(n+1);
	par.resize(n+1);
	vis.resize(n+1);
	vaz.resize(n+1);
	res.resize(n+1,1);
	for(int i=0;i<2;i++){
		for(int j=0;j<2;j++){
			resa[i][j].resize(n+1);
		}
	}
	we.resize(n+1);
	for(int i=0;i<m;i++){
		long long u,v;
		cin>>u>>v;
		adj[u].push_back(v);
		adj[v].push_back(u);
	}
	solvef(1);
	vis.clear();
	vis.resize(n+1);
	solves(1);
	long long mainres=0;
	for(int i=1;i<=n;i++){
	//	cout<<i<<" "<<res[i]<<" "<<resa[1][0][i]<<" "<<resa[1][1][i]<<" "<<resa[0][1][i]<<" "<<vaz[i]<<endl;
		mainres+=res[i];
		mainres%=mod;
	}
	cout<<mainres<<endl;
}

Compilation message

Main.cpp: In function 'void solvef(long long int)':
Main.cpp:16:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   16 |  for(int i=0;i<adj[u].size();i++){
      |              ~^~~~~~~~~~~~~~
Main.cpp:24:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   24 |  for(int i=0;i<adj[u].size();i++){
      |              ~^~~~~~~~~~~~~~
Main.cpp:28:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   28 |  for(int i=0;i<adj[u].size();i++){
      |              ~^~~~~~~~~~~~~~
Main.cpp: In function 'void solves(long long int)':
Main.cpp:49:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   49 |  for(int i=0;i<adj[u].size();i++){
      |              ~^~~~~~~~~~~~~~
Main.cpp:53:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   53 |   for(int i=0;i<adj[u].size();i++){
      |               ~^~~~~~~~~~~~~~
Main.cpp:62:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   62 |   for(int i=0;i<adj[u].size();i++){
      |               ~^~~~~~~~~~~~~~
Main.cpp:66:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   66 |   for(int i=0;i<adj[u].size();i++){
      |               ~^~~~~~~~~~~~~~
Main.cpp:85:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   85 |   for(int i=0;i<adj[u].size();i++){
      |               ~^~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 300 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 300 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 296 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 1 ms 300 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 1 ms 212 KB Output is correct
21 Correct 0 ms 212 KB Output is correct
22 Correct 0 ms 212 KB Output is correct
23 Correct 0 ms 212 KB Output is correct
24 Correct 1 ms 212 KB Output is correct
25 Correct 0 ms 212 KB Output is correct
26 Correct 0 ms 212 KB Output is correct
27 Correct 0 ms 212 KB Output is correct
28 Correct 1 ms 212 KB Output is correct
29 Correct 0 ms 212 KB Output is correct
30 Correct 1 ms 212 KB Output is correct
31 Correct 1 ms 300 KB Output is correct
32 Correct 1 ms 212 KB Output is correct
33 Correct 1 ms 212 KB Output is correct
34 Correct 0 ms 212 KB Output is correct
35 Correct 0 ms 212 KB Output is correct
36 Correct 0 ms 212 KB Output is correct
37 Correct 0 ms 212 KB Output is correct
38 Correct 1 ms 212 KB Output is correct
39 Correct 0 ms 212 KB Output is correct
40 Correct 0 ms 212 KB Output is correct
41 Correct 1 ms 212 KB Output is correct
42 Correct 1 ms 212 KB Output is correct
43 Correct 0 ms 212 KB Output is correct
44 Correct 0 ms 212 KB Output is correct
45 Correct 0 ms 212 KB Output is correct
46 Correct 1 ms 300 KB Output is correct
47 Correct 0 ms 212 KB Output is correct
48 Correct 1 ms 212 KB Output is correct
49 Correct 0 ms 212 KB Output is correct
50 Correct 1 ms 212 KB Output is correct
51 Correct 0 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 8 ms 1544 KB Output is correct
3 Correct 93 ms 12588 KB Output is correct
4 Correct 92 ms 12684 KB Output is correct
5 Correct 104 ms 12556 KB Output is correct
6 Correct 92 ms 12596 KB Output is correct
7 Correct 110 ms 22352 KB Output is correct
8 Correct 110 ms 18892 KB Output is correct
9 Correct 117 ms 19240 KB Output is correct
10 Correct 80 ms 14116 KB Output is correct
11 Correct 87 ms 14080 KB Output is correct
12 Correct 92 ms 12304 KB Output is correct
13 Correct 97 ms 12236 KB Output is correct
14 Correct 86 ms 17356 KB Output is correct
15 Correct 119 ms 24092 KB Output is correct
16 Correct 112 ms 25164 KB Output is correct
17 Correct 96 ms 12132 KB Output is correct
18 Correct 97 ms 12132 KB Output is correct
19 Correct 100 ms 12192 KB Output is correct
20 Correct 68 ms 14664 KB Output is correct
21 Correct 72 ms 13340 KB Output is correct
22 Correct 75 ms 13132 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 300 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 300 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 296 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 1 ms 300 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 1 ms 212 KB Output is correct
21 Correct 0 ms 212 KB Output is correct
22 Correct 0 ms 212 KB Output is correct
23 Correct 0 ms 212 KB Output is correct
24 Correct 1 ms 212 KB Output is correct
25 Correct 0 ms 212 KB Output is correct
26 Correct 0 ms 212 KB Output is correct
27 Correct 0 ms 212 KB Output is correct
28 Correct 1 ms 212 KB Output is correct
29 Correct 0 ms 212 KB Output is correct
30 Correct 1 ms 212 KB Output is correct
31 Correct 1 ms 300 KB Output is correct
32 Correct 1 ms 212 KB Output is correct
33 Correct 1 ms 212 KB Output is correct
34 Correct 0 ms 212 KB Output is correct
35 Correct 0 ms 212 KB Output is correct
36 Correct 0 ms 212 KB Output is correct
37 Correct 0 ms 212 KB Output is correct
38 Correct 1 ms 212 KB Output is correct
39 Correct 0 ms 212 KB Output is correct
40 Correct 0 ms 212 KB Output is correct
41 Correct 1 ms 212 KB Output is correct
42 Correct 1 ms 212 KB Output is correct
43 Correct 0 ms 212 KB Output is correct
44 Correct 0 ms 212 KB Output is correct
45 Correct 0 ms 212 KB Output is correct
46 Correct 1 ms 300 KB Output is correct
47 Correct 0 ms 212 KB Output is correct
48 Correct 1 ms 212 KB Output is correct
49 Correct 0 ms 212 KB Output is correct
50 Correct 1 ms 212 KB Output is correct
51 Correct 0 ms 212 KB Output is correct
52 Correct 4 ms 980 KB Output is correct
53 Correct 4 ms 852 KB Output is correct
54 Correct 4 ms 980 KB Output is correct
55 Correct 3 ms 980 KB Output is correct
56 Correct 6 ms 1492 KB Output is correct
57 Correct 3 ms 952 KB Output is correct
58 Correct 4 ms 980 KB Output is correct
59 Correct 4 ms 1208 KB Output is correct
60 Correct 3 ms 980 KB Output is correct
61 Correct 5 ms 852 KB Output is correct
62 Correct 5 ms 980 KB Output is correct
63 Correct 3 ms 952 KB Output is correct
64 Correct 3 ms 980 KB Output is correct
65 Correct 5 ms 956 KB Output is correct
66 Correct 4 ms 972 KB Output is correct
67 Correct 5 ms 980 KB Output is correct
68 Correct 5 ms 1368 KB Output is correct
69 Correct 5 ms 852 KB Output is correct
70 Correct 3 ms 980 KB Output is correct
71 Correct 5 ms 948 KB Output is correct
72 Correct 5 ms 980 KB Output is correct
73 Correct 3 ms 980 KB Output is correct
74 Correct 5 ms 852 KB Output is correct
75 Correct 5 ms 1236 KB Output is correct
76 Correct 5 ms 952 KB Output is correct
77 Correct 6 ms 1368 KB Output is correct
78 Correct 4 ms 980 KB Output is correct
79 Correct 4 ms 1340 KB Output is correct
80 Correct 5 ms 980 KB Output is correct
81 Correct 4 ms 980 KB Output is correct
82 Correct 4 ms 948 KB Output is correct
83 Correct 4 ms 980 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 300 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 300 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 296 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 1 ms 300 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 1 ms 212 KB Output is correct
21 Correct 0 ms 212 KB Output is correct
22 Correct 0 ms 212 KB Output is correct
23 Correct 0 ms 212 KB Output is correct
24 Correct 1 ms 212 KB Output is correct
25 Correct 0 ms 212 KB Output is correct
26 Correct 0 ms 212 KB Output is correct
27 Correct 0 ms 212 KB Output is correct
28 Correct 1 ms 212 KB Output is correct
29 Correct 0 ms 212 KB Output is correct
30 Correct 1 ms 212 KB Output is correct
31 Correct 1 ms 300 KB Output is correct
32 Correct 1 ms 212 KB Output is correct
33 Correct 1 ms 212 KB Output is correct
34 Correct 0 ms 212 KB Output is correct
35 Correct 0 ms 212 KB Output is correct
36 Correct 0 ms 212 KB Output is correct
37 Correct 0 ms 212 KB Output is correct
38 Correct 1 ms 212 KB Output is correct
39 Correct 0 ms 212 KB Output is correct
40 Correct 0 ms 212 KB Output is correct
41 Correct 1 ms 212 KB Output is correct
42 Correct 1 ms 212 KB Output is correct
43 Correct 0 ms 212 KB Output is correct
44 Correct 0 ms 212 KB Output is correct
45 Correct 0 ms 212 KB Output is correct
46 Correct 1 ms 300 KB Output is correct
47 Correct 0 ms 212 KB Output is correct
48 Correct 1 ms 212 KB Output is correct
49 Correct 0 ms 212 KB Output is correct
50 Correct 1 ms 212 KB Output is correct
51 Correct 0 ms 212 KB Output is correct
52 Correct 0 ms 212 KB Output is correct
53 Correct 8 ms 1544 KB Output is correct
54 Correct 93 ms 12588 KB Output is correct
55 Correct 92 ms 12684 KB Output is correct
56 Correct 104 ms 12556 KB Output is correct
57 Correct 92 ms 12596 KB Output is correct
58 Correct 110 ms 22352 KB Output is correct
59 Correct 110 ms 18892 KB Output is correct
60 Correct 117 ms 19240 KB Output is correct
61 Correct 80 ms 14116 KB Output is correct
62 Correct 87 ms 14080 KB Output is correct
63 Correct 92 ms 12304 KB Output is correct
64 Correct 97 ms 12236 KB Output is correct
65 Correct 86 ms 17356 KB Output is correct
66 Correct 119 ms 24092 KB Output is correct
67 Correct 112 ms 25164 KB Output is correct
68 Correct 96 ms 12132 KB Output is correct
69 Correct 97 ms 12132 KB Output is correct
70 Correct 100 ms 12192 KB Output is correct
71 Correct 68 ms 14664 KB Output is correct
72 Correct 72 ms 13340 KB Output is correct
73 Correct 75 ms 13132 KB Output is correct
74 Correct 4 ms 980 KB Output is correct
75 Correct 4 ms 852 KB Output is correct
76 Correct 4 ms 980 KB Output is correct
77 Correct 3 ms 980 KB Output is correct
78 Correct 6 ms 1492 KB Output is correct
79 Correct 3 ms 952 KB Output is correct
80 Correct 4 ms 980 KB Output is correct
81 Correct 4 ms 1208 KB Output is correct
82 Correct 3 ms 980 KB Output is correct
83 Correct 5 ms 852 KB Output is correct
84 Correct 5 ms 980 KB Output is correct
85 Correct 3 ms 952 KB Output is correct
86 Correct 3 ms 980 KB Output is correct
87 Correct 5 ms 956 KB Output is correct
88 Correct 4 ms 972 KB Output is correct
89 Correct 5 ms 980 KB Output is correct
90 Correct 5 ms 1368 KB Output is correct
91 Correct 5 ms 852 KB Output is correct
92 Correct 3 ms 980 KB Output is correct
93 Correct 5 ms 948 KB Output is correct
94 Correct 5 ms 980 KB Output is correct
95 Correct 3 ms 980 KB Output is correct
96 Correct 5 ms 852 KB Output is correct
97 Correct 5 ms 1236 KB Output is correct
98 Correct 5 ms 952 KB Output is correct
99 Correct 6 ms 1368 KB Output is correct
100 Correct 4 ms 980 KB Output is correct
101 Correct 4 ms 1340 KB Output is correct
102 Correct 5 ms 980 KB Output is correct
103 Correct 4 ms 980 KB Output is correct
104 Correct 4 ms 948 KB Output is correct
105 Correct 4 ms 980 KB Output is correct
106 Correct 106 ms 13932 KB Output is correct
107 Correct 101 ms 13972 KB Output is correct
108 Correct 102 ms 14064 KB Output is correct
109 Correct 107 ms 14144 KB Output is correct
110 Correct 77 ms 14892 KB Output is correct
111 Correct 92 ms 16204 KB Output is correct
112 Correct 133 ms 21924 KB Output is correct
113 Correct 112 ms 13644 KB Output is correct
114 Correct 120 ms 13936 KB Output is correct
115 Correct 76 ms 15436 KB Output is correct
116 Correct 118 ms 22756 KB Output is correct
117 Correct 125 ms 21656 KB Output is correct
118 Correct 79 ms 15784 KB Output is correct
119 Correct 108 ms 13488 KB Output is correct
120 Correct 111 ms 13936 KB Output is correct
121 Correct 114 ms 14348 KB Output is correct
122 Correct 119 ms 14664 KB Output is correct
123 Correct 87 ms 14368 KB Output is correct
124 Correct 79 ms 14532 KB Output is correct
125 Correct 71 ms 15552 KB Output is correct
126 Correct 99 ms 13888 KB Output is correct
127 Correct 108 ms 14024 KB Output is correct
128 Correct 114 ms 14272 KB Output is correct
129 Correct 101 ms 14136 KB Output is correct
130 Correct 77 ms 14604 KB Output is correct
131 Correct 105 ms 16376 KB Output is correct
132 Correct 135 ms 21672 KB Output is correct
133 Correct 115 ms 13644 KB Output is correct
134 Correct 112 ms 13928 KB Output is correct
135 Correct 76 ms 15456 KB Output is correct
136 Correct 118 ms 20556 KB Output is correct
137 Correct 123 ms 21196 KB Output is correct
138 Correct 75 ms 15684 KB Output is correct
139 Correct 107 ms 13564 KB Output is correct
140 Correct 115 ms 13900 KB Output is correct
141 Correct 120 ms 14348 KB Output is correct
142 Correct 124 ms 14620 KB Output is correct
143 Correct 81 ms 14152 KB Output is correct
144 Correct 74 ms 14512 KB Output is correct
145 Correct 73 ms 15508 KB Output is correct
146 Correct 113 ms 13984 KB Output is correct
147 Correct 110 ms 13404 KB Output is correct
148 Correct 104 ms 14080 KB Output is correct
149 Correct 113 ms 15036 KB Output is correct
150 Correct 121 ms 23604 KB Output is correct
151 Correct 111 ms 13816 KB Output is correct
152 Correct 139 ms 14924 KB Output is correct
153 Correct 138 ms 14896 KB Output is correct
154 Correct 108 ms 15328 KB Output is correct
155 Correct 82 ms 14864 KB Output is correct
156 Correct 119 ms 21664 KB Output is correct
157 Correct 125 ms 20544 KB Output is correct
158 Correct 114 ms 19404 KB Output is correct
159 Correct 116 ms 13472 KB Output is correct
160 Correct 128 ms 22660 KB Output is correct
161 Correct 112 ms 13364 KB Output is correct
162 Correct 80 ms 14260 KB Output is correct