1. 程式人生 > >【打CF,學演算法——三星級】CodeForces 216B Forming Teams (圖論)

【打CF,學演算法——三星級】CodeForces 216B Forming Teams (圖論)

題面:

B. Forming Teams time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output

One day n students come to the stadium. They want to play football, and for that they need to split into teams, the teams must have an equal number of people.

We know that this group of people has archenemies. Each student has at most two archenemies. Besides, if student A is an archenemy to student B, then student B is an archenemy to student A.

The students want to split so as no two archenemies were in one team. If splitting in the required manner is impossible, some students will have to sit on the bench.

Determine the minimum number of students you will have to send to the bench in order to form the two teams in the described manner and begin the game at last.

Input

The first line contains two integers n and m (2 ≤ n ≤ 100, 1 ≤ m ≤ 100) — the number of students and the number of pairs of archenemies correspondingly.

Next m lines describe enmity between students. Each enmity is described as two numbers ai and bi (1 ≤ ai, bi ≤ n, ai ≠ bi) — the indexes of the students who are enemies to each other. Each enmity occurs in the list exactly once. It is guaranteed that each student has no more than two archenemies.

You can consider the students indexed in some manner with distinct integers from 1 to n.

Output

Print a single integer — the minimum number of students you will have to send to the bench in order to start the game.

Examples Input
5 4
1 2
2 4
5 3
1 4
Output
1
Input
6 2
1 4
3 4
Output
0
Input
6 6
1 2
2 3
3 1
4 5
5 6
6 4
Output
2

--------------------------------------------------------------------------------------做完再看------------------------------------------------------------------------------------------------------------------------------

題意:

    給定n個朋友,m個敵對關係。敵對關係是相互的,即a和b是敵對的,那麼b和a也是敵對的,具有敵對關係的兩個人不能在一個小組,一個人最多隻有兩個敵對物件。現在要求將所有人分成兩個小組,使得兩組人數相同,且內部不具備敵對關係,且做板凳(即未被選中的人的數量儘量少)。

解題:

    將題目給定的敵對關係構造成圖,可以發現,聯通塊要麼是環,要麼是鏈,要麼就是孤立點(因為一個點最多隻有2的度數)。分析可以比較容易得出,奇數長度的環,必須犧牲一個人做板凳,因為選出的len-1人分為兩個小組,必有一人與兩個小組的人都存在敵對關係,故而找奇環,發現則做板凳人數數量加一。如果是鏈,可以i,i+1分別分配給兩個小組,就不會出現敵對關係,但奇數鏈會損失一個,但不同鏈之間無敵對關係,因此可以連結所有鏈,最終鏈長度為奇數,則損失一個。

     注意:找奇環的過程,開始寫錯了,應該是計算一個聯通塊上的總度數,並記錄是否出現了度數為1或者為0的點,沒有則判定為環,奇環的總度數/2是奇數,可以通過這點判斷奇環。

程式碼:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <set>
#include <map>
using namespace std;
bool vis[105];
bool con[105][105];
int degree[105];
int minuss=0,n,m,sum;
bool flag;
void dfs(int x,int pre)
{
	vis[x]=1;
	if(degree[x]==1||degree[x]==0)
		flag=1;
	sum+=degree[x];
    for(int i=1;i<=n;i++)
	{
		if(!vis[i]&&con[x][i]&&i!=pre)
        {
			dfs(i,x);
		}
	}
}
int main()
{
	int a,b,ans;
	scanf("%d%d",&n,&m);
	for(int i=1;i<=m;i++)
	{
		scanf("%d%d",&a,&b);
		degree[a]++;
		degree[b]++;
		con[a][b]=con[b][a]=1;
	}
	for(int i=1;i<=n;i++)
	{
		if(!vis[i])
        {
			sum=0;
			flag=0;
			dfs(i,-1);
			if(!flag)
		    {
				if(sum/2%2)
				minuss++;
			}
		}
	}
	ans=minuss+(n-minuss)%2;
	printf("%d\n",ans);
	return 0;
}

相關推薦

CF演算法——三星級CodeForces 216B Forming Teams

題面: B. Forming Teams time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard o

CF演算法——三星級CodeForces 645C Enduring Exodus 二分+貪心

Note In the first sample, Farmer John can book room 3 for himself, and rooms 1 and 4 for his cows. The distance to the farthest cow is 2. Note that it is

CF演算法——三星級CodeForces 550D Regular Bridge 構造

題面: D. Regular Bridge time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard

CF演算法——二星級Codeforces 22B Bargaining Table區域和

提交連結:CF 22B 題面: B. Bargaining Table time limit per test 2 seconds memory limit per test 256 megabytes input standard input out

CF演算法——三星級Codeforces 704A Thor (模擬)

題面: A. Thor time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Th

CF演算法——三星級Codeforces 698A Vacations (動態規劃)

Note In the first test Vasya can write the contest on the day number 1 and do sport on the day number 3. Thus, he will have a rest for only 2 days. In the

CF演算法——三星級CodeForces 689B Mike and Shortcuts 最短路+spfa

題面: B. Mike and Shortcuts time limit per test 3 seconds memory limit per test 256 megabytes input standard input output stand

CF演算法——三星級CodeForces 513C Second price auction 進位制狀態表示

Nowadays, most of the internet advertisements are not statically linked to a web page. Instead, what will be shown to the person opening a web page is det

CF演算法——三星級CodeForces 701B Cells Not Under Attack 分析

題面: B. Cells Not Under Attack time limit per test 2 seconds memory limit per test 256 megabytes input standard input output st

CF演算法——三星級CodeForces 689C Mike and Chocolate Thieves 二分

Note In the first sample case the smallest n that leads to exactly one way of stealing chocolates isn = 8, whereas the amounts of stealed chocolates are(1

CF演算法——二星級CodeForces 237B Young Table 構造

題面: B. Young Table time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output

CF演算法——三星級CF Gym 100548K Last Denfence

題面:  Last Defence Description     Given two integers A and B. Sequence S is defined as follow:  

CF演算法——二星級CodeForces 520C DNA Alignment (構造)

題面: C. DNA Alignment time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard o

CF演算法——五星級CodeForces 478D (dp計數)

題面: D. Red-Green Towers time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standar

CF演算法——二星級Codeforces 705B Spider Man (簡單博弈)

Peter Parker wants to play a game with Dr. Octopus. The game is about cycles. Cycle is a sequence of vertices, such that first one is connected with the

CF演算法——二星級CodeForces 417B Crash 水題

During the "Russian Code Cup" programming competition, the testing system stores all sent solutions for each participant. We know that many participants u

CF演算法CodeForces網站簡介

你應當知道的關於Codeforces的事情 Codeforces 簡稱: cf(所以談論cf的時候經常被誤會成TX的那款遊戲). 網址: codeforces.com   這是一個俄國的演算法競賽網站,由來自薩拉托夫州立大學、由Mike Mirzayanov領導

CF演算法——一星級CodeForces 617D Polyline水題

There are three points marked on the coordinate plane. The goal is to make a simple polyline, without self-intersections and self-touches, such that it pa

CF演算法——一星級CodeForces 701A Cards水題

題面: A. Cards time limit per test 1 second memory limit per test 256 megabytes input standar

CF演算法——一星級CodeForces 227B Effective Approach 水題

Once at a team training Vasya, Petya and Sasha got a problem on implementing linear search in an array. According to the boys, linear search works as foll