Professional Documents
Culture Documents
#include<bits/stdc++.h>
using namespace std;
int m, n, u, v, res;
vector<int> adj[100001];
bool visited[100001];
vector<int> Gr[10001];
void connect(){
memset(visited, false, sizeof(visited));
res = 0;
for (int i = 1; i <= n; i ++){
if(!visited[i]){
res++;
DFS(i);
}
}
}
int main(){
freopen("GHEPNHOM.inp","r",stdin);
freopen("GHEPNHOM.out","w",stdout);
cin >> n >> m;
for (int i = 1; i <= m; i ++){
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
connect();
cout << res << endl;
for (int i = 1; i <= res; i ++){
sort(Gr[i].begin(),Gr[i].end());
for (int x : Gr[i]) cout << x << " ";
cout << endl;
}
return 0;
}
struct point {
int x,y;
};
bool check(point a) {
return (a.x > 0 && a.x <= n && a.y > 0 && a.y <= m);
}
void BFS(point go) {
queue<point> q;
q.push(go);
visited[go.x][go.y] = true;
while (!q.empty()) {
point now = q.front();
q.pop();
for (int i = 1; i <= 8; i++) {
point next = {now.x + dx[i],now.y + dy[i]};
if (check(next) && !visited[next.x][next.y] && a[next.x][next.y])
{
visited[next.x][next.y] = true;
q.push(next);
}
}
}
}
int main() {
freopen("DEMTAU.INP","r",stdin);
freopen("DEMTAU.OUT","w",stdout);
memset(a,0,sizeof(a));
memset(visited,0,sizeof(visited));
while (cin >> s) {
n++;
for (int i = 0; i < (int) s.size(); i++)
if (s[i] == 'x') a[n][i+1] = 1;
}
m = s.size();
int res = 0;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
if (a[i][j] && !visited[i][j]) {
res++;
BFS({i,j});
}
cout << res;
return 0;
}
3/Bắc cầu(đề 2)
#include <bits/stdc++.h>
using namespace std;
vector<pair<int, int>> T;
int m, n;
int F[10001][10001], A[10001][10001];
int main(){
freopen("BACCAU.inp","r",stdin);
freopen("BACCAU.out","w",stdout);
cin >> m >> n;
int u, v;
while(cin >> u >> v){
A[u][v] = A[v][u] = 1;
}
for (int i = 0; i <= m; i ++)
F[i][0] = 0;
for (int j = 0; j <= n; j ++)
F[0][j] = 0;
reverse(T.begin(), T.end());
for (auto x : T){
cout << x.first << " " << x.second<< endl;
}
return 0;
}
4/ Hành trình đông tây(đề 2)
#include <bits/stdc++.h>
using namespace std;
int main(){
freopen("Hanhtrinh.inp","r",stdin);
freopen("Hanhtrinh.out","w",stdout);
cin >> n >> m;
for(int i = 1; i<= n;i++)
for(int j = 1;j <= m;j++)
cin >> A[i][j];
for(int i = 1;i<=n;i++)
B[i][1] = A[i][1];
for(int i = 2;i<=m;i++)
for(int j = 1;j <= n;j++)
B[j][i] = max(max(B[j+1][i-1],B[j][i-1]),B[j-1][i-1])+A[j][i];
for(int i = 1;i<=n;i++)
if (B[i][m] > maxx){
maxx = max(B[i][m],maxx);
vt = i;
}
int i = vt,j = m;
while (j != 0){
if (B[i][j] - A[i][j] == A[i-1][j-1]){
T.push_back({j,i});
i--;
j--;
}
if (B[i][j] - A[i][j] == B[i][j-1]){
T.push_back({j,i});
j--;
}
if (B[i][j] - A[i][j] == B[i+1][j-1] ){
T.push_back({j,i});
i++;
j--;
}
}
reverse(T.begin(), T.end());
cout << maxx<<endl;
for(auto x: T)
cout << x.second <<" "<< x.first <<endl;
return 0;
}
5/ (đề 1 )
bài có test là
56
....*.
.G*...
.**.*.
..***.
*..*R.
#include <bits/stdc++.h>
using namespace std;
struct data{
int x,y;
};