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. 配置中心Apollo多环境部署

  2. node.js入门学习(六)--express

    1.官网:http://expressjs.com/ 中文:http://www.expressjs.com.cn/ 2.HelloWorld 1)mkdir node-express-demo 2) ...

  3. BZOJ 4011: [HNOI2015]落忆枫音 计数 + 拓扑排序

    Description 「恒逸,你相信灵魂的存在吗?」 郭恒逸和姚枫茜漫步在枫音乡的街道上.望着漫天飞舞的红枫,枫茜突然问出 这样一个问题.  「相信吧.不然我们是什么,一团肉吗?要不是有灵魂……我们 ...

  4. UNIX标准C - 进程之间的通信

    一.基本概念 进程间通信IPC:进程之间交换数据的过程叫进程间通信 进程间同性的方式: 简单的进程间的通信: 命令行:父进程通过exec函数创建子进程是可以附加一些数据 环境变量表:父进程通过exec ...

  5. Android 5种Toast特效

    Toast是Android中用来显示显示信息的一种机制,和Dialog不一样的是,Toast是没有焦点的,而且Toast显示的时间有限,过一定的时间就会自动消失.   1.默认效果: Toast.ma ...

  6. Vim 命令、操作、快捷键(收藏大全)

    ------ 命令历史 以:和/开头的命令都有历史纪录,可以首先键入:或/然后按上下箭头来选择某个历史命令. 启动vim 在命令行窗口中输入以下命令即可 vim 直接启动vim vim filenam ...

  7. Linux小记 -- [已解决]Failed to connect to https://changelogs.ubuntu.com/meta-release-lts. Check your Internet connection or proxy settings

    问题描述 操作系统:Ubuntu Server 18.04 LTS Ubuntu每次启动时产生如下motd(message of today)输出 Failed to connect to https ...

  8. jsp四种属性范围

    在JSP提供了四种属性的保存范围.所谓的属性保存范围,指的就是一个设置的对象,可以在多个页面中保存并可以继续使用.它们分别是:page.request.session.appliction. 1.pa ...

  9. 将数据库中带出的列,在gridview中影藏起来

    前台增加事件:OnRowCreated="GridView1_RowCreated" protected void GridView1_RowCreated(object send ...

  10. 关于R语言中set.seed()

    在r中取sample时候,经常会有set.seed(某数),经常看见取值很大,其实这里无论括号里取值是多少,想要上下两次取值一样,都需要在每次取值前输入同样的set.seed(某数),才能保证两次取值 ...