UVa11686 - Pick up sticks(拓扑排序)

Problem C: 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();
		}
	}
}



  • 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、付费专栏及课程。

余额充值