Minimum Cut(2015年吉林網絡賽)

思路:

貪心結論:切出一個點的合格切法一定符合最小原則

證明:

加入當前方法切出n個結點,則切掉的樹邊兩頭的節點之一進行切除可小化數目


根據此結論,可以對結點進行遍歷維護最小值。

爲保證切除且僅切除一條樹邊,通過遍歷樹邊的兩個端點即可。

遍歷時檢查每個結點的樹邊數量與非樹邊的圖邊數量


#include <stdio.h>
#include <string.h>
#include <math.h>
#include <algorithm>
#define MAX 200010

using namespace std;

struct link
{
	int x1, x2;
};

link l[200010];  // 記錄樹邊
int num1[20010]; // 記錄樹邊數量
int num2[20010]; // 記錄非樹邊圖邊數量

int main()
{
	int T;
	scanf("%d", &T);

	for (int i = 1; i <= T; ++i)
	{
		memset(num1, 0, sizeof(num1));
		memset(num2, 0, sizeof(num2));
		int num_node;
		int num_link;
		scanf("%d %d", &num_node, &num_link);
		for (int j = 0; j < num_node-1; ++j)
		{
			int a, b;
			scanf("%d %d", &a, &b);
			l[j].x1 = a;
			l[j].x2 = b;
			num1[a]++;
			num1[b]++;
		}
		for (int j = 0; j < num_link - num_node + 1; ++j)
		{
			int a, b;
			scanf("%d %d", &a, &b);
			num2[a]++;
			num2[b]++;
		}

		int res = MAX;
		for (int j = 0; j < num_node-1; ++j)
		{
			int temp1 = l[j].x1;
			int temp2 = l[j].x2;
			if(num1[temp1]<2)
			{
				res = min(res, num2[temp1]);
			}
			if(num1[temp2]<2)
			{
				res = min(res, num2[temp2]);
			}
			//if(res==0)
                //printf("%d\n", j);
		}
		printf("Case #%d: %d\n", i, res+1);
	}
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章