第二筆測資總是TLE
import java.util.Scanner;
public class Bin {
public static void main(String []argv){
Scanner input = new Scanner(System.in);
while(input.hasNext()){
int city=input.nextInt(), num = input.nextInt();
boolean flag = false;
boolean[][] road = new boolean[city][city];
for(int x=0; x<num; x++){
int a=input.nextInt()-1, b=input.nextInt()-1;
road[a][b] = true;
}
int form=input.nextInt()-1, to=input.nextInt()-1;
boolean[] used = new boolean[city];
for(int x=0; x<city; x++){
if(road[form][to] == true){
System.out.println("Yes!!!");
flag = true;
break;
}
if(road[form][x]==true && used[x] != true){
used[x] = true;
for(int y=0; y<city; y++){
if(road[x][y]==true){
road[form][y] = true;
}
}
}
}
if(flag==false){
System.out.println("No!!!");
}
}
input.close();
}
}
整理一下。。
格式跑掉了
import java.util.Scanner;
public class Bin {
public static void main(String []argv){
Scanner input = new Scanner(System.in);
while(input.hasNext()){
int city=input.nextInt(), num = input.nextInt();
boolean flag = false;
boolean[][] road = new boolean[city][city];
for(int x=0; x<num; x++){
int a=input.nextInt()-1, b=input.nextInt()-1;
road[a][b] = true;
}
int form=input.nextInt()-1, to=input.nextInt()-1;
boolean[] used = new boolean[city];
for(int x=0; x<city; x++){
if(road[form][to] == true){
System.out.println("Yes!!!");
flag = true;
break;
}
if(road[form][x]==true && used[x] != true){
used[x] = true;
for(int y=0; y<city; y++){
if(road[x][y]==true){
road[form][y] = true;
}
}
}
}
if(flag==false){
System.out.println("No!!!");
}
}
input.close();
}
}