zoukankan      html  css  js  c++  java
  • cf459E Pashmak and Graph

    E. Pashmak and Graph
    time limit per test
    1 second
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    Pashmak's homework is a problem about graphs. Although he always tries to do his homework completely, he can't solve this problem. As you know, he's really weak at graph theory; so try to help him in solving the problem.

    You are given a weighted directed graph with n vertices and m edges. You need to find a path (perhaps, non-simple) with maximum number of edges, such that the weights of the edges increase along the path. In other words, each edge of the path must have strictly greater weight than the previous edge in the path.

    Help Pashmak, print the number of edges in the required path.

    Input

    The first line contains two integers nm (2 ≤ n ≤ 3·105; 1 ≤ m ≤ min(n·(n - 1), 3·105)). Then, m lines follows. The i-th line contains three space separated integers: uiviwi (1 ≤ ui, vi ≤ n; 1 ≤ wi ≤ 105) which indicates that there's a directed edge with weight wi from vertex ui to vertex vi.

    It's guaranteed that the graph doesn't contain self-loops and multiple edges.

    Output

    Print a single integer — the answer to the problem.

    Sample test(s)
    input
    3 3
    1 2 1
    2 3 1
    3 1 1
    
    output
    1
    
    input
    3 3
    1 2 1
    2 3 2
    3 1 3
    
    output
    3
    
    input
    6 7
    1 2 1
    3 2 5
    2 4 2
    2 5 2
    2 6 9
    5 4 3
    4 3 4
    
    output
    6
    
    Note

    In the first sample the maximum trail can be any of this trails: .

    In the second sample the maximum trail is .

    In the third sample the maximum trail is .

    题意是给定n个点m条边,求一条最长路径,使得路径上的边的权值严格递增

    这题原来是ccr给的代码,但是实际上还是挺水的……我觉得最难的是A和C啊QAQ

    先把边按权值排序,然后令f[i]表示以i结尾的路径的最大长度

    然后一条一条加进去就好了

    以下ccr的代码

    #include<bits/stdtr1c++.h>
    #include<algorithm>
    using namespace std;
    typedef long long ll;
    typedef pair<int, int> pi;
    typedef double ld;
    typedef struct edge{
        int f, t, v;
        bool operator<(const edge& e) const{
            return v < e.v;
        }
    }edge;
    const int N = 3e5 + 50;
    int ans[N];
    vector< pi > w;
    int main() {
        int n, m, a, b, c;
        vector< edge > v;
        cin >> n >> m;
        for(int i = 0; i < m; i++){
            cin >> a >> b >> c;
            edge e = {a, b, c};
            v.push_back(e);
        }
        sort(v.begin(), v.end());
    
        int i = 0, j;
        while( i < v.size()){
            j = i;
            while(j < v.size() && v[j].v == v[i].v){
                if(ans[v[j].f] + 1 > ans[v[j].t]) w.push_back(pi(v[j].t, ans[v[j].f] + 1));
                j++;
            }
            for(int q = 0; q < w.size(); q++){
                ans[w[q].first] = max(ans[w[q].first], w[q].second);
            }
            i = j;
            w.clear();
        }
        cout << *max_element(ans, ans + N) << endl;
    }
    ——by zhber,转载请注明来源
  • 相关阅读:
    编译FreePascal和Lazarus
    QTreeView使用点点滴滴
    刨根问底儿 -- intVal($str) 跟 (int) $str 的运算结果有什么区别
    Qt源代码分析
    QString够绕的,分为存储(编译器)和解码(运行期),还有VS编译器的自作主张,还有QT5的变化
    C++静态变量本身可否是一个实例对象
    QT4.86写中文XML
    点击TButton后的执行OnClick和OnMouseDown两个事件的过程(其实是通过WM_COMMAND执行程序员的代码)
    Hibernate3.0中的session.find()问题
    曲线控件我一直用codeproject上的那几个(C++ 100款开源界面库)
  • 原文地址:https://www.cnblogs.com/zhber/p/4035916.html
Copyright © 2011-2022 走看看