微软面试题 JAVA编程
描述
小Hi和小Ho准备国庆期间去A国旅游。A国的城际交通比较有特色:它共有n座城市(编号1-n);城市之间恰好有n-1条公路相连,形成一个树形公路网。小Hi计划从A国首都(1号城市)出发,自驾遍历所有城市,并且经过每一条公路恰好两次——来回各一次——这样公路两旁的景色都不会错过。
令小Hi苦恼的是他的小伙伴小Ho希望能以某种特定的顺序游历其中m个城市。例如按3-2-5的顺序游历这3座城市。(具体来讲是要求:第一次到达3号城市比第一次到达2号城市早,并且第一次到达2号城市比第一次到达5号城市早)。
小Hi想知道是否有一种自驾顺序满足小Ho的要求。
输入
输入第一行是一个整数T(1<=T<=20),代表测试数据的数量。
每组数据第一行是一个整数n(1 <= n <= 100),代表城市数目。
之后n-1行每行两个整数a和b (1 <= a, b <= n),表示ab之间有公路相连。
之后一行包含一个整数m (1 <= m <= n)
最后一行包含m个整数,表示小Ho希望的游历顺序。
输出
YES或者NO,表示是否有一种自驾顺序满足小Ho的要求。
package None;
import java.util.Scanner;
public class go {
public static void main(String[] args){
int x1,x2,index;
//Boolean lean = true;
//System.out.print("输入实验个数:");
Scanner sc2 = new Scanner(System.in);
int w2 = Integer.parseInt(sc2.nextLine());
Boolean lean[] = new Boolean[w2];
for(int i = 0;i for(int f = 0;f //System.out.print("输入城市数目:"); Scanner sc = new Scanner(System.in); int w = Integer.parseInt(sc.nextLine()); int a[] = new int[w+1]; int b[][] = new int[w+1][w+1]; //System.out.print("输入城市道路:"); for(int i=1; i Scanner s = new Scanner(System.in); String str = s.nextLine(); String ss[] = str.split(" "); x1 = Integer.parseInt(ss[0]); x2 = Integer.parseInt(ss[1]); if(a[x1] == 0) a[x1]=1; a[x2]=a[x1]+1; b[x1][x2] = 1; } //System.out.print("输入顺序城市数目:"); Scanner s1 = new Scanner(System.in); int w1 = Integer.parseInt(s1.nextLine()); int c[] = new int[w1+1]; c[0]=1; //System.out.print("输入具体城市:"); Scanner s = new Scanner(System.in); String str = s.nextLine(); String ss[] = str.split(" "); for(int i = 1; i c[i] = Integer.parseInt(ss[i-1]); } for(int i=0;i x1 = c[i]; x2 = c[i+1]; if(a[x1]>a[x2]){index = x1;x1 = x2;x2 = index;} while(a[x1] != a[x2]){ for(int q=1;q if(b[q][x2] != 0 && a[q]<=a[x1]) { b[q][x2]++; x2 = q; } } } while(a[x1] == a[x2] && x1 != x2){ for(int q=1;q if(b[q][x1] != 0 && a[x1] >= a[x2]) { b[q][x1]++; x1 = q; } if(b[q][x2] != 0 && a[x2] >= a[x1]) { b[q][x2]++; x2 = q; } } } } for(int i=1;i for(int j=1;j if(b[i][j] > 3) lean[f] = false; } } //System.out.println(); for(int f = 0;f System.out.println(lean[f]); } } }