FZU - 2112 Tickets (欧拉通路+联通块)

题意:给出n个点,m条边,添加最少的边,可以不重复的走m条边一次

思路:一笔画问题,就是求欧拉通路,欧拉通路:在无向连通图中,有0个或2个奇度顶点。如果有4个奇度顶点,我们只需在任意两个奇度点之间添加一条边就行,因为并没有说是连通图,所以如果有n个联通块的话,我们还需添加n-1条边。

这道题不判断联通块也能过。。。。。。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <vector>

using namespace std;
typedef long long ll;
const int INF = 0x3f3f3f3f;
int deg[100005];
int vis[100005];
int cnt;
vector<int> v[100005];
void dfs(int node){

	if(deg[node] & 1){
		cnt++;
	}

	for(int i = 0; i < v[node].size(); i++){
		if(!vis[v[node][i]]){
			
			vis[v[node][i]] = 1;
			dfs(v[node][i]);
			
		}
	}
}

int main(void){
	
	int t;
	cin >> t;
	while(t--){
		
		int n,m;
		scanf("%d%d",&n,&m);
		for(int i = 1; i <= n; i++){
			deg[i] = 0;
			vis[i] = 0;
		} 
		while(m--){
			int x,y;
			scanf("%d%d",&x,&y);
			v[x].push_back(y);
			v[y].push_back(x);
			deg[x]++;
			deg[y]++;
		}
		
		//如果有一个点没有出现在m条边里,我们就不需要加上它了
		//第一次没写这个,wa了 
		for(int i = 1; i <= n; i++){
			if(deg[i] == 0){
				vis[i] = 1;
			}
		}
		
		int total = 0;
		int cntt = 0;//联通块的个数 
		for(int i = 1; i <= n; i++){
			if(vis[i] == 0){
				cnt = 0;
				vis[i] = 1;
				cntt++;
				dfs(i);
				if(cnt >= 2){
					total += (cnt - 2) / 2;
				}
			}
		}
	    printf("%d\n",total + cntt - 1);	
	}
	return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值