Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define task "CAU4"
- #include <iostream>
- #include <cstdio>
- #include <vector>
- #include <queue>
- using namespace std;
- constexpr int N = 1e2 + 5;
- int m, n, l, cnt;
- int a[N][N], reid[N][N]; // Đánh số lại các đỉnh
- vector<int> adj[N * N]; // Danh sách kề
- bool vis[N * N]; // Đã duyệt qua hay chưa
- void Read()
- {
- cin >> m >> n;
- for (int i = 1; i <= m; ++i)
- for (int j = 1; j <= n; ++j)
- {
- cin >> a[i][j];
- reid[i][j] = ++l;
- }
- }
- void DFS(int v)
- {
- vis[v] = 1;
- ++cnt;
- for (auto i : adj[v])
- if (!vis[i])
- DFS(i);
- }
- void BFS(int v)
- {
- queue<int> q({v});
- while (!q.empty())
- {
- int c = q.front();
- q.pop();
- vis[c] = 1;
- ++cnt;
- for (auto i : adj[v])
- if (!vis[i])
- q.emplace(i);
- }
- }
- void Solve()
- {
- for (int i = 1; i <= m; ++i)
- for (int j = 1; j <= n; ++j)
- for (int t = i - 1; t <= i + 1; ++t)
- for (int h = j - 1; h <= j + 1; ++h)
- if (t > 0 && t <= m && h > 0 && h <= n && (t != i || j != h) && a[i][j] == a[t][h])
- adj[reid[i][j]].emplace_back(reid[t][h]);
- int res(0), maxn(0);
- for (int i = 1; i <= m; ++i)
- for (int j = 1; j <= n; ++j)
- if (!vis[reid[i][j]])
- {
- cnt = 0; // Reset biến đếm
- // Chọn DFS hoặc BFS
- DFS(reid[i][j]);
- // BFS(reid[i][j]);
- if (cnt != 1)
- {
- ++res;
- maxn = max(maxn, cnt);
- }
- }
- cout << res << "\n"
- << maxn;
- }
- int32_t main()
- {
- ios::sync_with_stdio(0);
- cin.tie(0);
- cout.tie(0);
- if (fopen(task ".INP", "r"))
- {
- freopen(task ".INP", "r", stdin);
- freopen(task ".OUT", "w", stdout);
- }
- Read();
- Solve();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement