题意:求从1到N是否存在一条路,可以遍历每个节点。
思路:求任意两点之间是否通畅即可;
疑惑:完全暴力,bfs但是TLE,问题在于求连通分量(PS:不会)贴别人代码,先保存着。
1 #include <bits/stdc++.h>
2 #define CL(arr, val) memset(arr, val, sizeof(arr))
3 #define M 10007
4 #define N 2007
5 using namespace std;
6 const int inf = 0x7f7f7f7f;
7 const int mod = 1000000007;
8 struct node
9 {
10 int v;
11 int next;
12 } g[M];
13 int head[N],ct;
14 bool ok[N][N];
15 bool vt[N];
16 int n,m;
17
18 void add(int u,int v)
19 {
20 g[ct].v = v;
21 g[ct].next = head[u];
22 head[u] = ct++;
23 }
24 void bfs(int s)
25 {
26 int i;
27 for (i = 1; i <= n; ++i) vt[i] = false;
28 vt[s] = true;
29 queue<int> Q;
30 Q.push(s);
31 while (!Q.empty())
32 {
33 int u = Q.front();
34 Q.pop();
35 for (i = head[u]; i != -1; i = g[i].next)
36 {
37 int v = g[i].v;
38 if (!vt[v])
39 {
40 vt[v] = true;
41 ok[s][v] = true;
42 Q.push(v);
43 }
44 }
45 }
46 }
47 int main()
48 {
49 // Read();
50 int T;
51 int i,j;
52 scanf("%d",&T);
53 int cas = 1;
54 while (T--)
55 {
56 scanf("%d%d",&n,&m);
57 CL(head,-1);
58 ct = 0;
59 int x,y;
60 for (i = 0; i < m; ++i)
61 {
62 scanf("%d%d",&x,&y);
63 add(x,y);
64 }
65 CL(ok,false);
66 for (i = 1; i <= n; ++i) ok[i][i] = true;
67 for (i = 1; i <= n; ++i) bfs(i);
68 bool flag = false;
69 for (i = 1; i <= n && !flag; ++i)
70 {
71 for (j = 1; j <= n && !flag; ++j)
72 {
73 if (ok[i][j] || ok[j][i]) continue;
74 else
75 {
76 flag = true;
77 break;
78 }
79 }
80 }
81 if (!flag) printf("Case %d: Kalimdor is just ahead
",cas++);
82 else printf("Case %d: The Burning Shadow consume us all
",cas++);
83 }
84 return 0;
85 }