CodeForces 1133F2 : Spanning Tree with One Fixed Degree tarjan + 并查集

该博客讨论了一种图论问题,即如何构造一个以节点1为中心,度数为DDD的无环无重边树。首先通过缩点判断与节点1相连的边中桥边数量,如果超过DDD则无解。接着处理边数小于DDD的情况,最后通过并查集维护剩余边的选择,确保构建的树满足条件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

传送门

题目描述

给你一个图,让你构造出一个编号为1的点的度数为 D D D的树
(保证没有自环和重边)

分析

是个麻烦题
首先我们去缩点,判断一下和 1 1 1连接的边中,桥边的数量为 p p p,如果 p > D p > D p>D,那么显然是无解的
然后如果和 1 1 1连接的边数小于 D D D,那么也是无解的
剩下的,和 1 1 1连接的桥边是必选的,剩下的用并查集维护一下即可

代码

#pragma GCC optimize(3)
#include <bits/stdc++.h>
#define debug(x) cout<<#x<<":"<<x<<endl;
#define dl(x) printf("%lld\n",x);
#define di(x) printf("%d\n",x);
#define _CRT_SECURE_NO_WARNINGS
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> PII;
typedef vector<int> VI;
const int INF = 0x3f3f3f3f;
const int N = 2e5 + 10,M = N * 2;
const ll mod = 1000000007;
const double eps = 1e-9;
const double PI = acos(-1);
template<typename T>inline void read(T &a) {
	char c = getchar(); T x = 0, f = 1; while (!isdigit(c)) {if (c == '-')f = -1; c = getchar();}
	while (isdigit(c)) {x = (x << 1) + (x << 3) + c - '0'; c = getchar();} a = f * x;
}
int gcd(int a, int b) {return (b > 0) ? gcd(b, a % b) : a;}
int h[N],ne[M],e[M],idx;
int dfn[N],low[N],ti;
stack<int> Q;
VI nums[N];
int n,m,D;
int q[N];

void add(int x,int y){
	ne[idx] = h[x],e[idx] = y,h[x] = idx++;
}

int find(int x){
	if(x != q[x]) q[x] = find(q[x]);
	return q[x];
}

void tarjan(int u,int from){
    dfn[u] = low[u] = ++ti;
    for(int i = h[u];~i;i = ne[i]){
    	int j = e[i];
    	if(j == from) continue;
    	if(!dfn[j]) tarjan(j,u);
    	else low[u] = min(low[u],dfn[j]);
    	if(dfn[u] < low[j]) nums[u].pb(j);
    }
}

int main() {
	memset(h,-1,sizeof h);
	read(n),read(m),read(D);
	int p = 0;
	while(m--){
		int x,y;
		read(x),read(y);
		add(x,y),add(y,x);
		if(x == 1 || y == 1) p++;
	}
	tarjan(1,-1);
	if(p < D || nums[1].size() > D){
		puts("NO");
		return 0;
	}
	puts("YES");
	for(int i = 1;i <= n;i++) q[i] = i;
	for(auto t:nums[1]) {
		int x = 1,y = t;
		x = find(x),y = find(y);
		if(x != y) {
			q[x] = y;
			printf("%d %d\n",1,t);
			D--;
		}
	}
	for(int i = h[1];~i && D;i = ne[i]){
		int x = find(1),y = find(e[i]);
		if(x != y){
			D--;
			printf("%d %d\n",1,e[i]);
			q[y] = x;
		}
	}
	for(int i = 2;i <= n;i++)
		for(int j = h[i];~j;j = ne[j]){
			if(e[j] == 1) continue;
			int x = find(i),y = find(e[j]);
			if(x != y){
				D--;
				printf("%d %d\n",i,e[j]);
				q[y] = x;
			}
		}
	return 0;
}

/**
*  ┏┓   ┏┓+ +
* ┏┛┻━━━┛┻┓ + +
* ┃       ┃
* ┃   ━   ┃ ++ + + +
*  ████━████+
*  ◥██◤ ◥██◤ +
* ┃   ┻   ┃
* ┃       ┃ + +
* ┗━┓   ┏━┛
*   ┃   ┃ + + + +Code is far away from  
*   ┃   ┃ + bug with the animal protecting
*   ┃    ┗━━━┓ 神兽保佑,代码无bug 
*   ┃        ┣┓
*    ┃        ┏┛
*     ┗┓┓┏━┳┓┏┛ + + + +
*    ┃┫┫ ┃┫┫
*    ┗┻┛ ┗┻┛+ + + +
*/
 
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值