ACdream 1424 Diversion( 树链剖分 )

Diversion

Time Limit: 2000/1000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others)

Problem Description

      The kingdom of Farland has n cities connected by m bidirectional roads. Some of the roads are paved with stone, and others are just country roads. The capital of the kingdom is the city number 1. The roads are designed in such a way that it is possible to get from any city to any other using only roads paved with stone, and the number of stone roads is minimal possible. The country roads were designed in such a way that if any stone road is blocked or destroyed it is still possible to get from any city to any other by roads.
​      Let us denote the number of stone roads needed to get from city u to city v as s(u, v). The roads were created long ago and follow the strange rule: if two cities u and v are connected by a road (no matter,stone or country), then either s(1, u) + s(u, v) = s(1, v ) or s(1, v ) + s(v, u) = s(1, u).
​      The king of Edgeland is planning to attack Farland. He is planning to start his operation by destroying some roads. Calculations show that the resources he has are enough to destroy one stone road and one country road. The king would like to destroy such roads that after it there were at least two cities in Farland not connected by roads any more.
​      Now he asks his minister of defense to count the number of ways he can organize the diversion. But the minister can only attack or defend, he cannot count. Help him!

Input

      The first line of the input file contains n and m — the number of cities and roads respectively (3 ≤ n ≤ 20 000, m ≤ 100 000). The following m lines describe roads, each line contains three integer numbers — the numbers of cities connected by the corresponding road, and 1 for a stone road or 0 for a country road. No two cities are connected by more than one road, no road connects a city to itself.

Output

      Output one integer number — the number of ways to organize the diversion.

Sample Input

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

Sample Output

4

题意 : 给出两种边 0 , 1 , 1是可以构成树的 。问删除0 , 1 边各一条 , 能否把图分割开 。

先对 边1构成的树进行树剖 , 再看看有多少条 0 边经过 某条 1 边的路径上 , 没有的话可以任意选 0 边, 有的话只能有1条0边, 答案更新1 

#include <bits/stdc++.h>
using namespace std ;
const int N = 20010;
const int M = 400010 ;

int n , m ;
int eh[N] , et[M] , nxt[M] , tot ;
int top[N] , fa[N] , dep[N] , num[N] , p[N] , fp[N] , son[N] ;
int pos ;

void addedge( int u , int v ) {
    et[tot] = v , nxt[tot] = eh[u] , eh[u] = tot++ ;
    et[tot] = u , nxt[tot] = eh[v] , eh[v] = tot++ ;
}

void dfs1( int u , int pre , int d ) {
    dep[u] = d ;
    fa[u] = pre ;
    num[u] = 1 ;
    for( int i = eh[u] ; ~i ; i = nxt[i] ) {
        int v = et[i] ; if( v == pre ) continue ;
        dfs1( v , u , d + 1 ) ;
        num[u] += num[v] ;
        if( son[u] == -1 || num[v] > num[ son[u] ] ) son[u] = v ;
    }
}

void dfs2( int u , int sp ) {
    top[u] = sp ;
    p[u] = pos++ ;
    fp[ p[u] ] = u ;
    if( son[u] == -1 ) return ;
    dfs2( son[u] , sp ) ;
    for( int i = eh[u] ; ~i ; i = nxt[i] ) {
        int v = et[i] ; if( v == son[u] || v == fa[u] ) continue ;
        dfs2(v,v) ;
    }
}

void init() {
    tot = 0 ; pos = 1 ;
    memset( eh , -1 , sizeof eh ) ;
    memset( son , -1 , sizeof son ) ;
}

int val[N] ;

void Change( int u , int v ) {
    int f1 = top[u] , f2 = top[v] ;
    while( f1 != f2 ) {
        if( dep[f1] < dep[f2] ){
            swap(f1,f2);
            swap(u,v);
        }
        val[ p[f1] ] += 1 ;
        val[ p[u] + 1 ] -= 1 ;
        u=fa[f1];
        f1=top[u];
    }
    if( dep[u] > dep[v] ) swap(u,v);
    val[ p[ son[u] ] ] += 1 ;
    val[ p[v] + 1 ] -= 1 ;
}

typedef pair<int,int> pii ;
#define X first
#define Y second
vector<pii>Q;

int Run() {
    while( cin >> n >> m ) {
        memset( val , 0 , sizeof val ) ;
        init() ; Q.clear() ;
        int tt = 0 ;
        while( m-- ) {
            int u , v , c ; cin >> u >> v >> c ;
            if( c ) {
                addedge( u , v ) ;
            } else {
                Q.push_back( pii(u,v) ) ;
                tt++ ;
            }
        }
        dfs1( 1 , 0 , 0 ) , dfs2( 1 , 1 ) ;
        for( int i = 0 ; i < Q.size() ; ++i ) {
            Change( Q[i].X , Q[i].Y ) ;
        }
        int ans = 0 , t = val[1] ;
        for( int i = 2 ; i <= n ; ++i ) {
            t += val[i] ;
            if( t == 0 ) ans += tt ;
            else if( t == 1 ) ans++ ;
        }
        cout << ans << endl ;
    }
    return 0 ;
}

int main() {
    ios::sync_with_stdio(0);
    return Run();
}
View Code
原文地址:https://www.cnblogs.com/hlmark/p/4502173.html