ACdream 1424 Diversion( 树链剖分 )
                                                            生活随笔
收集整理的這篇文章主要介紹了
                                ACdream 1424 Diversion( 树链剖分 )
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.                        
                                Diversion
Time Limit: 2000/1000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others)
Submit Statistic Next Problem
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!
? 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( 树链剖分 )的全部內容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: 抽取JDBC工具类
- 下一篇: std::wcout输出1遍不输出
