We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 332b493 commit 848e22cCopy full SHA for 848e22c
图论/拓扑排序/dfs式拓扑排序.cpp
@@ -0,0 +1,36 @@
1
+//luogu P1113
2
+//dfs式拓扑排序
3
+
4
+#include<bits/stdc++.h>
5
+using namespace std;
6
+typedef long long ll;
7
+const int N = 1e4+5;
8
+int t[N],f[N]; //时间和答案
9
+vector<int>edge[N];
10
11
+int dfs(int x) {
12
+ if(f[x])return f[x]; //已经被访问过
13
+ for(int i=0; i<edge[x].size(); i++) { //循环:x的每条出边指向的点
14
+ f[x] = max(f[x],dfs(edge[x][i])); //dp,求pre最大值
15
+ };
16
+ f[x]+=t[x];
17
+ return f[x];
18
+}
19
20
+int main() {
21
+ int n;
22
+ cin>>n;
23
+ for(int i=1; i<=n; i++) {
24
+ int x,y;
25
+ cin>>x>>t[i];
26
+ cin>>y;
27
+ while(y) {
28
+ edge[y].push_back(x); //y->x
29
30
+ }
31
32
+ int ans = 0;
33
+ for(int i=1; i<=n; i++)ans = max(ans,dfs(i));
34
+ cout<<ans;
35
+ return 0;
36
0 commit comments