Trust groups
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 ( 1P1000, 0T999000) 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();
}
}
}