pat 1013(DFS 并查集)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#include <iostream>
#include <vector>
#include <algorithm>
#define MAX 1010
using namespace std;
/*int father[MAX];
void init(int n)
{
for(int i = 1 ; i <= n; ++i)
{
father[i] = i;
}
}
int findFather(int x)
{
int a = x;
while(x != father[x])
x = father[x];
while(a != father[a])
{
int z = a;
a = father[a];
father[z] = x;
}
return x;
}
void Union(int a , int b)
{
int faA = findFather(a);
int faB = findFather(b);
if(faA != faB)
{
father[faA] = faB;
}
}
bool isRoot[MAX] = {false};
int calBlock(int n)
{
int Block = 0;
int i;
for(i = 1 ; i <= n ; ++i)
{
if(checked[findFather(i)] == false)
isRoot[findFather(i)] = true;
}
for(i = 1; i <= n ; ++i )
{
Block += isRoot[i];
}
return Block;
}
*/
vector<int> C[MAX];
bool vis[MAX] = {false};
int cnt = 0;
void DFS(int root , int depth)
{
vis[root] = true;
for(int i = 0 ; i < C[root].size() ; ++i)
{
int v = C[root][i];
if(vis[v] == false)
{
vis[v] = true;
DFS(v , depth+1);
}
}
}
void DFSTravel(int n)
{
for(int i = 1 ; i <= n ; ++i)
{
if(vis[i] == false)
{
DFS(i , 0);
++cnt;
}
}
}
int main()
{
int n , m , k;
cin>>n>>m>>k;
// init(n) ;
bool isE = false;
if(n <= 1)
isE = true;
int i;
for(i = 1 ; i <= m ; ++i)
{
int a , b;
cin>>a>>b;
C[a].push_back(b);
C[b].push_back(a);
//Union(a , b);
}
for(i = 1 ; i <= k ; ++i)
{
fill(vis , vis+MAX , false);//此处要注意!!!
int index;
cin>>index;
vis[index] = true;
DFSTravel(n);
if(isE == false)
cout<<cnt-1<<endl;
cnt = 0;
}
if(isE)
cout<<"0"<<endl;
/* for(i = 1 ; i <= k ; ++i)
{
fill(checked , checked + MAX , false);
int index;
cin>>index;
int temp = findFather(index);
if(temp != index)
{
}
checked[index] = true;
int Block = calBlock(n);
cout<<Block<<endl;
}*/
return 0;
}

并查集方法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#include <iostream>
#include <vector>
#include <algorithm>
#define MAX 1010
using namespace std;
vector<int> C[MAX];
bool vis[MAX] = {false};
int father[MAX];
void init(int n)
{
for(int i = 1 ; i <= n; ++i)
{
father[i] = i;
}
}
int findFather(int x)
{
int a = x;
while(x != father[x])
x = father[x];
while(a != father[a])
{
int z = a;
a = father[a];
father[z] = x;
}
return x;
}
void Union(int a , int b)
{
int faA = findFather(a);
int faB = findFather(b);
if(faA != faB)
{
father[faA] = faB;
}
}
/*bool isRoot[MAX] = {false};
int calBlock(int n)
{
int Block = 0;
int i;
for(i = 1 ; i <= n ; ++i)
{
isRoot[findFather(i)] = true;
}
for(i = 1; i <= n ; ++i )
{
Block += isRoot[i];
}
return Block;
}
*/
/*
int cnt = 0;
void DFS(int root , int depth)
{
vis[root] = true;
for(int i = 0 ; i < C[root].size() ; ++i)
{
int v = C[root][i];
if(vis[v] == false)
{
vis[v] = true;
DFS(v , depth+1);
}
}
}
void DFSTravel(int n)
{
for(int i = 1 ; i <= n ; ++i)
{
if(vis[i] == false)
{
DFS(i , 0);
++cnt;
}
}
}*/
int main()
{
int n , m , k;
cin>>n>>m>>k;
// init(n) ;
bool isE = false;
if(n <= 1)
isE = true;
int i;
for(i = 1 ; i <= m ; ++i)
{
int a , b;
cin>>a>>b;
C[a].push_back(b);
C[b].push_back(a);
// Union(a , b);
}
int currentPoint;//当前要移除的顶点号
for(int query = 0 ; query < k ; ++query)
{
fill(vis , vis+MAX , false);
cin>>currentPoint;
init(n);
for(i = 1 ; i <= n ; ++i)
{
for(int j = 0 ; j < C[i].size() ; ++j)
{
int u = i , v = C[i][j];//现场合并
if(u == currentPoint || v == currentPoint)
continue;
Union(u , v);
}
}
//计算连通分量个数
int block = 0;
for(i = 1 ; i <= n ; ++i)
{
if(i == currentPoint)
continue;
int fa_i = findFather(i);
if(vis[fa_i] == false)
{
block++;
vis[fa_i] = true;
}
}
cout<<block-1<<endl;
}
/*
for(i = 1 ; i <= k ; ++i)
{
fill(vis , vis+MAX , false);//每次记得刷新!!
int index;
cin>>index;
vis[index] = true;
DFSTravel(n);
if(isE == false)
cout<<cnt-1<<endl;
cnt = 0;
}
if(isE)
cout<<"0"<<endl;
*/
/* for(i = 1 ; i <= k ; ++i)
{
fill(checked , checked + MAX , false);
int index;
cin>>index;
int temp = findFather(index);
if(temp != index)
{
}
checked[index] = true;
int Block = calBlock(n);
cout<<Block<<endl;
}*/
return 0;
}

热评文章