fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. struct anh
  5. {
  6. int a[100][100],n,m;
  7. multiset<int> Res;
  8. void nhap()
  9. {
  10. cin>>n>>m;
  11. for(int i=1;i<=n;i++)
  12. for(int j=1;j<=m;j++) cin>>a[i][j];
  13. for(int i=0;i<=n+1;i++) a[i][0]=a[i][m+1]=2;
  14. for(int j=0;j<=m+1;j++) a[0][j]=a[n+1][j]=2;
  15. }
  16. void dfs(int x,int y)
  17. {
  18. stack<pair<int,int>> S;
  19. int d=1;
  20. S.push({x,y}); a[x][y]=3;
  21. while(S.size())
  22. {
  23. pair<int,int> u=S.top(); S.pop();
  24. for(int i=-1;i<=1;i++)
  25. for(int j=-1;j<=1;j++)
  26. if(a[u.first+i][u.second+j]==0)
  27. {
  28. a[u.first+i][u.second+j]=3;
  29. S.push({u.first+i,u.second+j});
  30. d++;
  31. }
  32. }
  33. Res.insert(d);
  34. }
  35. void bfs(int x,int y)
  36. {
  37. queue<pair<int,int>> S;
  38. int d=1;
  39. S.push({x,y}); a[x][y]=3;
  40. while(S.size())
  41. {
  42. pair<int,int> u=S.front(); S.pop();
  43. for(int i=-1;i<=1;i++)
  44. for(int j=-1;j<=1;j++)
  45. if(a[u.first+i][u.second+j]==0)
  46. {
  47. a[u.first+i][u.second+j]=3;
  48. S.push({u.first+i,u.second+j});
  49. d++;
  50. }
  51. }
  52. Res.insert(d);
  53. }
  54. void sol()
  55. {
  56. nhap();
  57. for(int i=1;i<=n;i++)
  58. for(int j=1;j<=m;j++) if(a[i][j]==0) bfs(i,j);
  59. cout<<Res.size()<<"\n";
  60. for(auto r:Res) cout<<r<<" ";
  61. }
  62. };
  63.  
  64. int main()
  65. {
  66. anh A; A.sol();
  67. }
  68.  
  69.  
Success #stdin #stdout 0s 5568KB
stdin
Standard input is empty
stdout
0