题意:
一个n个节点的树的 label sequence就是选n次叶子,每次选的叶子是字典序最小的,选了之后去掉
给定一个图,求它的一个生成树,使得它的 label sequence字典序最小
对于一个节点,如果它是割顶的话它在生成树中就一定不能当叶子。
生成树可以看成有次序地选n个节点,每次选的节点要么不是割顶,要么是叶子,所以每选一个节点都重新算一下每个节点是不是割顶,
并且每次选的节点要尽可能的小
运气好,最近看下了大白的图论,但是还不会写,果断抄大白
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<cmath>
#include<set>
#include<map>
#include<queue>
#include<vector>
#include<functional>
#include<cstdlib>
using namespace std;
#define LL long long
#define ULL unsigned long long
#define inf 0x3f3f3f3f
#define ls ( i << 1 )
#define rs ( ls | 1 )
#define md ( ( ll[i] + rr[i] ) >> 1 )
#define eps 1e-8
#define mxn 1020
int first[mxn], nxt[mxn*mxn], to[mxn*mxn], e;
bool vis[mxn];
int in[mxn]; // in[i]表示与节点i相连边的边数
bool iscut[mxn];
int pre[mxn], low[mxn], dfs_clock, n, m;
vector<int>ans;
void init() {
memset( first, -1, sizeof( first ) );
memset( in, 0, sizeof( in ) );
e = 0;
ans.clear();
dfs_clock = 0;
memset( vis, 0, sizeof( vis ) );
}
void add( int u, int v ) {
to[e] = v;
nxt[e] = first[u];
first[u] = e ++;
}
void read() {
scanf( "%d%d", &n, &m );
for( int i = 1; i <= m; ++i ) {
int u, v;
scanf( "%d%d", &u, &v );
add( u, v );
add( v, u );
in[u] ++;
in[v] ++;
}
}
int dfs( int u, int fa ) {
int lowu = pre[u] = ++dfs_clock;
int ch = 0;
for( int i = first[u]; i != -1; i = nxt[i] ) {
int v = to[i];
if( vis[v] )
continue;
if( !pre[v] ) {
ch++;
int lowv = dfs( v, u );
lowu = min( lowu, lowv );
if( lowv >= pre[u] ) {
iscut[u] = 1;
}
}
else
if( pre[v] < pre[u] && v != fa )
lowu = min( lowu, pre[v] );
}
if( fa < 0 && ch == 1 )
iscut[u] = 0;
low[u] = lowu;
return lowu;
}
void solve() {
memset( vis, 0, sizeof( vis ) );
while( 1 ) {
for( int i = 0; i < n; ++i ) {
if( vis[i] )
continue;
memset( iscut, 0, sizeof( iscut ) );
memset( pre, 0, sizeof( pre ) );
dfs_clock = 0;
dfs( i, -1 );
for( int j = 0; j < n; ++j ) {
if( vis[j] )
continue;
if( !iscut[j] || in[j] == 1 || in[j] == 0 ) {
vis[j] = 1; // 节点j被选后要去掉和它相连的所有边
for( int k = first[j]; k != -1; k = nxt[k] ) {
if( vis[to[k]] )
continue;
in[to[k]]--;
}
ans.push_back( j );
break;
}
}
break;
}
if( ans.size() == n )
break;
}
for( int i = 0; i < n; ++i )
printf( "%d%c", ans[i], i == n - 1 ? '\n': ' ' );
}
int main() {
// freopen( "tt.txt", "r", stdin );
int cas;
scanf( "%d", &cas );
while( cas-- ) {
init();
read();
solve();
}
return 0;
}