UVa11686 - Pick up sticks(拓扑排序)

Problem C: Pick up sticks

《UVa11686 - Pick up sticks(拓扑排序)》
Pick up sticks is a fascinating game. A collection of coloured sticks are dumped in a tangled heap on the table. Players take turns trying to pick up a single stick at a time without moving any of the other sticks. It is very difficult to pick up a stick if there is another stick lying on top of it. The players therefore try to pick up the sticks in an order such that they never have to pick up a stick from underneath another stick.

Input Specification

The input consists of several test cases. The first line of each test case contains two integers 
n
 and 
m
 each at least one and no greater than one million. The integer 
n
is the number of sticks, and 
m
 is the number of lines that follow. The sticks are numbered from 1 to 
n
. Each of the following lines contains a pair of integers 
a

b
, indicating that there is a point where stick 
a
 lies on top of stick 
b
. The last line of input is  0 0
. These zeros are not values of 
n
 and 
m
, and should not be processed as such.

Sample Input

3 2
1 2
2 3
0 0

Output Specification

For each test case, output 
n
 lines of integers, listing the sticks in the order in which they could be picked up without ever picking up a stick with another stick on top of it. If there are multiple such correct orders, any one will do. If there is no such correct order, output a single line containing the word IMPOSSIBLE
.

Output for Sample Input

1
2
3

import java.io.FileInputStream;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.OutputStreamWriter;
import java.io.StreamTokenizer;

public class Main {
	public static final boolean DEBUG = false;
	public static final int N = 1000010;
	public BufferedReader cin;
	public PrintWriter cout;
	public StreamTokenizer tokenizer;
	public int n, m;
	public Vertex[] vertex;
	public int[] ans;
	public int cnt;
	public int[] vis;
	
	static class Vertex
	{
		int u;
		Edge firstEdge;
	}

	static class Edge
	{
		int u, v;
		Edge next;
	}
	
	public void init() {
		try {
			if (DEBUG) {
				cin = new BufferedReader(new InputStreamReader(
						new FileInputStream("e:\\uva_in.txt")));
			} else {
				cin = new BufferedReader(new InputStreamReader(System.in));
			}

			tokenizer = new StreamTokenizer(cin);
			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;
			
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	public boolean input() 
	{
		n = Integer.parseInt(next());
		m = Integer.parseInt(next());
		
		if (n == 0 && m == 0) return false;

		
		vertex = new Vertex[n];
		for (int i = 0; i < n; i++) {
			vertex[i] = new Vertex();
			vertex[i].u = i;
			vertex[i].firstEdge = null;
		}

		for (int i = 0; i < m; i++) {
			int a, b;
			a = Integer.parseInt(next());
			b = Integer.parseInt(next());
			a--; b--;
			Edge edge = new Edge();
			edge.u = a;
			edge.v = b;
			edge.next = vertex[a].firstEdge;
			vertex[a].firstEdge = edge;
		}
		
		return true;
	}

	public boolean dfs(int u)
	{
		vis[u] = -1;

		for (Edge edge = vertex[u].firstEdge;  edge != null;  edge = edge.next) {
			int v = edge.v;
			if (vis[v] == -1) return false;
			else if (vis[v] == 0 && !dfs(v)) return false;
		}

		vis[u] = 1;
		ans[--cnt] = u + 1;
		return true;
	}

	public boolean topoSort()
	{
		vis = new int[n];
		ans = new int[n];
		cnt = n;

		for (int i = 0; i < n; i++) {
			if (vis[i] == 0) {
				if (!dfs(i)) return false;
			}
		}

		return true;
	}

	
	public void solve() 
	{
		boolean ok = topoSort();
		if (!ok) {
			cout.println("IMPOSSIBLE");
		} else {
			for (int i = 0; i < n; i++) {
				cout.println(ans[i]);
			}
		}

		cout.flush();
		
	}
	
	public static void main(String[] args) 
	{
		Main solver = new Main();
		solver.init();

		while (solver.input()) {
			solver.solve();
		}
	}
}

    原文作者:拓扑排序
    原文地址: https://blog.csdn.net/xiexingshishu/article/details/38272885
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞