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. python-魔法属性和反射

    python魔法属性和反射 #!/usr/bin/python3 # coding:utf-8 # Auther:AlphaPanda # Description:与类相关的魔法属性 # Versio ...

  2. 16. ClustrixDB Rebalancer

    管理平衡 Clustrix Rebalancer被设计成自动作为后台进程运行,以便跨集群重新平衡数据.介绍如何配置和监视rebalancer,但是大多数部署不需要用户干预. Rebalancer主要通 ...

  3. JSON.parse 测试

    第一种 报错 var t = JSON.parse(""); console.log(t); 第二种 正常 var t = JSON.parse('{"AA": ...

  4. CentOS查看进程端口号以及kill操作

    查看端口: 使用 netstat   -anp   |   grep  8090即:netstat –apn | grep  8090 查看进程:1.ps 命令用于查看当前正在运行的进程,grep 是 ...

  5. Spring Boot教程(二)关于RabbitMQ服务器整合

    准备工作 15min IDEA maven 3.0 在开始构建项目之前,机器需要安装rabbitmq,你可以去官网下载,http://www.rabbitmq.com/download.html ,如 ...

  6. js for循环中i++与++i有什么区别

    平时都是这样写的for循环, 1 2 3 for(var i = 0; i < 20 ; i++){        ....       } 但我看有的人这样写 for (var i = 0; ...

  7. HBase 集群监控系统构建

    HBase 集群监控系统构建 标签(空格分隔): Hbase 一, 集群为什么需要监控系统 总的来说是为了保证系统的稳定性,可靠性,可运维性.具体来说我认为有以下几点: 掌控集群的核心性能指标,了解集 ...

  8. pymysql 处理数据的几种方式

    1.表中提取数据 sql = "SELECT * FROM table WHERE name='%s'AND time='%s'" % (name,time)多个选择条件用AND连 ...

  9. AndroidStudio设置SVN忽略文件

    方法一: 在SVN中进行设置: 在空白处右键单击,选择TortoiseSVN -> Settings ->General:在General界面找到Global ignore pattern ...

  10. 方法二破解:Excel工作表保护密码

    最简单,复制整表,粘贴在全新的表中.但是有时候会丢失一些元素 在excel2016中实测验证过有效 第1步:在工作表菜单栏上添加[开发工具].方法是:依次单击[文件]--->[选项]---> ...