ACdream 1424 Diversion( 树链剖分 )


Diversion

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

Problem Description

The kingdom of Farland has n cities connected by m bidirectional roads. Some of the roads are pavedwith stone, and others are just country roads. The capital of the kingdom is the city number 1. Theroads are designed in such a way that it is possible to get from any city to any other using only roadspaved with stone, and the number of stone roads is minimal possible. The country roads were designedin such a way that if any stone road is blocked or destroyed it is still possible to get from any city to anyother 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 werecreated 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 destroyingsome roads. Calculations show that the resources he has are enough to destroy one stone road and onecountry road. The king would like to destroy such roads that after it there were at least two cities inFarland 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 theminister 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 0for 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

优质内容筛选与推荐>>
1、SQL Server中将多行数据拼接为一行数据(一个字符串)
2、Access Helper 类似sqlhelper的好东东
3、codeforces 887A Div. 64 思维 模拟
4、Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
5、多线程


长按二维码向我转账

受苹果公司新规定影响,微信 iOS 版的赞赏功能被关闭,可通过二维码转账支持公众号。

    阅读
    好看
    已推荐到看一看
    你的朋友可以在“发现”-“看一看”看到你认为好看的文章。
    已取消,“好看”想法已同步删除
    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号





    联系我们

    欢迎来到TinyMind。

    关于TinyMind的内容或商务合作、网站建议,举报不良信息等均可联系我们。

    TinyMind客服邮箱:support@tinymind.net.cn