|
| 1 | +#include <cstdio> |
| 2 | +#include <queue> |
| 3 | + |
| 4 | +#define VERTEX_COUNT 100010 |
| 5 | +#define EDGE_COUNT 200010 |
| 6 | + |
| 7 | +using namespace std; |
| 8 | + |
| 9 | +struct vertex |
| 10 | +{ |
| 11 | + int first, subcnt, fa; |
| 12 | + bool vis; |
| 13 | +}V[VERTEX_COUNT]; |
| 14 | + |
| 15 | +struct edge |
| 16 | +{ |
| 17 | + int endp, next; |
| 18 | +}E[EDGE_COUNT]; |
| 19 | + |
| 20 | +int ivc, ec = 2, prufer[VERTEX_COUNT], pc; |
| 21 | +priority_queue< int, vector<int>, greater<int> > Q; |
| 22 | + |
| 23 | +inline void add_edge(int u, int v) |
| 24 | +{ |
| 25 | + E[ec].next = V[u].first; |
| 26 | + V[u].first = ec; |
| 27 | + E[ec].endp = v; |
| 28 | + ec++; |
| 29 | +} |
| 30 | + |
| 31 | +void dfs(int u, int fa) |
| 32 | +{ |
| 33 | + V[u].vis = true; |
| 34 | + V[u].fa = fa; |
| 35 | + for (int cur = V[u].first; cur != 0; cur = E[cur].next) |
| 36 | + { |
| 37 | + if (V[E[cur].endp].vis == false) dfs(E[cur].endp, u), V[u].subcnt++; |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +int main() |
| 42 | +{ |
| 43 | + int u, v; |
| 44 | + scanf("%d", &ivc); |
| 45 | + for (int i = 1; i < ivc; i++) |
| 46 | + { |
| 47 | + scanf("%d%d", &u, &v); |
| 48 | + add_edge(u, v); |
| 49 | + add_edge(v, u); |
| 50 | + } |
| 51 | + dfs(1, 0); |
| 52 | + for (int i = 1; i <= ivc; i++) |
| 53 | + { |
| 54 | + if (E[V[i].first].next == 0) Q.push(i); |
| 55 | + } |
| 56 | + for (int i = 3; i <= ivc; i++) |
| 57 | + { |
| 58 | + int u = Q.top();printf("pop : %d, fa = %d\n", u, V[u].fa); |
| 59 | + Q.pop(); |
| 60 | + if (V[u].fa == 0) |
| 61 | + { |
| 62 | + for (int cur = V[u].first; cur != 0; cur = E[cur].next) |
| 63 | + { |
| 64 | + if (V[E[cur].endp].fa != 0) |
| 65 | + { |
| 66 | + prufer[pc++] = E[cur].endp; |
| 67 | + V[E[cur].endp].fa = 0; |
| 68 | + if (V[E[cur].endp].subcnt == 1) Q.push(E[cur].endp); |
| 69 | + break; |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | + else |
| 74 | + { |
| 75 | + prufer[pc++] = V[u].fa; |
| 76 | + V[V[u].fa].subcnt--; |
| 77 | + if (V[V[u].fa].subcnt == 0) Q.push(V[u].fa); |
| 78 | + else if (V[V[u].fa].subcnt == 1 && V[V[u].fa].fa == 0) Q.push(V[u].fa); |
| 79 | + } |
| 80 | + } |
| 81 | + for (int i = 0; i < pc; i++) |
| 82 | + { |
| 83 | + printf("%d ", prufer[i]); |
| 84 | + } |
| 85 | + return 0; |
| 86 | +} |
0 commit comments