답안 #636246

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
636246 2022-08-28T15:22:03 Z amirhoseinfar1385 Subtree (INOI20_subtree) C++17
100 / 100
89 ms 25192 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(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	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++){
      |               ~^~~~~~~~~~~~~~
# 결과 실행 시간 메모리 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 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 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 0 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
21 Correct 0 ms 212 KB Output is correct
22 Correct 1 ms 212 KB Output is correct
23 Correct 0 ms 212 KB Output is correct
24 Correct 0 ms 212 KB Output is correct
25 Correct 0 ms 212 KB Output is correct
26 Correct 1 ms 212 KB Output is correct
27 Correct 0 ms 212 KB Output is correct
28 Correct 0 ms 212 KB Output is correct
29 Correct 0 ms 212 KB Output is correct
30 Correct 0 ms 212 KB Output is correct
31 Correct 0 ms 212 KB Output is correct
32 Correct 0 ms 212 KB Output is correct
33 Correct 0 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 0 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 0 ms 212 KB Output is correct
42 Correct 0 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 0 ms 212 KB Output is correct
47 Correct 0 ms 212 KB Output is correct
48 Correct 0 ms 212 KB Output is correct
49 Correct 0 ms 212 KB Output is correct
50 Correct 0 ms 212 KB Output is correct
51 Correct 0 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 5 ms 1492 KB Output is correct
3 Correct 60 ms 12616 KB Output is correct
4 Correct 62 ms 12668 KB Output is correct
5 Correct 62 ms 12700 KB Output is correct
6 Correct 59 ms 12740 KB Output is correct
7 Correct 81 ms 22352 KB Output is correct
8 Correct 71 ms 18892 KB Output is correct
9 Correct 70 ms 19268 KB Output is correct
10 Correct 42 ms 13988 KB Output is correct
11 Correct 45 ms 14148 KB Output is correct
12 Correct 69 ms 12348 KB Output is correct
13 Correct 61 ms 12116 KB Output is correct
14 Correct 48 ms 17304 KB Output is correct
15 Correct 73 ms 24012 KB Output is correct
16 Correct 75 ms 25192 KB Output is correct
17 Correct 69 ms 12188 KB Output is correct
18 Correct 62 ms 12128 KB Output is correct
19 Correct 75 ms 12176 KB Output is correct
20 Correct 36 ms 14588 KB Output is correct
21 Correct 38 ms 13380 KB Output is correct
22 Correct 39 ms 13120 KB Output is correct
# 결과 실행 시간 메모리 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 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 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 0 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
21 Correct 0 ms 212 KB Output is correct
22 Correct 1 ms 212 KB Output is correct
23 Correct 0 ms 212 KB Output is correct
24 Correct 0 ms 212 KB Output is correct
25 Correct 0 ms 212 KB Output is correct
26 Correct 1 ms 212 KB Output is correct
27 Correct 0 ms 212 KB Output is correct
28 Correct 0 ms 212 KB Output is correct
29 Correct 0 ms 212 KB Output is correct
30 Correct 0 ms 212 KB Output is correct
31 Correct 0 ms 212 KB Output is correct
32 Correct 0 ms 212 KB Output is correct
33 Correct 0 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 0 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 0 ms 212 KB Output is correct
42 Correct 0 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 0 ms 212 KB Output is correct
47 Correct 0 ms 212 KB Output is correct
48 Correct 0 ms 212 KB Output is correct
49 Correct 0 ms 212 KB Output is correct
50 Correct 0 ms 212 KB Output is correct
51 Correct 0 ms 212 KB Output is correct
52 Correct 3 ms 852 KB Output is correct
53 Correct 3 ms 852 KB Output is correct
54 Correct 4 ms 852 KB Output is correct
55 Correct 2 ms 916 KB Output is correct
56 Correct 3 ms 1364 KB Output is correct
57 Correct 2 ms 980 KB Output is correct
58 Correct 2 ms 980 KB Output is correct
59 Correct 2 ms 1108 KB Output is correct
60 Correct 2 ms 980 KB Output is correct
61 Correct 3 ms 852 KB Output is correct
62 Correct 3 ms 980 KB Output is correct
63 Correct 2 ms 852 KB Output is correct
64 Correct 2 ms 980 KB Output is correct
65 Correct 2 ms 852 KB Output is correct
66 Correct 3 ms 852 KB Output is correct
67 Correct 2 ms 988 KB Output is correct
68 Correct 3 ms 1236 KB Output is correct
69 Correct 3 ms 852 KB Output is correct
70 Correct 2 ms 980 KB Output is correct
71 Correct 2 ms 852 KB Output is correct
72 Correct 3 ms 980 KB Output is correct
73 Correct 2 ms 980 KB Output is correct
74 Correct 3 ms 852 KB Output is correct
75 Correct 2 ms 1108 KB Output is correct
76 Correct 3 ms 852 KB Output is correct
77 Correct 3 ms 1364 KB Output is correct
78 Correct 3 ms 980 KB Output is correct
79 Correct 3 ms 1236 KB Output is correct
80 Correct 3 ms 980 KB Output is correct
81 Correct 2 ms 980 KB Output is correct
82 Correct 3 ms 904 KB Output is correct
83 Correct 2 ms 980 KB Output is correct
# 결과 실행 시간 메모리 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 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 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 0 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
21 Correct 0 ms 212 KB Output is correct
22 Correct 1 ms 212 KB Output is correct
23 Correct 0 ms 212 KB Output is correct
24 Correct 0 ms 212 KB Output is correct
25 Correct 0 ms 212 KB Output is correct
26 Correct 1 ms 212 KB Output is correct
27 Correct 0 ms 212 KB Output is correct
28 Correct 0 ms 212 KB Output is correct
29 Correct 0 ms 212 KB Output is correct
30 Correct 0 ms 212 KB Output is correct
31 Correct 0 ms 212 KB Output is correct
32 Correct 0 ms 212 KB Output is correct
33 Correct 0 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 0 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 0 ms 212 KB Output is correct
42 Correct 0 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 0 ms 212 KB Output is correct
47 Correct 0 ms 212 KB Output is correct
48 Correct 0 ms 212 KB Output is correct
49 Correct 0 ms 212 KB Output is correct
50 Correct 0 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 5 ms 1492 KB Output is correct
54 Correct 60 ms 12616 KB Output is correct
55 Correct 62 ms 12668 KB Output is correct
56 Correct 62 ms 12700 KB Output is correct
57 Correct 59 ms 12740 KB Output is correct
58 Correct 81 ms 22352 KB Output is correct
59 Correct 71 ms 18892 KB Output is correct
60 Correct 70 ms 19268 KB Output is correct
61 Correct 42 ms 13988 KB Output is correct
62 Correct 45 ms 14148 KB Output is correct
63 Correct 69 ms 12348 KB Output is correct
64 Correct 61 ms 12116 KB Output is correct
65 Correct 48 ms 17304 KB Output is correct
66 Correct 73 ms 24012 KB Output is correct
67 Correct 75 ms 25192 KB Output is correct
68 Correct 69 ms 12188 KB Output is correct
69 Correct 62 ms 12128 KB Output is correct
70 Correct 75 ms 12176 KB Output is correct
71 Correct 36 ms 14588 KB Output is correct
72 Correct 38 ms 13380 KB Output is correct
73 Correct 39 ms 13120 KB Output is correct
74 Correct 3 ms 852 KB Output is correct
75 Correct 3 ms 852 KB Output is correct
76 Correct 4 ms 852 KB Output is correct
77 Correct 2 ms 916 KB Output is correct
78 Correct 3 ms 1364 KB Output is correct
79 Correct 2 ms 980 KB Output is correct
80 Correct 2 ms 980 KB Output is correct
81 Correct 2 ms 1108 KB Output is correct
82 Correct 2 ms 980 KB Output is correct
83 Correct 3 ms 852 KB Output is correct
84 Correct 3 ms 980 KB Output is correct
85 Correct 2 ms 852 KB Output is correct
86 Correct 2 ms 980 KB Output is correct
87 Correct 2 ms 852 KB Output is correct
88 Correct 3 ms 852 KB Output is correct
89 Correct 2 ms 988 KB Output is correct
90 Correct 3 ms 1236 KB Output is correct
91 Correct 3 ms 852 KB Output is correct
92 Correct 2 ms 980 KB Output is correct
93 Correct 2 ms 852 KB Output is correct
94 Correct 3 ms 980 KB Output is correct
95 Correct 2 ms 980 KB Output is correct
96 Correct 3 ms 852 KB Output is correct
97 Correct 2 ms 1108 KB Output is correct
98 Correct 3 ms 852 KB Output is correct
99 Correct 3 ms 1364 KB Output is correct
100 Correct 3 ms 980 KB Output is correct
101 Correct 3 ms 1236 KB Output is correct
102 Correct 3 ms 980 KB Output is correct
103 Correct 2 ms 980 KB Output is correct
104 Correct 3 ms 904 KB Output is correct
105 Correct 2 ms 980 KB Output is correct
106 Correct 60 ms 12728 KB Output is correct
107 Correct 59 ms 12748 KB Output is correct
108 Correct 62 ms 12820 KB Output is correct
109 Correct 67 ms 12808 KB Output is correct
110 Correct 44 ms 13736 KB Output is correct
111 Correct 70 ms 15032 KB Output is correct
112 Correct 86 ms 20684 KB Output is correct
113 Correct 65 ms 12404 KB Output is correct
114 Correct 66 ms 12752 KB Output is correct
115 Correct 38 ms 14332 KB Output is correct
116 Correct 81 ms 21576 KB Output is correct
117 Correct 78 ms 20440 KB Output is correct
118 Correct 38 ms 14652 KB Output is correct
119 Correct 69 ms 12304 KB Output is correct
120 Correct 69 ms 12752 KB Output is correct
121 Correct 73 ms 13036 KB Output is correct
122 Correct 76 ms 13272 KB Output is correct
123 Correct 45 ms 13152 KB Output is correct
124 Correct 38 ms 13404 KB Output is correct
125 Correct 37 ms 14436 KB Output is correct
126 Correct 59 ms 12736 KB Output is correct
127 Correct 62 ms 12824 KB Output is correct
128 Correct 63 ms 12776 KB Output is correct
129 Correct 65 ms 12888 KB Output is correct
130 Correct 42 ms 13452 KB Output is correct
131 Correct 52 ms 14968 KB Output is correct
132 Correct 87 ms 20428 KB Output is correct
133 Correct 76 ms 12404 KB Output is correct
134 Correct 64 ms 12708 KB Output is correct
135 Correct 38 ms 14280 KB Output is correct
136 Correct 81 ms 19444 KB Output is correct
137 Correct 85 ms 20164 KB Output is correct
138 Correct 39 ms 14676 KB Output is correct
139 Correct 85 ms 12264 KB Output is correct
140 Correct 69 ms 12760 KB Output is correct
141 Correct 82 ms 12968 KB Output is correct
142 Correct 73 ms 13240 KB Output is correct
143 Correct 45 ms 13084 KB Output is correct
144 Correct 39 ms 13368 KB Output is correct
145 Correct 39 ms 14404 KB Output is correct
146 Correct 70 ms 12588 KB Output is correct
147 Correct 89 ms 12280 KB Output is correct
148 Correct 63 ms 12812 KB Output is correct
149 Correct 64 ms 13900 KB Output is correct
150 Correct 76 ms 22436 KB Output is correct
151 Correct 69 ms 12612 KB Output is correct
152 Correct 76 ms 13260 KB Output is correct
153 Correct 81 ms 13424 KB Output is correct
154 Correct 74 ms 14212 KB Output is correct
155 Correct 42 ms 13684 KB Output is correct
156 Correct 81 ms 20616 KB Output is correct
157 Correct 74 ms 19444 KB Output is correct
158 Correct 70 ms 18296 KB Output is correct
159 Correct 61 ms 12176 KB Output is correct
160 Correct 86 ms 21492 KB Output is correct
161 Correct 72 ms 12272 KB Output is correct
162 Correct 44 ms 13140 KB Output is correct