Advertisement
Dang_Quan_10_Tin

CAU4 HSGHN L9 2014-2015

Dec 24th, 2021
1,107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.02 KB | None | 0 0
  1. #define task "CAU4"
  2.  
  3. #include <iostream>
  4. #include <cstdio>
  5. #include <vector>
  6. #include <queue>
  7.  
  8. using namespace std;
  9.  
  10. constexpr int N = 1e2 + 5;
  11. int m, n, l, cnt;
  12. int a[N][N], reid[N][N]; // Đánh số lại các đỉnh
  13. vector<int> adj[N * N];  // Danh sách kề
  14. bool vis[N * N];         // Đã duyệt qua hay chưa
  15.  
  16. void Read()
  17. {
  18.     cin >> m >> n;
  19.  
  20.     for (int i = 1; i <= m; ++i)
  21.         for (int j = 1; j <= n; ++j)
  22.         {
  23.             cin >> a[i][j];
  24.             reid[i][j] = ++l;
  25.         }
  26. }
  27.  
  28. void DFS(int v)
  29. {
  30.     vis[v] = 1;
  31.     ++cnt;
  32.  
  33.     for (auto i : adj[v])
  34.         if (!vis[i])
  35.             DFS(i);
  36. }
  37.  
  38. void BFS(int v)
  39. {
  40.     queue<int> q({v});
  41.  
  42.     while (!q.empty())
  43.     {
  44.         int c = q.front();
  45.         q.pop();
  46.  
  47.         vis[c] = 1;
  48.         ++cnt;
  49.  
  50.         for (auto i : adj[v])
  51.             if (!vis[i])
  52.                 q.emplace(i);
  53.     }
  54. }
  55.  
  56. void Solve()
  57. {
  58.     for (int i = 1; i <= m; ++i)
  59.         for (int j = 1; j <= n; ++j)
  60.             for (int t = i - 1; t <= i + 1; ++t)
  61.                 for (int h = j - 1; h <= j + 1; ++h)
  62.                     if (t > 0 && t <= m && h > 0 && h <= n && (t != i || j != h) && a[i][j] == a[t][h])
  63.                         adj[reid[i][j]].emplace_back(reid[t][h]);
  64.  
  65.     int res(0), maxn(0);
  66.  
  67.     for (int i = 1; i <= m; ++i)
  68.         for (int j = 1; j <= n; ++j)
  69.             if (!vis[reid[i][j]])
  70.             {
  71.                 cnt = 0; // Reset biến đếm
  72.  
  73.                 // Chọn DFS hoặc BFS
  74.                 DFS(reid[i][j]);
  75.                 // BFS(reid[i][j]);
  76.  
  77.                 if (cnt != 1)
  78.                 {
  79.                     ++res;
  80.                     maxn = max(maxn, cnt);
  81.                 }
  82.             }
  83.  
  84.     cout << res << "\n"
  85.          << maxn;
  86. }
  87.  
  88. int32_t main()
  89. {
  90.     ios::sync_with_stdio(0);
  91.     cin.tie(0);
  92.     cout.tie(0);
  93.     if (fopen(task ".INP", "r"))
  94.     {
  95.         freopen(task ".INP", "r", stdin);
  96.         freopen(task ".OUT", "w", stdout);
  97.     }
  98.  
  99.     Read();
  100.     Solve();
  101. }
  102.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement