第一行一个数表示数据组数
每组数据第一行两个数。含义见题意。
接下来m行每行两个数表示
到
之间有一条边
每组数据一行如果能回到公司输出“POSSIBLE”,不能输出"IMPOSSIBLE"。
1 4 3 1 2 2 3 3 4
IMPOSSIBLE
import java.util.*;
import java.util.LinkedList;
public class Main {
public static boolean isTouch;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int T = sc.nextInt();
while (T -- > 0) {
isTouch = false;
int n = sc.nextInt();
int m = sc.nextInt();
Map<Integer, List<Integer>> map = new HashMap<>();
while (m -- > 0) {
int a = sc.nextInt();
int b = sc.nextInt();
List<Integer> tmpList = map.getOrDefault(a, new ArrayList<>());
tmpList.add(b);
map.put(a, tmpList);
tmpList = map.getOrDefault(b, new ArrayList<>());
tmpList.add(a);
map.put(b, tmpList);
}
boolean[] vis = new boolean[n];
vis[0] = true;
helper(map, vis, 1, n, 2);
System.out.println(isTouch == true ? "POSSIBLE" : "IMPOSSIBLE");
}
}
public static void helper(Map<Integer, List<Integer>> map, boolean[] vis, int start, int end, int tili) {
if (isTouch) return;
if (tili == 0) {
if (start == end) {
isTouch = true;
}
return;
}
if (map.containsKey(start)) {
List<Integer> tmpList = map.get(start);
for (int t : tmpList) {
if (vis[t - 1]) continue;
vis[t - 1] = true;
helper(map, vis, t, end, tili - 1);
vis[t - 1] = false;
}
}
}
} // 标准bfs import java.util.*; public class Main{ public static void main(String[] args) { Scanner sc = new Scanner(System.in); int T = sc.nextInt(); // T组数据 for (int i = 0; i < T; i++) { // n个点,m条边 int n = sc.nextInt(); int m = sc.nextInt(); HashSet<Integer> visited = new HashSet<>(); Map<Integer, List<Integer>> neighbors = new HashMap<>(); for (int j = 0; j < n; j++) { neighbors.put(j + 1, new ArrayList<>()); } for (int j = 0; j < m; j++) { int point1 = sc.nextInt(); int point2 = sc.nextInt(); List<Integer> list1 = neighbors.get(point1); list1.add(point2); neighbors.put(point1, list1); List<Integer> list2 = neighbors.get(point2); list2.add(point1); neighbors.put(point2, list2); } boolean flag = false; int length = 0; Queue<Integer> queue = new LinkedList<>(); queue.add(1); while (!queue.isEmpty() && !flag) { int size = queue.size(); length++; for (int j = 0; j < size; j++) { int point = queue.poll(); if (point == n && length == 3) { flag = true; } visited.add(point); List<Integer> nexts = neighbors.get(point); for (int k = 0; k < nexts.size(); k++) { int next = nexts.get(k); if (!visited.contains(next)) { queue.add(next); } } } } if (flag){ System.out.println("POSSIBLE"); }else { System.out.println("IMPOSSIBLE"); } } } }
import java.util.*;
import java.io.*;
public class Main {
private static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
// 读取一个整数
private static int read() throws IOException {
Integer res = null;
boolean isNative = false;
for (;;) {
int ch = reader.read();
if (Character.isDigit(ch)) {
res = (res == null ? 0 : res * 10) + (ch - '0');
} else if (ch == '-') {
isNative = true;
} else if (res == null && ch != -1) {
continue;
} else if (res == null || !isNative) {
return res;
} else {
return -res;
}
}
}
public static void main(String[] args) throws IOException {
int T = read();
while (T-- > 0) {
int n = read(), m = read();
// 1 的邻接节点
Set<Integer> set1 = new HashSet<>();
// n 的邻接节点
Set<Integer> setn = new HashSet<>();
// 读取邻接边
for (int i = 0; i < m; ++i) {
int a = read(), b = read();
// 保证 a < b
if (a > b) {
a ^= b;
b ^= a;
a ^= b;
}
// 如果 a 是 1,放入集合
if (a == 1) {
set1.add(b);
}
// 如果 b 是 n,放入集合
if (b == n) {
setn.add(a);
}
}
boolean result = false;
// 1 和 n 之间有一条边,距离为 1
if (set1.contains(n)) {
result = true;
}
// 1 和 n 包含公共的邻接节点,距离为 2
else {
for (Integer i : set1) {
if (setn.contains(i)) {
result = true;
break;
}
}
}
// 输出结果
System.out.println(result ? "POSSIBLE" : "IMPOSSIBLE");
}
}
} import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int t = input.nextInt();
for (int i = 0; i < t; i++) {
int n = input.nextInt(), m = input.nextInt();
Map<Integer, List<Integer>> map = new HashMap<>();
int count = 2;
for (int j = 0; j < m; j++) {
int x = input.nextInt(), y = input.nextInt();
List<Integer> tempList = map.getOrDefault(x, new ArrayList<>());
tempList.add(y);
map.put(x, tempList);
tempList = map.getOrDefault(y, new ArrayList<>());
tempList.add(x);
map.put(y, tempList);
}
Main main = new Main();
if (main.findWay(map, 1, count, n)) {
System.out.println("POSSIBLE");
} else {
System.out.println("IMPOSSIBLE");
}
}
}
/**
* 寻找通路,使用递归
* @param map 道路的地图
* @param x 当前所在的点
* @param count 体力值
* @param end 终点,也可以使用全局变量
* @return 是否可达
*/
public boolean findWay(Map<Integer, List<Integer>> map, Integer x, int count, int end) {
if (x == end && count >= 0) {
// 如果到了终点,且体力值不为负数,则可通
return true;
} else if (x != end && count <= 0) {
// 没到达重点,就没体力了,则不通
return false;
} else {
// 否则进行递归
if (map.containsKey(x)) {
List<Integer> tempList = map.get(x);
for (Integer y : tempList) {
if (y < x) {
// 不能往回走,不然绕着走会内存超限
continue;
}
if (findWay(map, y, count - 1, end)) {
return true;
}
}
}
return false;
}
}
} C++ bfs求最短路
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 5;
vector<int> g[maxn];
int inq[maxn];
bool solve() {
int n, m, u, v;
cin >> n >> m;
for (int i = 1; i <= n; ++i) g[i].clear();
for (int i = 1; i <= m; ++i) {
scanf("%d%d", &u, &v);
g[u].push_back(v);
g[v].push_back(u);
}
memset(inq, 0, sizeof(inq));
queue<int> q;
q.push(1);
inq[1] = true;
int dis = 0;
while (!q.empty()) {
int sz = q.size();
while (sz--) {
int cur = q.front(); q.pop();
if (cur == n) return true;
for (int v : g[cur]) {
if (inq[v]) continue;
inq[v] = true;
q.push(v);
}
}
if (dis++ == 2) break;
}
return false;
}
int main() {
int t; cin >> t;
while (t--) {
if (solve()) puts("POSSIBLE");
else puts("IMPOSSIBLE");
}
return 0;
}
T=int(input())
for t in range(T):
n,m=list(map(int,input().split()))
s1,s2=set(),set()
for i in range(m):
a,b=list(map(int,input().split()))
if a==1:
s1.add(b)
if b==n:
s2.add(a)
if len(s1&s2)>0 or n in s1 or 1 in s2:
print("POSSIBLE")
else:
print("IMPOSSIBLE")
大佬们,为啥不对呢,样例也看不到
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Scanner;
class Point {
int index;
List<Point> neighbor;
boolean alreadyPassed = false;
public Point(int index) {
this.index = index;
this.neighbor = new ArrayList<>();
}
}
public class Main {
static boolean res;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int T = sc.nextInt();
for (int i = 0; i < T; i++) {
res = false;
int n = sc.nextInt();
int m = sc.nextInt();
//Point[]数组用来帮助保存相邻关系
Point[] points = new Point[n];
for (int j = 0; j < n; j++) {
points[j] = new Point(j + 1);
}
for (int j = 0; j < m; j++) {
int a = sc.nextInt() - 1;
int b = sc.nextInt() - 1;
points[a].neighbor.add(points[b]);
points[b].neighbor.add(points[a]);
}
points[0].alreadyPassed = true;
recur(points[0], 2, n);
System.out.println(res ? "POSSIBLE" : "IMPOSSIBLE");
}
}
private static void recur(Point point, int count, int n) {
if (res || count < 0) return;
if (point.index == n) {
res = true;
return;
}
for (Point neighbor : point.neighbor) {
if (neighbor.alreadyPassed) continue;
neighbor.alreadyPassed = true;
recur(neighbor, count - 1, n);
neighbor.alreadyPassed = false;
}
}
}
import java.lang.reflect.Array;
import java.util.*;
public class Main {
static class State{
public int id;
public int dist;
public State(int id,int dist) {
this.id=id;
this.dist=dist;
}
}
static List<Integer>[] grapLists;
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
for(int l=0;l<t;l++) {
int n=sc.nextInt();
int m=sc.nextInt();
grapLists=new List[n+1];
for(int i=1;i<=n;i++) {
grapLists[i]=new ArrayList<>();
}
for(int i=0;i<m;i++) {
int a=sc.nextInt();
int b=sc.nextInt();
grapLists[a].add(b);
grapLists[b].add(a);
}
int[] distTo=new int[n+1];
Arrays.fill(distTo, Integer.MAX_VALUE);
distTo[1]=0;
PriorityQueue<State> pq=new PriorityQueue<>(new Comparator<State>() {
@Override
public int compare(State o1, State o2) {
return o1.dist-o2.dist;
}
});
pq.add(new State(1,0));
while (!pq.isEmpty()){
State poll = pq.poll();
if(poll.dist>2){
System.out.println("IMPOSSIBLE");
break;
}
if(poll.id==n){
System.out.println("POSSIBLE");
break;
}
if(distTo[poll.id]<poll.dist){
continue;
}
List<Integer> list = grapLists[poll.id];
for (Integer integer : list) {
int newDist=poll.dist+1;
if(newDist<distTo[integer]){
distTo[integer]=newDist;
pq.add(new State(integer,newDist));
}
}
}
}
sc.close();
}
}
图的遍历