你好, 我是個圖論新手, 寫這題時, 我用 DFS 走訪, 最後檢查目標 t, 是否被 visited.
因為是連續輸入,每次輸入我都有把資料清空,但是還是 NA 99%. 求大神指導
結果:
Accept
Accept
您的答案為: No!!! 正確答案為: Yes!!!
Code:
#include <bits/stdc++.h> using namespace std; #define N 850 int n, m, visited[N], adj[N][N], f, t; //from, to void dfs(int start) { visited[start] = 1; for (int i = 0; i < n; i++) { if (!visited[i] and adj[start][i] == 1) dfs(i); } } int main() { ios_base::sync_with_stdio(false); cin.tie(0); while (cin >> n >> m) { //reset adj for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { adj[i][j] = 0; //set all to false } } //reset visited for (int i = 0; i < n; i++) { visited[i] = 0; //false; } for (int i = 0; i < m; i++) { int from, to; cin >> from >> to; adj[from][to] = 1; //unweighted. } cin >> f >> t; dfs(f); visited[t] ? cout << "Yes!!!\n" : cout << "No!!!\n"; } }
你好, 我是個圖論新手, 寫這題時, 我用 DFS 走訪, 最後檢查目標 t, 是否被 visited.
因為是連續輸入,每次輸入我都有把資料清空,但是還是 NA 99%. 求大神指導
結果:
#0: 1% AC (2ms, 364KB)
Accept#1: 98% AC (0.2s, 2.5MB)
Accept#2: 1% WA (line:3)
您的答案為: No!!! 正確答案為: Yes!!!
Code:
#include <bits/stdc++.h> using namespace std; #define N 850 int n, m, visited[N], adj[N][N], f, t; //from, to void dfs(int start) { visited[start] = 1; for (int i = 0; i < n; i++) { if (!visited[i] and adj[start][i] == 1) dfs(i); } } int main() { ios_base::sync_with_stdio(false); cin.tie(0); while (cin >> n >> m) { //reset adj for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { adj[i][j] = 0; //set all to false } } //reset visited for (int i = 0; i < n; i++) { visited[i] = 0; //false; } for (int i = 0; i < m; i++) { int from, to; cin >> from >> to; adj[from][to] = 1; //unweighted. } cin >> f >> t; dfs(f); visited[t] ? cout << "Yes!!!\n" : cout << "No!!!\n"; } }
找到錯誤了!!!!! 少加了一個致命的 '='