首页 > 其他分享 >Educational DP Contest G - Longest Path (dfs+dp)

Educational DP Contest G - Longest Path (dfs+dp)

时间:2022-12-23 11:12:18浏览次数:54  
标签:Educational Contest int LL dfs dp

https://atcoder.jp/contests/dp/tasks/dp_g

题目大意:

给定n个点,m条有向边(确定无环),问最长路径是多少?
Sample Input 1  
4 5
1 2
1 3
3 2
2 4
3 4
Sample Output 1  
3
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<LL,LL> PII;
const LL MAXN=1e18;
const LL N=200200,M=3020;
LL n,m,maxn=0,f[N];
vector<LL> g[N];
bool st[N];
void dfs(LL idx)
{
    st[idx]=true;
    for(int i=0;i<g[idx].size();i++)
    {
        LL flag=g[idx][i];
        if(st[flag]==false) dfs(flag);
        f[idx]=max(f[idx],f[flag]+1);//dp部分
    }
}
int main()
{
    cin.tie(0); cout.tie(0); ios::sync_with_stdio(false);
    LL T=1;
    //cin>>T;
    while(T--)
    {
        cin>>n>>m;
        for(int i=1;i<=m;i++)
        {
            LL u,v;
            cin>>u>>v;
            g[u].push_back(v);//有向图
        }
        for(int i=1;i<=n;i++)
        {
            if(st[i]==false) dfs(i);
        }
        for(int i=1;i<=n;i++)
        {
            maxn=max(maxn,f[i]);
        }
        cout<<maxn<<endl;
    }
    return 0;
}

标签:Educational,Contest,int,LL,dfs,dp
From: https://www.cnblogs.com/Vivian-0918/p/17000257.html

相关文章