You are on page 1of 6

1/ Ghép nhóm(đề 1)

#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 DFS(int s){


    visited[s] = true;
    Gr[res].push_back(s);
    for (int x : adj[s]){
        if(!visited[x]) DFS(x);
    }
}

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;
}

2/ Đếm Tàu(đề 1);


#include <bits/stdc++.h>
using namespace std;
const int MAX = 250+2;
int a[MAX][MAX];
int n = 0, m = 0;
bool visited[MAX][MAX];
int dx[] = {0,1,1,0,0,-1,1,-1,-1};
int dy[] = {0,1,0,1,-1,0,-1,1,-1};
string s;

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;

    for (int i = 1; i <= m ; i ++)


        for (int j = 1; j <= n; j ++)
            if (A[i][j] == 1) F[i][j] = F[i-1][j-1] + 1;
            else F[i][j] = max(F[i - 1][j], F[i][j-1]);
    cout << F[m][n] << endl;
    int i = m, j = n;
    while(i !=0 && j != 0)
        if(A[i][j] == 1){
            T.push_back({i, j});
            i--, j--;
        }
        else if (F[i][j] == F[i - 1][j]) i--;
            else j--;

    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 A[1000][1000],B[1000][1000],vt,n,m,maxx = -1e8;


vector<pair<int,int>> T;

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;
};

int dx[] = {0,1,-1,0};


int dy[] = {-1,0,0,1};
int n,m,d[1000][1000],kq;
char a[1000][1000];
int check[1000][1000];

void bfs(int u,int v){


    queue<data> q;
    q.push({u,v});
    check[u][v] = 0;
    while(!q.empty()){
        data now = q.front();
        q.pop();
        for (int i = 0; i <= 3; i ++){
            int u = now.x + dx[i];
            int v = now.y + dy[i];
            if (check[u][v]==0 && (a[u][v] =='.' || a[u][v] == 'R')){
                check[u][v] = check[now.x][now.y]+1;
                q.push({u,v});
                if (a[u][v] == 'R') {
                    kq = check[u][v];
                    return;
                }
            }
        }
    }
}
int main(){
    freopen("Bai3.inp","r",stdin);
    freopen("Bai3.out","w",stdout);
    cin >> n >> m;
    for (int i = 1; i <= n; i ++)
        for (int j = 1; j <= m ; j++){
            cin >> a[i][j];
            check[i][j] = 0;
        }
    for (int i = 1; i <= n; i ++)
        for (int j = 1; i <= m; i ++)
            if (a[i][j] =='G') {
                bfs(i,j);
                cout <<kq;
        }
    return 0;
}

You might also like