Advertisement
Josif_tepe

Untitled

Feb 8th, 2024
965
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.63 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <cstring>
  4. #include <fstream>
  5. #include <algorithm>
  6. #include <map>
  7. using namespace std;
  8. typedef long long ll;
  9. const int maxn = 2e5 + 10;
  10. vector<int> graph[maxn];
  11. bool mark_centroid[maxn];
  12. int n;
  13. void subtree_dfs(int node, vector<bool> & visited, vector<int> & subtree_size, int & cnt_nodes) {
  14.     visited[node] = true;
  15.     subtree_size[node] = 1;
  16.     cnt_nodes++;
  17.     for(int neighbour : graph[node]) {
  18.         if(!visited[neighbour] and !mark_centroid[neighbour]) {
  19.             subtree_dfs(neighbour, visited, subtree_size, cnt_nodes);
  20.             subtree_size[node] += subtree_size[neighbour];
  21.         }
  22.     }
  23. }
  24. int find_centroid(int node, vector<bool> & visited, vector<int> & subtree_size, int & cnt_nodes) {
  25.     bool is_centroid = true;
  26.     visited[node] = true;
  27.     int child = 0;
  28.     for(int neighbour : graph[node]) {
  29.         if(!visited[neighbour] and !mark_centroid[neighbour]) {
  30.             if(subtree_size[neighbour] > n / 2) {
  31.                 is_centroid = false;
  32.             }
  33.             if(child == 0 or subtree_size[neighbour] > subtree_size[child]) {
  34.                 child = neighbour;
  35.                
  36.             }
  37.         }
  38.     }
  39.     if(is_centroid and cnt_nodes - subtree_size[node] <= n / 2) {
  40.         return node;
  41.     }
  42.     return find_centroid(child, visited, subtree_size, cnt_nodes);
  43. }
  44. int centroid(int node) {
  45.     int cnt_nodes = 0;
  46.     vector<bool> visited(n + 10, false);
  47.     vector<int> subtree_size(n + 10, 0);
  48.     subtree_dfs(1, visited, subtree_size, cnt_nodes);
  49.     for(int i = 0; i < n + 10; i++) {
  50.         visited[i] = false;
  51.     }
  52.     int cent = find_centroid(node, visited, subtree_size, cnt_nodes);
  53.     mark_centroid[cent] = true;
  54.     return cent;
  55. }
  56. vector<int> cent_tree[maxn];
  57. int centoroid_decomposition(int node) {
  58.     int cent = centroid(node);
  59.     cout << cent << " ";
  60.     for(int neighbour : graph[cent]) {
  61.         if(!mark_centroid[neighbour]) {
  62.             int subtree = centoroid_decomposition(neighbour);
  63.            
  64.             cent_tree[cent].push_back(subtree);
  65.             cent_tree[subtree].push_back(cent);
  66.            
  67.         }
  68.     }
  69.     return cent;
  70. }
  71. int main() {
  72.     ios_base::sync_with_stdio(false);
  73.     cin >> n;
  74.     memset(mark_centroid, false, sizeof mark_centroid);
  75.     for(int i = 0; i < n - 1; i++) {
  76.         int a, b;
  77.         cin >> a >> b;
  78.         graph[a].push_back(b);
  79.         graph[b].push_back(a);
  80.     }
  81.     cout << centoroid_decomposition(1) << endl;
  82.     return 0;
  83. }
  84. /*
  85.  16
  86.  1 4
  87.  2 4
  88.  3 4
  89.  4 5
  90.  5 6
  91.  6 7
  92.  7 8
  93.  7 9
  94.  6 10
  95.  10 11
  96.  11 12
  97.  12 13
  98.  12 14
  99.  13 15
  100.  13 16
  101.  **/
  102.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement