【树直径】 POJ 1985 Cow Marathon

树直径模板

用到了vectpr中捆绑pair : https://blog.csdn.net/computer055maxi/article/details/6129736

#include <iostream>
#include <algorithm>
#include <queue>
#include <cstring>
#pragma GCC optimize(2)
using namespace std;
#define maxn 50005
#define inf 1e18
//#define mod 1e9+7
typedef long long ll;
const ll mod = 1e9+7;
ll n,m,ans,flag[maxn],point;
vector < pair<ll,ll> > arr[maxn];
void slove(ll now,ll step)
{
    if(flag[now] == 1)
        return ;
    else
        flag[now] = 1;

    if(step > ans)
    {
        ans = step;
        point = now;
    }


    for(ll i = 0; i < arr[now].size(); i++)
    {
        slove(arr[now][i].first,arr[now][i].second+step);
    }

    return ;
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);

    cin >> n >> m;

    for(ll i = 0; i < m; i++)
    {
        ll a,b,c;
        char s;
        cin >> a >> b >> c >> s;
        arr[a].push_back( make_pair<ll,ll>(b,c)  );
        arr[b].push_back( make_pair<ll,ll>(a,c)  );
    }

    slove(1,0);

    ans = 0;
    memset(flag,0,sizeof(flag));

    slove(point,0);

    cout << ans << endl;

    return 0;
}

猜你喜欢

转载自blog.csdn.net/Whyckck/article/details/82586180