陈正磊


私信TA

用户名:RossTran

访问量:13603

签 名:

晨兴理荒秽,带月荷锄归。

等  级
排  名 178
经  验 6675
参赛次数 15
文章发表 34
年  龄 0
在职情况 学生
学  校 湖北生物科技职业学院
专  业

  自我简介:

坑题,题目描述不清楚。


1:“ 接下来n行每行两个数字表示相互连通的点”,题目说相互连通容易误解为两个顶点可以相互畅通,其实只是a点到b点,b不能到a。

2: 题目没有说明接下来输入的n行数字的范围,通过样例输入很容易误解为范围<=n。


这题,不做也罢!


import java.util.Scanner;

public class Main {

	/**
	 * @param args
	 */
	public static int arr[][],flag[][],n;//存储输入数据
	public static boolean result;
	public static void main(String[] args) {
		Scanner sc=new Scanner(System.in);
		while (sc.hasNext()) {
			n=sc.nextInt();
			
			flag=new int[n+10][n+10];//标记是否走过
			arr=new int[n+10][n+10];
			//输入
			for (int i = 0; i <n; i++) {
				int start=sc.nextInt();
				int end=sc.nextInt();
				arr[start][end]=1;//标记有路
			}
			int p=sc.nextInt();//初始位置
			result=true;
			dfs(p);
			if (result) {
				System.out.println("Yes");
			}else {
				System.out.println("No");
			}
		}
}
	//搜索
	public static void dfs(int detp) {
		for (int i = 1; i <=n; i++) {
			if (arr[detp][i]==1 && flag[detp][i]==1) {//原地打转
				result=false;
				return ;
			}
			if (arr[detp][i]==1 && flag[detp][i]==0) {//有路线,且没走过
				flag[detp][i]=1;
				dfs(i);
				flag[detp][i]=0;
			}
		}
	}
	
	
}


 

0.0分

4 人评分

  评论区

卧槽 磊哥还是顶啊
2020-10-13 14:58:57
厉害啊,我之前一直没看懂题目是什么意思
2020-09-26 22:07:34
  • «
  • 1
  • »