Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
dqx-1005 authored Sep 8, 2023
1 parent 332b493 commit 848e22c
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions 图论/拓扑排序/dfs式拓扑排序.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
//luogu P1113
//dfs式拓扑排序

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e4+5;
int t[N],f[N]; //时间和答案
vector<int>edge[N];

int dfs(int x) {
if(f[x])return f[x]; //已经被访问过
for(int i=0; i<edge[x].size(); i++) { //循环:x的每条出边指向的点
f[x] = max(f[x],dfs(edge[x][i])); //dp,求pre最大值
};
f[x]+=t[x];
return f[x];
}

int main() {
int n;
cin>>n;
for(int i=1; i<=n; i++) {
int x,y;
cin>>x>>t[i];
cin>>y;
while(y) {
edge[y].push_back(x); //y->x
cin>>y;
}
};
int ans = 0;
for(int i=1; i<=n; i++)ans = max(ans,dfs(i));
cout<<ans;
return 0;
}

0 comments on commit 848e22c

Please sign in to comment.