ural 1272. Non-Yekaterinburg Subway

题目链接:http://acm.timus.ru/problem.aspx?space=1&num=1272


题意描述:图里现存两种连接方式(通道):tunnel和bridge,修地铁顾名思义在地下,所以bridge尽量少;那么任务就是用现有的两种通道建立连通图,给出需要的最少的bridge数;


思路就是并查集,先用tunnel合并,再用bridge合并;


AC代码:


#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <stdio.h>
#include <string>
#include <algorithm>
#include <vector>
#include <queue>
#include <cmath>
#include <map>
using namespace std;

struct Node{
	int x, y;
};

vector<int>parent;

int getParent(int num){
	return num == parent[num] ? num : parent[num] = getParent(parent[num]);
}

bool link(Node *node){
	int a = getParent(node->x);
	int b = getParent(node->y);

	if (a != b){
		parent[a] = b;
		return true;
	}

	return false;
}

void func(){
	
	int N, T, B;
	cin >> N >> T >> B;
	for (int i = 0; i <= N; i++)
		parent.push_back(i);

	Node node;

	for (int i = 0; i < T; i++){
		cin >> node.x >> node.y;
		link(&node);
	}

	int ans = 0;
	for (int i = 0; i < B; i++){
		cin >> node.x >> node.y;
		if (link(&node))ans++;
	}

	cout << ans << endl;

}

int main(){

	freopen("out.txt", "w", stdout);
	freopen("in.txt", "r", stdin);

	func();

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值