zoukankan      html  css  js  c++  java
  • [POJ1985] Cow Marathon 「树的直径」

    >传送门<

    题意:求树的直径

    思路:就是道模板题,两遍dfs就求出来了

    Code

    #include <cstdio>
    #include <iostream>
    #include <algorithm>
    #include <vector>
    using namespace std;
    typedef pair<int, int> P;
    
    const int MAX_N = 1000005;
    vector<P> G[MAX_N];
    int n, m, s, ans;
    int u, v, val;
    char ch;
    
    void dfs(int u, int pre, int step)
    {
        if (ans<step) ans = step, s = u;
        for (int i = 0; i < G[u].size(); i++) {
            int v = G[u][i].first, val = G[u][i].second;
            if (v!=pre) dfs(v, u, step+val);
        }
    }
    int main() 
    {
        scanf("%d%d", &n, &m);
        for (int i = 1; i <= m; i++) {
            scanf("%d %d %d %c", &u, &v, &val, &ch);
            G[u].push_back(P(v, val));
            G[v].push_back(P(u,val));
        }
        dfs(1, 0, 0);
        dfs(s, 0, 0);
        printf("%d", ans);
    }
  • 相关阅读:
    模拟--北京标准时间
    DOM方法
    Document-对象属性和常用的对象方法
    struts2标签
    OGNL
    Java基础方面
    初识拦截器
    访问者模式
    备忘录模式
    门面模式
  • 原文地址:https://www.cnblogs.com/wizarderror/p/11269758.html
Copyright © 2011-2022 走看看