UVa11709 - Trust groups(Kosaraju,十字链表)

Trust groups 

The personnel department of Association of Cookie Monsters (ACM) has noticed that the productivity of various work groups in the company is not as good as it could be. They have interviewed the employees in the affected groups and they have detected the root of the problem: trust (or, rather, the lack thereof). Some employees do not trust the rest of the group, and this is decreasing their motivation and happiness. The personnel department wants to solve this problem, and has decided to reorganize the groups so that they are stable, i.e., they are formed by people who trust each other. They have asked the employees, and they know the people each employee trusts directly. Moreover, if employee A trusts employee B and employee B trusts employee C, then employee A will trust employee C. And obviously, each employee trusts himself. They want to create as few groups as possible to reduce administration overhead (they also do not want to work too hard).

With this information they have contacted you, and asked you to write a program that finds the minimum number of stable groups that can be created.

Input 

The input consists of several test cases. Each test case begins with a line containing two positive integers P and T ( 1$ \le$P$ \le$1000, 0$ \le$T$ \le$999000) separated by a single space. P lines come next, each containing the name of one person. The names will have the following format: surname, a comma, a space and first name (for example McBride, John or Smith, Peter). Both the surname and the first name will be strings of uppercase or lowercase characters (with no blanks or punctuation marks), with a maximum length of 10 characters. There will not be repetitions in the complete names of the people. After the names there will appear T blocks of 2 lines representing the trust relations between people. Each line of the block will contain the name of a person in the same format as before, and the block will mean that the person in the first line trusts the person in the second line. All people appearing in the confidence relations will have appeared in the previous list of P people.

The input will end with the ``phantom'' test case `0 0', which must not be processed.

Output 

For each test case, the output will be a line containing a positive integer representing the minimum number of stable groups of people that can be formed.

Sample Input 

3 2
McBride, John
Smith, Peter
Brown, Anna
Brown, Anna
Smith, Peter
Smith, Peter
Brown, Anna
3 2
McBride, John
Smith, Peter
Brown, Anna
Brown, Anna
Smith, Peter
McBride, John
Smith, Peter
0 0

Sample Output 

2
3



用Kosaraju来求强连通分量,同时用到了十字链表来表示有向图

import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.util.HashMap;
import java.util.Arrays;

public class Main 
{
	private static final boolean DEBUG = false;
	private BufferedReader cin;
	private PrintWriter cout;
	private int p, t;
	private Vertex[] vertexs;
	private boolean[] vis;
	private int cnt;
	private int[] topo;
	
	class Vertex
	{
		Edge firstin, firstout;
	}
	
	class Edge
	{
		int u, v;
		Edge hnext, tnext;
	}
	
	public void init() 
	{
		try {
			if (DEBUG) {
				cin = new BufferedReader(new InputStreamReader(
						new FileInputStream("d:\\OJ\\uva_in.txt")));
			} else {
				cin = new BufferedReader(new InputStreamReader(System.in));
			}
			cout = new PrintWriter(new OutputStreamWriter(System.out));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public String next()
	{ 
		
		try {
			/*
			tokenizer.nextToken();
			if (tokenizer.ttype == StreamTokenizer.TT_EOF) return null;
			else if (tokenizer.ttype == StreamTokenizer.TT_NUMBER) 
				return String.valueOf((int)tokenizer.nval);
			else return tokenizer.sval;
			*/
			return cin.readLine();
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		
		
		
	}
	
	public boolean input() 
	{
		String s = next();
		StringTokenizer tokenizer = new StringTokenizer(s);
		
		p = Integer.parseInt(tokenizer.nextToken());
		t = Integer.parseInt(tokenizer.nextToken());
		
		if (p == 0 && t == 0) return false;
		
		vertexs = new Vertex[p];
		for (int i = 0; i < p; i++) {
			vertexs[i] = new Vertex();
			vertexs[i].firstin = null;
			vertexs[i].firstout = null;
		}
		
		HashMap<String, Integer> strMap = new HashMap<String, Integer>();
		for (int i = 0; i < p; i++) {
			s = next();
			strMap.put(s, i);
		}
		
		for (int i = 0; i < t; i++) {
			String s1 = next();
			String s2 = next();
			int u = strMap.get(s1);
			int v = strMap.get(s2);
			Edge edge = new Edge();
			edge.u = u; edge.v = v;
			edge.tnext = vertexs[u].firstout;
			edge.hnext = vertexs[v].firstin;
			vertexs[v].firstin = edge;
			vertexs[u].firstout = edge;
		}
		
		return true;
	}

	private void dfs(int u)
	{
		vis[u] = true;
		for (Edge edge = vertexs[u].firstout; edge != null; edge = edge.tnext) {
			int v = edge.v;
			if (!vis[v]) {
				dfs(v);
			}
		}
		topo[cnt++] = u;
	}
	
	private void idfs(int v)
	{
		vis[v] = true;
		for (Edge edge = vertexs[v].firstin; edge != null; edge = edge.hnext) {
			int u = edge.u;
			if (!vis[u]) {
				idfs(u);
			}
		}
	}
	
	public void solve() 
	{
		cnt = 0;
		vis = new boolean[p];
		topo = new int[p];
		
		for (int i = 0; i < p; i++) {
			if (!vis[i]) dfs(i);
		}
		
		
		int ans = 0;
		Arrays.fill(vis, false);
		for (int i = p - 1; i >= 0; i--) {
			if (!vis[topo[i]]) {
				ans++;
				idfs(topo[i]);
			}
		}
		cout.println(ans);
		cout.flush();
		
	}

	public static void main(String[] args) 
	{
		Main solver = new Main();
		solver.init();
		
		while (solver.input()) {
			solver.solve();
		}
	}
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kgduu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值