HDU 5639 Deletion

Problem Description
There is an undirected graph  G  with  n  vertices and  m  edges. Every time, you can select several edges and delete them. The edges selected must meet the following condition: let  G  be graph induced from these edges, then every connected component of  G  has at most one cycle. What is the minimum number of deletion needed in order to delete all the edges.
 

Input
There are multiple test cases. The first line of input contains an integer  T  indicating the number of test cases. For each test case:

The first line contains two integers  n  and  m   (1n2000,0m2000)  -- the number of vertices and the number of edges.

For the next  m  lines, each line contains two integers  ui  and  vi , which means there is an undirected edge between  ui  and  vi   (1ui,vin,uivi) .

The sum of values of  n  in all test cases doesn't exceed  2104 . The sum of values of  m  in all test cases doesn't exceed  2104 .
 

Output
For each test case, output the minimum number of deletion needed.
 

Sample Input
  
  
3 4 2 1 2 1 3 4 5 1 2 1 3 1 4 2 3 2 4 4 4 1 2 2 3 3 4 4 1
 

Sample Output
  
  
1 2 1
表示想不到,参考的是题解中的解法2,把原图中的边新建成一个点与原图中的两个点相连建成二分图,然后二分答案验证时否完全匹配
#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
#include<functional>
#include<algorithm>
using namespace std;
typedef long long LL;
const int maxn=2e3+10;
const int mod=1e9+7;
int T,n,m,x[maxn],y[maxn];

struct MaxFlow
{
	const static int maxe = 2e6 + 10;    //边数
	const static int maxp = 1e5 + 10;   //点数
	const static int INF = 0x7FFFFFFF;
	struct Edges
	{
		int x, f;
		Edges(){}
		Edges(int x, int f) :x(x), f(f){}
	}edge[maxe];
	int first[maxp], next[maxe], dis[maxp], tot, work[maxp], n;

	void clear(int x){ n = x; tot = 0; for (int i = 0; i <= n; i++) first[i] = -1; }

	void AddEdge(int s, int t, int f)
	{
		edge[tot] = Edges(t, 0); next[tot] = first[s]; first[s] = tot++;
		edge[tot] = Edges(s, f); next[tot] = first[t]; first[t] = tot++;
	}

	bool bfs(int s, int t)
	{
		for (int i = 0; i <= n; i++) dis[i] = -1;
		queue<int> p;    p.push(s);    dis[s] = 0;
		while (!p.empty())
		{
			int q = p.front();    p.pop();
			for (int i = first[q]; i != -1; i = next[i])
			{
				if (edge[i ^ 1].f&&dis[edge[i].x] == -1)
				{
					p.push(edge[i].x);
					dis[edge[i].x] = dis[q] + 1;
					if (dis[t] != -1) return true;
				}
			}
		}
		return false;
	}

	int dfs(int s, int t, int low)
	{
		if (s == t) return low;
		for (int &i = work[s], x; i >= 0; i = next[i])
		{
			if (dis[s] + 1 == dis[edge[i].x] && edge[i ^ 1].f && (x = dfs(edge[i].x, t, min(low, edge[i ^ 1].f))))
			{
				edge[i].f += x;    edge[i ^ 1].f -= x;	return x;
			}
		}
		return 0;
	}

	int dinic(int s, int t)
	{
		int maxflow = 0, inc = 0;
		while (bfs(s, t))
		{
			for (int i = 0; i <= n; i++) work[i] = first[i];
			while (inc = dfs(s, t, INF)) maxflow += inc;
		}
		return maxflow;
	}
}solve;

bool check(int flow)
{
	solve.clear(n+m+1);
	for (int i=1;i<=m;i++) 
	{
		solve.AddEdge(i,0,1);
		solve.AddEdge(m+x[i],i,1);
		solve.AddEdge(m+y[i],i,1);
	}
	for (int i=m+1;i<=m+n;i++) solve.AddEdge(n+m+1,i,flow);
	return solve.dinic(n+m+1,0)==m;
}

int main()
{
	scanf("%d",&T);
	while (T--)
	{
		scanf("%d%d",&n,&m);
		for (int i=1;i<=m;i++) scanf("%d%d",&x[i],&y[i]);
		if (!m) {printf("0\n"); continue;}
		int q=1,h=m;
		while (q<=h)
		{
			int mid=q+h>>1;
			if (check(mid)) h=mid-1; else q=mid+1;
		}
		printf("%d\n",q);
	}
	return 0;
}


评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值