现在的位置: 首页 > 综合 > 正文

hdu 3861 The King’s Problem

2019年02月20日 ⁄ 综合 ⁄ 共 3188字 ⁄ 字号 评论关闭

The King’s Problem

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 1792    Accepted Submission(s): 652

Problem Description
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
 

Source
2011 Multi-University Training Contest 3 - Host by BIT
题意貌似挺难懂,可参考

http://zhidao.baidu.com/link?url=vSDaJzKCDF4gGnLZwvriRZoUG24Bsyvw_WEdPkOu7RaHuGNADkmkUbqj2YQcvnjtK4EsUtYdkNuJTP-ubStOuq

红色那句很好理解,强连通分量一定在同一个州里,但是后面那句,是说:国王确保对于每一个州,每一对city(u, v),u -> v 或者v->u一定不经过别的州,换句话说,同一个州里可以有单向边,(即一个州不一定是整个强连通分量)。那么样例就很好理解了, (1 2 )(3) 或者(1 3 )(2)。问最少的州的个数,就是求最小路径覆盖,图缩点之后新图上求一下就好。

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<queue>
#include<stack>

using namespace std;

const int N = 5005;
const int inf = 1 << 28;

struct node{
    int to, nxt;
}e[N*20];

struct pp{
    int st, ed;
}p[N*20];

int head[N], vis[N];
int scc[N], sccnum;
int cnt, index;
int pay[N];
int low[N], dfn[N];
int n, m;
stack<int> s;
int in[N];
int link[N];

void init()
{
    sccnum = cnt = index = 0;
    memset(head, -1, sizeof(head));
    memset(low, 0, sizeof(low));
    memset(vis, 0, sizeof(vis));
    memset(in, 0, sizeof(in));
    memset(dfn, 0, sizeof(dfn));
    while( !s.empty() )
        s.pop();
}

void add(int u, int v)
{
    e[cnt].to = v;
    e[cnt].nxt = head[u];
    head[u] = cnt++;
}

void tarjan( int u )
{
    dfn[u] = low[u] = ++index;	//memset(0的时候++index,-1就随意了)
    vis[u] = 1;
    s.push(u);
    for( int i = head[u]; ~i; i = e[i].nxt )
    {
        int to = e[i].to;
        if( !dfn[to] )
        {
            tarjan(to);
            low[u] = min(low[u], low[to]);
        }
        else if( vis[to] )
        {
            low[u] = min(low[u], dfn[to]);
        }
    }
    if( low[u] == dfn[u] )
    {
        sccnum++;
        while( 1 )
        {
            int tmp = s.top();
            s.pop();
            vis[tmp] = 0;
            scc[tmp] = sccnum;
            if( low[tmp] == dfn[tmp] )
                break;
        }
    }
}

int dfs( int u )
{
	for( int i = head[u]; ~i; i = e[i].nxt )
	{
		int to = e[i].to;
		if( !vis[to] )
		{
			vis[to] = 1;
			if( link[to] == -1 || dfs(link[to]) )
			{
				link[to] = u;
				return 1;
			}
		}
	}
	return 0;
}

int main()
{
	int tot;
	scanf("%d", &tot);
    while(tot--)
    {
    	scanf("%d%d", &n, &m);
        init();
        int u, v;
        for( int i = 1; i <= m; i++ )
        {
            scanf("%d%d", &u, &v);
            p[i].st = u;
            p[i].ed = v;
            add(u, v);
        }
        for( int i = 1; i <= n; i++ )
        {
            if( !dfn[i] )
                tarjan(i);
        }
        memset(head, -1, sizeof(head));
        cnt = 0;
        for( int i = 1 ; i <= m ; i++ )
        {
            if( scc[ p[i].st ] != scc[ p[i].ed ] )
            //如果起点终点不在同一个联通块里,起点所在联通块出度++ ,终点的入度++ 
            {
                add(scc[ p[i].st ], scc[ p[i].ed ]);
            }
        }
        memset(link, -1, sizeof(link));
        int ans = 0;
        for( int i = 1; i <= sccnum; i++)
        {
        	memset(vis, 0, sizeof(vis));
        	ans += dfs( i );
        }
        printf("%d\n", sccnum - ans);
    }
    return 0;
}

抱歉!评论已关闭.