Codeforces 960F - Pathwalks

960F - Pathwalks

思路:

ORZ 杜老师

用map写1e5个树状数组,骚操作

记Q为query和update次数,则节点个数约为Q*log(N)

代码:

#include<bits/stdc++.h>
using namespace std;
#define LL long long 
#define pb push_back
#define mem(a, b) memset(a, b, sizeof(a))

const int N = 1e5 + 5;
map<int, int> bits[N];
int query(int u, int x){
    int ans = 0;
    while (x) {
        ans = max(ans, bits[u][x]);
        x -= x&-x;
    }
    return ans;
}
void update(int u, int x, int v) {
    while(x < N){
        bits[u][x] = max(bits[u][x], v);
        x += x&-x;
    }
}
int main() {
    int n, m, u, v, w;
    scanf("%d%d", &n, &m);
    for (int i = 0; i < m; i++) {
        scanf("%d%d%d", &u, &v, &w);
        w++; 
        update(v, w, query(u, w - 1) + 1);
    }
    int ans = 0;
    for (int i = 1; i <= n; i++) {
        ans = max(ans, query(i, 100001));
    }
    printf("%d\n", ans);
    return 0;
}

 

posted @ 2018-04-08 22:15  Wisdom+.+  阅读(296)  评论(0编辑  收藏  举报