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. [转] 数值优化(Numerical Optimization)学习系列-目录

    from:https://blog.csdn.net/fangqingan_java/article/details/48951191 概述数值优化对于最优化问题提供了一种迭代算法思路,通过迭代逐渐接 ...

  2. linux 内存

    [转]Linux 查看内存(free buffer cache) 转自:http://elf8848.iteye.com/blog/1995638 Linux下如何查内存信息,如内存总量.已使用量.可 ...

  3. 消息队列之--RocketMQ

    序言 资料 https://github.com/alibaba/RocketMQ http://rocketmq.apache.org/

  4. UNIX环境--线程

    一.线程的概念 1.线程在进程中是负责执行代码的一个单位,可以说线程是进程的一部分.一个进程中至少要有一个主线程,进程可以拥有多个线程. 2.线程和进程一样,线程会共享进程的一些信息.比如,代码段.全 ...

  5. Mac 安装 MongoDB 数据库

    1. 使用 brew install mongodb 安装 (参见下图) 2. 安装成功如下图 (成功与否可参考 方框内字符) 3. 启动 MongoDB 数据库 3.1 先创建数据库存储目录 /da ...

  6. Springboot 构建http服务,返回的http行是'HTTP/1.1 200' 无状态码描述 客户端解析错误

    ————————————————————————————————————————— *** 响应的数据格式  HTTP/1.1 200 OK  Server: Apache-Coyote/1.1  A ...

  7. nginx实现域名跳转

    server { listen 80; server_name www.dd.com www.tt.com; index index.html index.htm index.php; root /u ...

  8. 阶段3 1.Mybatis_11.Mybatis的缓存_1 今日课程安排

  9. 《Using Databases with Python》Week3 Data Models and Relational SQL 课堂笔记

    Coursera课程<Using Databases with Python> 密歇根大学 Week3 Data Models and Relational SQL 15.4 Design ...

  10. 那些堪称神器的 Chrome 插件

    Chrome 的简洁快速以及丰富的插件种类使得它在国内日益盛行,帮助了我们很多 Chrome 用户提升了工作效率,而今天要给大家推荐8款实用甚至堪称神器的 Chrome 插件,希望对提升大家的工作效率 ...