Problem:
MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }});

Problem H
Animal Companion in Maze

George, your pet monkey, has escaped, slipping the leash!

George is hopping around in a maze-like building with many rooms. The doors of the rooms, if any, lead directly to an adjacent room, not through corridors. Some of the doors, however, are one-way: they can be opened only from one of their two sides.

He repeats randomly picking a door he can open and moving to the room through it. You are chasing him but he is so quick that you cannot catch him easily. He never returns immediately to the room he just has come from through the same door, believing that you are behind him. If any other doors lead to the room he has just left, however, he may pick that door and go back.

If he cannot open any doors except one through which he came from, voila, you can catch him there eventually.

You know how rooms of the building are connected with doors, but you don't know in which room George currently is.

It takes one unit of time for George to move to an adjacent room through a door.

Write a program that computes how long it may take before George will be confined in a room. You have to find the longest time, considering all the possibilities of the room George is in initially, and all the possibilities of his choices of doors to go through.

Note that, depending on the room organization, George may have possibilities to continue hopping around forever without being caught.

Doors may be on the ceilings or the floors of rooms; the connection of the rooms may not be drawn as a planar graph.

Input

The input consists of a single test case, in the following format.

$n$ $m$

$x_1$ $y_1$ $w_1$

.

.

.

$x_m$ $y_m$ $w_m$

The first line contains two integers $n$ ($2 \leq n \leq 100000$) and $m$ ($1 \leq m \leq 100000$), the number of rooms and doors, respectively. Next $m$ lines contain the information of doors. The $i$-th line of these contains three integers $x_i$, $y_i$ and $w_i$ ($1 \leq x_i \leq n, 1 \leq y_i \leq n, x_i \ne y_i, w_i = 1$ or $2$), meaning that the $i$-th door connects two rooms numbered $x_i$ and $y_i$, and it is one-way from $x_i$ to $y_i$ if $w_i = 1$, two-way if $w_i = 2$.

Output

Output the maximum number of time units after which George will be confined in a room. If George has possibilities to continue hopping around forever, output "Infinite".

Sample Input 1

2 1
1 2 2

Sample Output 1

1

Sample Input 2

2 2
1 2 1
2 1 1

Sample Output 2

Infinite

Sample Input 3

6 7
1 3 2
3 2 1
3 5 1
3 6 2
4 3 1
4 6 1
5 2 1

Sample Output 3

4

Sample Input 4

3 2
1 3 1
1 3 1

Sample Output 4

1

Solution in C++11 (judge verdict: Accepted)

#include<bits/stdc++.h>
using namespace std;

#define int long long

#define rep(i,n) for(int i=0;i<(n);i++)
#define pb push_back
#define all(v) (v).begin(),(v).end()
#define fi first
#define se second
typedef vector<int>vint;
typedef pair<int,int>pint;
typedef vector<pint>vpint;

template<typename A,typename B>inline void chmin(A &a,B b){if(a>b)a=b;}
template<typename A,typename B>inline void chmax(A &a,B b){if(a<b)a=b;}

int N,M;

struct UnionFindTree{
    vector<int>par,sz;
    UnionFindTree(int n){
        par.resize(n);
        sz.resize(n);
        for(int i=0;i<n;i++){
            par[i]=i;
            sz[i]=1;
        }
    }
    int find(int x){
        return x==par[x]?x:par[x]=find(par[x]);
    }
    void unite(int x,int y){
        x=find(x);y=find(y);
        if(x==y)return;
        if(sz[x]<sz[y])swap(x,y);
        sz[x]+=sz[y];
        par[y]=x;
    }
    bool areSame(int x,int y){
        return find(x)==find(y);
    }
    int size(int x){
        return sz[find(x)];
    }
};
UnionFindTree uf(0);

int dp[111111],dp2[111111];
vint G[111111];
void dfs(int v,int p,int r){
    for(auto u:G[v]){
        if(u==p)continue;
        if(uf.find(u)!=r){
            chmax(dp2[v],dp[u]+1);
        }
        else{
            dfs(u,v,r);
            chmax(dp2[v],dp2[u]+1);
        }
    }
}

void dfs2(int v,int p,int r,int x){
    vint lis={x};
    for(auto u:G[v]){
        if(u==p)continue;
        if(uf.find(u)==r){
            lis.pb(dp2[u]+1);
        }
        else{
            lis.pb(dp[u]+1);
        }
    }
    sort(all(lis));reverse(all(lis));
    dp[v]=lis[0];

    for(auto u:G[v]){
        if(u==p||uf.find(u)!=r)continue;
        int xx;
        if(lis[0]==dp2[u]+1)xx=lis[1];
        else xx=lis[0];

        dfs2(u,v,r,xx+1);
    }
}

signed main(){
    cin>>N>>M;
    vpint es,es2;

    rep(i,M){
        int a,b,w;
        cin>>a>>b>>w;
        a--;b--;
        if(w==1)es.pb({a,b});
        else es2.pb({a,b});
    }

    uf=UnionFindTree(N);
    for(auto &p:es2){
        if(uf.areSame(p.fi,p.se)){
            cout<<"Infinite"<<endl;
            return 0;
        }
        uf.unite(p.fi,p.se);
    }


    vector<vint>D(N);
    vint deg(N);
    for(auto &p:es){
        D[uf.find(p.fi)].pb(uf.find(p.se));
        deg[uf.find(p.se)]++;
    }


    vint ord;
    rep(i,N)if(uf.find(i)==i&&deg[i]==0)ord.pb(i);
    rep(i,ord.size()){
        int v=ord[i];
        for(auto u:D[v]){
            if(--deg[u]==0)ord.pb(u);
        }
    }

    if(count(all(deg),0)!=N){
        cout<<"Infinite"<<endl;
        return 0;
    }


    for(auto &p:es)G[p.fi].pb(p.se);
    for(auto &p:es2)G[p.fi].pb(p.se),G[p.se].pb(p.fi);

    for(int i=(int)ord.size()-1;i>=0;i--){
        dfs(ord[i],-1,ord[i]);
        dfs2(ord[i],-1,ord[i],0);
    }

    cout<<*max_element(dp,dp+N)<<endl;
    return 0;
}