比赛 20151026 评测结果 C
题目名称 游历校园 最终得分 0
用户昵称 琴殇''遙暩焱鐄 运行时间 0.000 s
代码语言 C++ 内存使用 0.00 MiB
提交时间 2015-10-26 21:23:35
显示代码纯文本
#include <algorithm>
#include <fstream>
#include <cstdlib>
#include <string>
#include <cstdio>
#include <vector>
#include <cmath>

#define MAXN 100005

using namespace std;

ifstream cin ("sent.in");
ofstream cout ("sent.out");

int n, m, a, b, len, cnt;
vector <int> eto[MAXN];
int blocks[MAXN], hash[MAXN];

void DfsEdges (int x, int now)
{
	len = eto[now].size ();
	for (int i = 0; i < len; i ++)
	{
		blocks[eto[now][i]] = x;
		DfsEdges (x, eto[now][i]);
	}
}

void FindBlocks (void)
{
	for (int i = 1; i <= n; i ++)
		if (blocks[i] == i)
			DfsEdges (i, i);
}

int CountAns (void)
{
	for (int i = 1; i <= n; i ++)
		hash[blocks[i]] ++;
	
	for (int i = 1; i <= n; i ++)
		if (hash[i] > 1)
			cnt ++;
	int ans = cnt - 1;
	
	memset (hash, 0, sizeof (hash));
	for (int i = 1; i <= n; i ++)
		if (eto[i].size () % 2)
			hash[blocks[i]] ++;
	
	for (int i = 1; i <= n; i ++)
		if (hash[i] > 2)
			ans += (hash[i] - 2) / 2;
	
	return ans;
}

int main ()
{
	cin >> n >> m;
	for (int i = 1; i <= m; i ++)
	{
		cin >> a >> b;
		eto[a].push_back (b);
	}
	
	for (int i = 1; i <= n; i ++)
		blocks[i] = i;
	
	FindBlocks ();
	
	cout << CountAns () << endl;
	
	return 0;
}