DFS tree cut demand point problem

Time complexity: O (n metaphysics) is not short

Code implementation ( good trouble, purple and blue theme title )

  1 #include<iostream>
  2 #include<string.h>
  3 #include<algorithm>
  4 #include<vector>
  5 #include<map>
  6 #include<bitset>
  7 #include<set>
  8 #include<string>
  9 #if !defined(_WIN32)
 10 #include<bits/stdc++.h>
 11 #endif // !defined(_WIN32)
 12 #define ll long long
 13 #define dd double
 14 using namespace std;
 15 int n, m;
 16 int tot;
 17 ll ans;
 18 struct edge
 19 {
 20     bool t;
 21     bool flag;
 22     int to;
 23     int num;
 24     int next;
 25 }e[500005];
 26 struct node
 27 {
 28     int son;
 29     bool flag;
 30     int f;
 31     int head;
 32     int d;
 33     int deep;
 34 }p[100086];
 35 int vis[100086];
 36 void add(int x, int y)
 37 {
 38     tot++;
 39     e[tot].to = y;
 40     e[tot].next = p[x].head;
 41     p[x].head = tot;
 42 }
 43 bool check(int x)
 44 {
 45     for (int i = p[x].head; i; i = e[i].next)
 46     {
 47         if (e[i].flag && !e[i].t)
 48         {
 49             int to = e[i].to;
 50             if (!(p[to].d < p[x].deep))
 51                 return 0;
 52         }
 53     }
 54     return 1;
 55 }
 56 void dfs(int x, int f)
 57 {
 58     vis[x] = 1;
 59     p[x].deep = p[f].deep + 1;
 60     p[x].d = p[x].deep;
 61     for (int i = p[x].head; i; i = e[i].next)
 62     {
 63         int to = e[i].to;
 64         if (!vis[to])
 65         {
 66             p[x].son++;
 67             p[to].f = x;
 68             e[i].flag = 1;
 69             dfs(to, x);
 70         }
 71     }
 72 }
 73 void init(int x)
 74 {
 75     for (int i = p[x].head; i; i = e[i].next)
 76     {
 77         int to = e[i].to;
 78         if (e[i].flag && !e[i].t)
 79         {
 80             init(to);
 81             p[x].d = min(p[x].d, p[to].d);
 82         }
 83     }
 84 }
 85 void work()
 86 {
 87     for (int x = 1; x <= n; x++)
 88     {
 89         if (x == 1)
 90         {
 91             if (p[x].son <= 1)
 92                 p[x].flag = 1;
 93         }
 94         else if (p[x].son == 0)
 95         {
 96             p[x].flag = 1;
 97         }
 98         else
 99         {
100             if (check(x))
101                 p[x].flag = 1;
102         }
103     }
104 }
105 int main()
106 {
107     cin >> n >> m;
108     for (int i = 1; i <= m; i++)
109     {
110         int x, y;
111         cin >> x >> y;
112         add(x, y);
113         add(y, x);
114     }
115     dfs(1, 0);
116     for (int i = 1; i <= n; i++)
117     {
118         for (int j = p[i].head; j; j = e[j].next)
119         {
120             int to = e[j].to;
121             if (!e[j].flag && to != p[i].f)
122             {
123                 p[i].d = min(p[i].d, p[to].deep);
124             }
125             else if (to == p[i].f)
126             {
127                 e[j].t = 1;
128             }
129         }
130     }
131     init(1);
132     work();
133     for (int i = 1; i <= n; i++)
134     {
135         if (!p[i].flag)
136             cout << i << endl;
137     }
138     return 0;
139 }

 

Guess you like

Origin www.cnblogs.com/HNFOX/p/11273855.html