Advertisement
tepyotin2

Tree Width

May 11th, 2025
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.75 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. struct Con{
  6.     int val;
  7.     int layer;
  8. };
  9.  
  10. int n;
  11. string grid;
  12. vector<int> adj[1001];
  13. int C[1001];
  14. int ans;
  15. bool visited[1001];
  16. queue<Con> q;
  17.  
  18. int main(){
  19.     //freopen("treewidth.in", "r", stdin);
  20.    
  21.     cin >> n;
  22.     for(int i=0; i<n; i++){
  23.         cin >> grid;
  24.         for(int j=0; j<n; j++){
  25.             if(grid[j] == '1'){
  26.                 adj[i+1].push_back(j+1);
  27.             }
  28.         }
  29.     }
  30.     q.push({1, 0});
  31.     visited[1] = true;
  32.     while(!q.empty()){
  33.         Con cur = q.front();
  34.         q.pop();
  35.         //cout << cur.val << ", " << cur.layer << '\n';
  36.         C[cur.layer]++;
  37.         ans = max(ans, C[cur.layer]);
  38.         for(auto v: adj[cur.val]){
  39.             if(!visited[v]){
  40.                 visited[v] = true;
  41.                 q.push({v, cur.layer+1});
  42.             }
  43.         }
  44.     }
  45.     cout << ans << '\n';
  46.    
  47.     return 0;
  48. }
  49.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement