HDU - 3861 The King’s Problem

In the Kingdom of Silence, the king has a new problem. There are N cities in the kingdom and there are M directional roads between the cities. That means that if there is a road from u to v, you can only go from city u to city v, but can’t go from city v to city u. In order to rule his kingdom more effectively, the king want to divide his kingdom into several states, and each city must belong to exactly one state. What’s more, for each pair of city (u, v), if there is one way to go from u to v and go from v to u, (u, v) have to belong to a same state. And the king must insure that in each state we can ether go from u to v or go from v to u between every pair of cities (u, v) without passing any city which belongs to other state. 
  Now the king asks for your help, he wants to know the least number of states he have to divide the kingdom into.

Input

The first line contains a single integer T, the number of test cases. And then followed T cases. 

The first line for each case contains two integers n, m(0 < n <= 5000,0 <= m <= 100000), the number of cities and roads in the kingdom. The next m lines each contains two integers u and v (1 <= u, v <= n), indicating that there is a road going from city u to city v.

Output

The output should contain T lines. For each test case you should just output an integer which is the least number of states the king have to divide into.

Sample Input

1
3 2
1 2
1 3

Sample Output

2

 

#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10;
int n, m;
struct nod{
	int head[maxn];
	int dfn[maxn];
	int low[maxn];
	bool vis[maxn];
	int colour[maxn];
	int indu[maxn];
	int dist[maxn];
	int tot;
	int tot1;
	int sum;//联通块个数&&颜色
	int dex;
	int cnt;
	int ans;
	struct node{
		int to;
		int next;
		int from;
		node() {}
		node(int a, int b, int c) : to(a), next(b), from(c) {}
	}edge[maxn], edge1[maxn];
	 
	void edgeadd(int a, int b){
		edge[tot] = node(b, head[a], a);
		head[a] = tot++;
	}
	
	void init(){
		memset(head, -1, sizeof(head));
		memset(vis, 0, sizeof(vis));
		memset(dfn, 0, sizeof(dfn));
		memset(indu, 0, sizeof(indu));
		tot = 0;
		tot1 = 0;
		sum = 0;	
	dex = 0;
		ans = 0;
	}
	stack<int> s;
	 
	void tarjan(int u){
		dfn[u] = low[u] = ++dex;
		s.push(u);
		vis[u] = 1;
		for(int i = head[u]; i != -1; i = edge[i].next){
			int v = edge[i].to;
			if(!dfn[v]){
				tarjan(v);
				low[u] = min(low[u], low[v]);
			} 
			else if(vis[v]){
				low[u] = min(low[u], dfn[v]);
			}
		}
		if(low[u] == dfn[u]){
			sum++;
			while(1){
				int now = s.top();
				//cout << now << endl;
				colour[now] = sum;
				s.pop();
				vis[now] = 0;
				if(now == u) break;
			}	
		}
	}
	 	
}T;


struct Hungary                                //最大匹配
{
	vector<int> g[maxn];                      //关系图
	int from[maxn], mx[maxn];
	bool use[maxn];
	int n, ans;
 
	void init(int nn)                          //初始化
	{
		n = nn;
		for(int i=1;i<=n;i++)
		   g[i].clear();
	}
 
	void add_edge(int u, int v)                //填加有向边
	{
		g[u].push_back(v);
	}
 
	bool match(int x)                           //匹配
	{
		for(int i=0;i<g[x].size();i++)
			if (!use[g[x][i]])
			{
				use[g[x][i]] = true;
				if (from[g[x][i]] == -1 || match(from[g[x][i]]))
				{
					from[g[x][i]] = x;
					//mx[x] = g[x][i];
					return true;
				}
			}
		return false;
	}
 
	int get_ans()                               //求得最大匹配值
	{
		ans = 0;
		memset(from, -1, sizeof(from));
		//memset(mx, 0, sizeof(mx));
		for(int i=1;i<=n;i++)
		{
			memset(use, 0, sizeof(use));
			if (match(i))ans++;
		}
		return ans;
	}
}H;

int main(){
	ios::sync_with_stdio(false);
	int t;
	cin >> t;
	while(t--){
		cin >> n >> m;
		T.init();
		int x, y;
		for(int i = 1; i <= m; i++){
			cin >> x >> y;
			T.edgeadd(x, y);
		}
		for(int i = 1; i <= n; i++){
			if(!T.dfn[i])
				T.tarjan(i);
		}
		memset(T.head, -1, sizeof(T.head));
		H.init(T.sum);
		int ans = 0;
		for(int i = 0; i < m; i++){
			int o, g;
			o = T.colour[T.edge[i].from];
			g = T.colour[T.edge[i].to];
			if(o != g){
				H.add_edge(o, g);
				ans++;
			}
		}
		cout << T.sum - H.get_ans() << endl;	 
	}
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值