c++ - What is the complexity of that code? -
i looking @ code on link:
http://codeforces.com/contest/459/submission/7495216
code given directed weighted graph , wanna find longest path strictly increasing edges.
can me?
#include <iostream> #include <vector> using namespace std; const int max = 300005; vector<pair<int, int> > w[max]; int dp[max], tmp[max]; int main() { ios::sync_with_stdio(false); int n, m; cin >> n >> m; (int = 0; < m; i++) { int u, v, len; cin >> u >> v >> len; w[len].push_back(make_pair(u, v)); } (int = 0; < max; i++) { (int j = 0; j < w[i].size(); j++) { int u = w[i][j].first, v = w[i][j].second; tmp[v] = 0; } (int j = 0; j < w[i].size(); j++) { int u = w[i][j].first, v = w[i][j].second; tmp[v] = max(tmp[v], dp[u] + 1); } (int j = 0; j < w[i].size(); j++) { int u = w[i][j].first, v = w[i][j].second; dp[v] = max(dp[v], tmp[v]); } } int ans = 0; (int = 1; <= n; i++) ans = max(ans, dp[i]); cout << ans << endl; return 0; }
for each node code iterates on outgoing edges. runtime complexity o(n*m) n number of nodes , m number edges.
edit: after closer @ data , code, complexity o(n+m). first , second loop iterate on edges, o(m), , third loop iterates on nodes, o(n).
Comments
Post a Comment