Algo-Trian
This commit is contained in:
61
Algorithm/DP-DynamicProgramming/P3916 图的遍历.cpp
Normal file
61
Algorithm/DP-DynamicProgramming/P3916 图的遍历.cpp
Normal file
@@ -0,0 +1,61 @@
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
using namespace std;
|
||||
int main(){
|
||||
//dpֻ<70><D6BB>O(n3)<29><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>bfs<66><73><EFBFBD><EFBFBD>dfs
|
||||
int v, e;
|
||||
cin >> v >> e;
|
||||
//vector<vector<int>> adj(v, vector<int>());
|
||||
vector<vector<bool>> dp(v, vector<bool>(v, false));
|
||||
for(int i = 0; i < e; i++){
|
||||
int a, b;
|
||||
cin >> a >> b;
|
||||
//adj[a - 1].push_back(b - 1);
|
||||
//adj[b - 1].push_back(a - 1);
|
||||
dp[a - 1][b - 1] = true;
|
||||
}
|
||||
//<2F><><EFBFBD><EFBFBD>dp[i][j] : <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD>ͨ<EFBFBD><CDA8><EFBFBD><EFBFBD>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD>k<EFBFBD>ķ<EFBFBD>ʽ<EFBFBD><CABD><EFBFBD><EFBFBD>
|
||||
for (int i = 0; i < v; i++) dp[i][i] = true; // <20>Լ<EFBFBD><D4BC><EFBFBD><EFBFBD>Լ<EFBFBD><D4BC><EFBFBD>ͨ
|
||||
for(int k = 0; k < v; k++){
|
||||
for(int i = 0; i < v; i++){
|
||||
for(int j = 0; j < v; j++){
|
||||
if(dp[i][j]) continue;
|
||||
dp[i][j] = dp[i][j] || (dp[i][k] & dp[k][j]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//bfs
|
||||
for(int i = 0; i < v; i++) {
|
||||
vector<bool> visited(v, false);
|
||||
queue<int> q;
|
||||
q.push(i);
|
||||
visited[i] = true;
|
||||
int mx = i;
|
||||
while (!q.empty()) {
|
||||
int curr = q.front();
|
||||
q.pop();
|
||||
for (int next : adj[curr]) {
|
||||
if (!visited[next]) {
|
||||
visited[next] = true;
|
||||
q.push(next);
|
||||
mx = max(mx, next);
|
||||
}
|
||||
}
|
||||
}
|
||||
cout << mx + 1 << " ";
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
for(int i = 0; i < v; i++){
|
||||
int mx = -1;
|
||||
for(int j = 0; j < v; j++){
|
||||
if(dp[i][j] && mx < j) mx = j;
|
||||
}
|
||||
cout << mx + 1 << " ";
|
||||
}
|
||||
cout << endl;
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user