hdu 3861 The King’s Problem

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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
根据提供的引用内容,HDU1622是一道关于二叉树的题目,要求读入一系列二叉树的节点信息,输出它们的层序遍历结果。如果输入的二叉树不完整或存在重复节点,则输出"not complete"。下面是Java的实现代码: ```java import java.util.LinkedList; import java.util.Queue; import java.util.Scanner; public class Main { static class Node { int val; Node left, right; public Node(int val) { this.val = val; } } public static void main(String[] args) { Scanner sc = new Scanner(System.in); while (sc.hasNext()) { String s = sc.nextLine(); if (s.isEmpty()) { continue; } String[] nodes = s.split("\\s+"); Node root = new Node(Integer.parseInt(nodes[0].substring(1))); Queue<Node> queue = new LinkedList<>(); queue.offer(root); boolean isComplete = true; for (int i = 1; i < nodes.length - 1; i += 2) { Node cur = queue.poll(); if (!nodes[i].equals("()")) { cur.left = new Node(Integer.parseInt(nodes[i].substring(1))); queue.offer(cur.left); } else { isComplete = false; } if (!nodes[i + 1].equals("()")) { cur.right = new Node(Integer.parseInt(nodes[i + 1].substring(0, nodes[i + 1].length() - 1))); queue.offer(cur.right); } else { isComplete = false; } } if (!isComplete) { System.out.println("not complete"); continue; } StringBuilder sb = new StringBuilder(); queue.offer(root); while (!queue.isEmpty()) { Node cur = queue.poll(); sb.append(cur.val).append(" "); if (cur.left != null) { queue.offer(cur.left); } if (cur.right != null) { queue.offer(cur.right); } } System.out.println(sb.toString().trim()); } } } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值