2013
11-11

# Part Acquisition

The cows have been sent on a mission through space to acquire a new milking machine for their barn. They are flying through a cluster of stars containing N (1 <= N <= 50,000) planets, each with a trading post.

The cows have determined which of K (1 <= K <= 1,000) types of objects (numbered 1..K) each planet in the cluster desires, and which products they have to trade. No planet has developed currency, so they work under the barter system: all trades consist of each party trading exactly one object (presumably of different types).

The cows start from Earth with a canister of high quality hay (item 1), and they desire a new milking machine (item K). Help them find the best way to make a series of trades at the planets in the cluster to get item K. If this task is impossible, output -1.

* Line 1: Two space-separated integers, N and K.

* Lines 2..N+1: Line i+1 contains two space-separated integers, a_i and b_i respectively, that are planet i’s trading trading products. The planet will give item b_i in order to receive item a_i.

* Line 1: One more than the minimum number of trades to get the milking machine which is item K (or -1 if the cows cannot obtain item K).

* Lines 2..T+1: The ordered list of the objects that the cows possess in the sequence of trades.

6 5
1 3
3 2
2 3
3 1
2 5
5 4

4
1
3
2
5

OUTPUT DETAILS:

The cows possess 4 objects in total: first they trade object 1 for object 3, then object 3 for object 2, then object 2 for object 5.

//* @author: ccQ.SuperSupper
import java.io.*;
import java.util.*;
public class Main {
static final int N = 10000+10;
static int meat[] = new int[N],pre[] = new int[N],n,K;
static Vector Graph[] = new Vector[N];
static Queue que = new LinkedList();
static void start(){
for(int i=0;i< N;++i)
Graph[i] = new Vector();
}
static void init(){
que.clear();
for(int i=0;i< N;++i){
Graph[i].clear();
meat[i] = N+N;
pre[i] = -1;
}
meat[1] = 1;
que.offer(1);
}

public static void main(String[]args) throws Exception{
int i,u,v;
start();
init();

n = Get_Num(cin);
K = Get_Num(cin);
for(i=0;i< n;++i){
v = Get_Num(cin);
u = Get_Num(cin);
}

solve();
if(meat[K]>=N) System.out.println(-1);
else{
System.out.println(meat[K]);
make_ans();
}
}

static void solve(){
int i,j,k,temp;
while(!que.isEmpty()){
temp = (Integer) que.element();
que.remove();
k = Graph[temp].size();
for(i=0;i< k;++i){
j = (Integer) Graph[temp].elementAt(i);
if(meat[j]>meat[temp]+1){
meat[j] = meat[temp]+1;
pre[j] = temp;
}
}
}
}

static void make_ans(){
int ans[] = new int[N],i,k=K,top=0;
while(true){
ans[top++] = k;
k = pre[k];
if(k< 0) break;
}
PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
for(i=top-1;i>=0;--i)
out.println(ans[i]);
out.flush();
}

static int Get_Num(StreamTokenizer cin) throws Exception{
cin.nextToken();
return (int) cin.nval;
}
}

1. #!/usr/bin/env python
def cou(n):
arr =
i = 1
while(i<n):
arr.append(arr[i-1]+selfcount(i))
i+=1
return arr[n-1]

def selfcount(n):
count = 0
while(n):
if n%10 == 1:
count += 1
n /= 10
return count

2. 您没有考虑 树的根节点是负数的情况， 若树的根节点是个很大的负数，那么就要考虑过不过另外一边子树了