Advertisement
nguyentien281006

TPLT-test03

Sep 28th, 2022
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.33 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. #define int long long
  3. using namespace std;
  4. int n, m, ans = 0, q = 0, cnt;
  5. vector<vector<int>> outp;
  6. vector<vector<int>> adj;
  7. vector<int> visited;
  8.  
  9. void dfs(int u){
  10.     outp[ans].push_back(u);
  11.     cnt++;
  12.     visited[u] = true;
  13.     for(auto &x : adj[u]){
  14.         if(!visited[x]) {
  15.             dfs(x);
  16.         }
  17.     }
  18. }
  19.  
  20. void connectCheck(int n){
  21.     for(int i = 1; i<=n; i++){
  22.         if(!visited[i]){
  23.             ans++;
  24.             cnt = 0;
  25.             dfs(i);
  26.             if(q<cnt) q = cnt;
  27.         }
  28.     }
  29. }
  30.  
  31. void output(){
  32.     for(int i = 1; i <= n; i++){
  33.         if( (int)outp[i].size() < 1 ) break;
  34.         for(auto &x : outp[i]){
  35.             cout << x << " ";
  36.         }
  37.         cout << "\n";
  38.     }
  39. }
  40.  
  41. signed main(){
  42.     ios::sync_with_stdio(false);
  43.     cin.tie(nullptr);
  44.     //freopen("TPLT.inp", "r", stdin);
  45.     //freopen("TPLT.out", "w", stdout);
  46.     cin >> n >> m;
  47.     adj.resize(n+1);
  48.     visited.resize(n+1, 0);
  49.     outp.resize(n+1);
  50.     for(int i = 0; i< m; i++){
  51.         int x, y; cin >> x >> y;
  52.         adj[x].push_back(y);
  53.         adj[y].push_back(x);
  54.     }
  55.     connectCheck(n);
  56.     cout << ans << " " << q << "\n";
  57.     output(); //hàm ni để in ra các đỉnh thuộc thành phần liên thông
  58.     return 0;
  59. }
  60.  
  61. /*
  62. 8 7
  63. 1 2
  64. 1 3
  65. 2 3
  66. 2 4
  67. 3 5
  68. 4 6
  69. 7 8
  70. */
  71.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement