20pts求调
查看原帖
20pts求调
1361104
Jack_2011_Liu楼主2024/7/7 19:19

rt,代码如下

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;
struct student
{
	int num;
	vector <student*> next;
	bool school;
};
int main()
{
	vector <student*> stu;
	int n, m;
	cin >> n >> m;
	int ans_a = 0, ans_b = 0;
	vector <bool> solve;
	for (int i = 0; i < n; i++)
	{
		solve.push_back(true);
		student* temp=new student;
		temp->num = i;
		stu.push_back(temp);
	}
	for (int i = 0; i < m; i++)
	{
		int a, b;
		cin >> a >> b;
		stu[a - 1]->next.push_back(stu[b - 1]);
		stu[b - 1]->next.push_back(stu[a - 1]);
	}

	vector <bool>::iterator it = solve.begin();
	int index = 0;
	while (it != solve.end())
	{
		if (*it)
		{
			int num_a = 0, num_b = 0;
			queue <student*> ts;
			ts.push(stu[index]);
			bool goal = true;
			ts.front()->school = goal;
			num_a++;
			solve[ts.front()->num] = false;
			while (!ts.empty())
			{
				for (int i = 0; i < ts.front()->next.size(); i++)
				{
					student* next = ts.front()->next[i];
					if (solve[next->num])
					{
						ts.push(next);
						next->school = !goal;
						if (!goal) num_a++;
						else num_b++;
						solve[next->num] = false;
					}
				}
				ts.pop();
				goal = !goal;
			}

			if (num_a >= num_b)
			{
				ans_a += num_a;
				ans_b += num_b;
			}
			else
			{
				ans_a += num_b;
				ans_b += num_a;
			}
		}
		it++;
		index++;
	}
	printf("%d %d", ans_b, ans_a);
	return 0;
}
2024/7/7 19:19
加载中...