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 = ;
const int M = ; 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] = ;
for( int i = eh[u] ; ~i ; i = nxt[i] ) {
int v = et[i] ; if( v == pre ) continue ;
dfs1( v , u , d + ) ;
num[u] += num[v] ;
if( son[u] == - || 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] == - ) 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 = ; pos = ;
memset( eh , - , sizeof eh ) ;
memset( son , - , 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] ] += ;
val[ p[u] + ] -= ;
u=fa[f1];
f1=top[u];
}
if( dep[u] > dep[v] ) swap(u,v);
val[ p[ son[u] ] ] += ;
val[ p[v] + ] -= ;
} typedef pair<int,int> pii ;
#define X first
#define Y second
vector<pii>Q; int Run() {
while( cin >> n >> m ) {
memset( val , , sizeof val ) ;
init() ; Q.clear() ;
int tt = ;
while( m-- ) {
int u , v , c ; cin >> u >> v >> c ;
if( c ) {
addedge( u , v ) ;
} else {
Q.push_back( pii(u,v) ) ;
tt++ ;
}
}
dfs1( , , ) , dfs2( , ) ;
for( int i = ; i < Q.size() ; ++i ) {
Change( Q[i].X , Q[i].Y ) ;
}
int ans = , t = val[] ;
for( int i = ; i <= n ; ++i ) {
t += val[i] ;
if( t == ) ans += tt ;
else if( t == ) ans++ ;
}
cout << ans << endl ;
}
return ;
} int main() {
ios::sync_with_stdio();
return Run();
}

ACdream 1424 Diversion( 树链剖分 )的更多相关文章

  1. HDU 5452——Minimum Cut——————【树链剖分+差分前缀和】ACdream 1429——Diversion——————【树链剖分】

    Minimum Cut Time Limit: 3000/2000 MS (Java/Others)    Memory Limit: 65535/102400 K (Java/Others)Tota ...

  2. ACdream 1103 瑶瑶正式成为CEO(树链剖分+费用流)

    Problem Description 瑶瑶(tsyao)是某知名货运公司(顺丰)的老板,这个公司很大,货物运输量极大,因此公司修建了许多交通设施,掌控了一个国家的交通运输. 这个国家有n座城市,公司 ...

  3. BZOJ 2157: 旅游( 树链剖分 )

    树链剖分.. 样例太大了根本没法调...顺便把数据生成器放上来 -------------------------------------------------------------------- ...

  4. BZOJ 3626: [LNOI2014]LCA [树链剖分 离线|主席树]

    3626: [LNOI2014]LCA Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 2050  Solved: 817[Submit][Status ...

  5. BZOJ 1984: 月下“毛景树” [树链剖分 边权]

    1984: 月下“毛景树” Time Limit: 20 Sec  Memory Limit: 64 MBSubmit: 1728  Solved: 531[Submit][Status][Discu ...

  6. codevs 1228 苹果树 树链剖分讲解

    题目:codevs 1228 苹果树 链接:http://codevs.cn/problem/1228/ 看了这么多树链剖分的解释,几个小时后总算把树链剖分弄懂了. 树链剖分的功能:快速修改,查询树上 ...

  7. 并查集+树链剖分+线段树 HDOJ 5458 Stability(稳定性)

    题目链接 题意: 有n个点m条边的无向图,有环还有重边,a到b的稳定性的定义是有多少条边,单独删去会使a和b不连通.有两种操作: 1. 删去a到b的一条边 2. 询问a到b的稳定性 思路: 首先删边考 ...

  8. 树链剖分+线段树 CF 593D Happy Tree Party(快乐树聚会)

    题目链接 题意: 有n个点的一棵树,两种操作: 1. a到b的路径上,给一个y,对于路径上每一条边,进行操作,问最后的y: 2. 修改某个条边p的值为c 思路: 链上操作的问题,想树链剖分和LCT,对 ...

  9. 树链剖分+线段树 HDOJ 4897 Little Devil I(小恶魔)

    题目链接 题意: 给定一棵树,每条边有黑白两种颜色,初始都是白色,现在有三种操作: 1 u v:u到v路径(最短)上的边都取成相反的颜色 2 u v:u到v路径上相邻的边都取成相反的颜色(相邻即仅有一 ...

随机推荐

  1. 关于 ATL 中 CComControl 的构造

    分享一篇 C++语言 & ATL 的高阶解读笔记,你需要在C++语言特性中上串下跳,应该算篇有质量的文章. class ATL_NO_VTABLE CHello : // ... public ...

  2. java+struts上传文件夹文件

    这里只写后端的代码,基本的思想就是,前端将文件分片,然后每次访问上传接口的时候,向后端传入参数:当前为第几块文件,和分片总数 下面直接贴代码吧,一些难懂的我大部分都加上注释了: 上传文件实体类: 看得 ...

  3. 杨辉三角 x

    杨辉三角是美丽的数学结晶,其结论往往多蕴含自然之美. ——以下内容均摘抄自题解. 例题: 洛谷P1762  偶数 正如这题所示,数据在n<=10^15的范围内则引导我们去寻找空间更节省,速率更高 ...

  4. Netty 介绍和应用场景(一)

    1.为什么选择Netty 需要了解了Socket通信(IO/NIO/AIO)编程,对于通信模型已经有了一个基本的认识.,果想把这些真正的用于实际工作中,那么还需要不断的完善.扩展和优化.比如经典的TC ...

  5. 使用Eigen遇到恶心报错

    参考博客:https://www.cnblogs.com/wongyi/p/8734346.html 1. 数据类型报错 /home/wy/workdir/slambook/ch3/useEigen/ ...

  6. 当在本地磁盘服务(Windows)中无法删除指定分区时的解决方案

    有时候,我们在使用Windows管理磁盘分区时,可能会出现部分分区无法删除的情况,也就是说右键快捷菜单中没有删除卷的操作项. 此时,我们可以按照如下的步骤进行操作即可完成: Step 1: 以管理员身 ...

  7. python3.x使用cxfreeze将.p打包成.exe

    之前写了一个使用ffplay批量查看格式为h264的图片,每次抽帧后都要打开pycharm编译器来运行程序,然后才能正常查看图片,或者在其他没有安装python环境的电脑中运行,很不方便.为此,在网上 ...

  8. 听说你还不理解JavaScript闭包

    闭包(Closure) 闭包是一个函数和词法环境的组合,函数声明在这个词法环境中 词法作用域 看下面一个例子 function init() { var name = 'Mozilla'; // na ...

  9. leetcode-mid- math-166. Fraction to Recurring Decimal

    mycode   73.92% 如何判断同号? 1)res = "-" if ((numerator>0) ^ (denominator>0)) else " ...

  10. Python 字符串与列表去重

    最近面试中出现频率比较高的字符串和列表的去重pstr = 'abcadcf'# 字符串去重# 1.使用集合 --没有保持原来的顺序 print(set(pstr)) # 2.使用字典 -- 没有保持原 ...