Jimmy’s travel plan

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others)
Total Submission(s): 341    Accepted Submission(s): 58

Problem Description
Jimmy
lives in a huge kingdom which contains lots of beautiful cities. He
also loves traveling very much, and even would like to visit each city
in the country. Jaddy, his secretary, is now helping him to plan the
routes, however, Jaddy suddenly find that is quite a tough task because
it is possible for Jimmy to ask route’s information toward any city.
What was worth? Jaddy has to response for queries about the distance
information nearly between any pair of cities due to the undeterminable
starting city which Jimmy is living in when he raises a query. Because
of the large scale of the whole country, Jaddy feel hopeless to archive
such an impossible job, however, in order to gratify his manager, Jaddy
is now looking forward to your assistance.
There might be good news
about Jaddy’s work: since Jimmy is very lazy and would not like to
travel to a destination whose distance between the original city is
larger than TWO. That means only one intermediate city among the route
is acceptable (Apparently, all the connecting paths between any two
cities, if exists, have the same length as ONE). But don’t be fooled:
Jimmy also needs to know that how many alternative different routes are
available so that he can have more options. In particular two routes
were named as different if and only if there is at least one path in the
two routes is distinguishable, moreover, if more than one paths exist
between a particular pair of cities, they are considered as distinct.
 
Input
Input
has multiple test cases. The first line of the input has a single
integer T indication the number of test cases, then each test case
following. For each test case, the first line contains two integers N
and M indication the number of cities and paths in the country. Then M
lines are following, each line contains a pair of integers A and B,
separated by space, denoting an undirected path between city A and city
B, all the cities are numbered from 1 to N. Then a new line contains a
single integer Q, which means there are Q queries following. Each query
contains a couple of integers A and B which means querying the distance
and number of shortest routes between city A and B, each query occupy a
single line separately.
All the test cases are separated by a single blank line.
You can assume that N, Q <= 100000, M <= 200000.
 
Output
For
each test case, firstly output a single line contains the case number,
then Q lines for the response to queries with the same order in the
input. For each query, if there exists at least one routes with length
no longer than TWO, then output two integer separated by a single space,
the former is the distance (shortest) of routes and the later means how
many different shortest routes Jimmy can choose; otherwise, output a
single line contains “The pair of cities are not connected or too far
away.” (quotes for clarifying). See the sample data carefully for
further details.
 
Sample Input
2
5 7
1 2
2 3
3 4
4 5
2 5
2 4
1 2
4
1 4
1 2
5 3
5 4

 
2 0
2
1 1
1 2
 
Sample Output
Case #1:
2 2
1 2
2 2
1 1
Case #2:
0 1
The pair of cities are not connected or too far away.
 
给出一个无向图,问 u -> v  距离不超过2的 路径数量有多少 。
去重边,记录每个点度。
若询问中u 可直接达到v的话 直接二分出那条边。( log M )
否则要通过一个中间点 mid , 那么就选择点度比较小的点作为 u 暴力枚举mid。 然后二分 mid -> v 。
然后可以把这个询问hash起来, 当有相同的询问直接从 hashmap 拿出来 。
 
对于第2种情况的复杂度来说, 假设 u , v 的点度都达到 M / 2  。 这个询问的复杂度就 M/2*log(M) , 除外其他询问都是 Log(M)
假设点度都分得很均匀 。 都是 sqrt(M) , 那么这个询问 复杂度就是 sqrt(M) * log(M) 了
 
总的来说 复杂度就是 Q*sqrt(M)*log(M) .
 
 
#include<bits/stdc++.h>
using namespace std ;
const int N = ;
const int M = ;
const long long K = ;
int n , m ;
struct node {
int u , v ;
long long cnt ;
bool operator < ( const node &a ) const {
if( u != a.u ) return u < a.u ;
else return v < a.v ;
}
} e[M] ;
int tot ;
int in[N] ;
const int HASH = ;
struct HASHMAP {
long long key[N] , f[N] ;
int head[HASH] , next[N] , size ;
void init() {
memset( head , - , sizeof head ) ;
size = ;
}
void insert( int u , int v , long long ans ) {
long long KEY = K * u + v ;
int c = KEY % HASH ;
f[size] = ans ;
key[size] = KEY ;
next[size] = head[c] ;
head[c] = size++;
}
long long find( int u , int v ) {
long long KEY = K * u + v ;
int c = KEY % HASH ;
for( int i = head[c] ; ~i ; i = next[i] ) {
if( key[i] == KEY ) return f[i] ;
}
return - ;
}
} mp ; long long find( int u , int v ) {
int l = , r = m - ;
// cout << "l :" << l << " r : " << r << endl ;
if( l > r ) return - ;
if( u > e[r].u || ( u == e[r].u && v > e[r].v ) ) return - ;
if( v < e[l].v || ( v == e[l].v && u < e[l].u ) ) return - ;
while( l <= r ) {
int mid = (l+r)>>;
if( u < e[mid].u ) {
r = mid - ;
}
else if( u == e[mid].u ) {
if( v < e[mid].v ) r = mid - ;
else if( v > e[mid].v ) l = mid + ;
else return e[mid].cnt ;
}
else {
l = mid + ;
}
}
return - ;
}
vector< pair<int,long long> >g[N]; void Work() {
mp.init();
int q ; cin >> q ;
while( q-- ) {
int u , v ; cin >> u >> v ;
u-- , v-- ;
if( u > v ) swap( u , v ) ;
else if( u == v ) { cout << "0 1"<< endl ; continue ; }
long long ans = find( u , v ) ;
if( ans != - ) {
cout << "1 " << ans << endl ;
} else {
ans = mp.find( u , v ) ;
if( ans == - ) {
ans = ;
if( in[u] > in[v] ) swap( u , v ) ;
for( int i = ; i < g[u].size() ; ++i ) {
int mid = g[u][i].first ;
long long cnt1 = g[u][i].second , cnt2 = find( min(mid,v) , max(mid,v) );
if( cnt2 != - ) ans += cnt1 * cnt2 ;
}
if( u > v ) swap( u , v ) ;
mp.insert( u , v , ans ) ;
}
if( ans ) cout << "2 " << ans << endl ;
else cout << "The pair of cities are not connected or too far away." << endl ;
}
}
} void Gao() {
memset( in , , sizeof in );
for( int i = ; i < n ; ++i ) g[i].clear();
cin >> n >> m ;
if( !m ) return ;
for( int i = ; i < m ; ++i ) {
int x , y ; cin >> x >> y ;
x-- , y-- ;
if( x > y ) swap( x , y ) ;
e[i].u = x , e[i].v = y , e[i].cnt = ;
in[x]++ , in[y]++ ;
}
sort( e , e + m ) ;
tot = ;
for( int i = ; i < m ; ++i ) {
if( e[tot-].u == e[i].u && e[tot-].v == e[i].v ) {
e[tot-].cnt++ ;
} else {
e[tot++] = e[i] ;
}
}
m = tot ;
for( int i = ; i < m ; ++i ) {
g[e[i].u].push_back( make_pair( e[i].v,e[i].cnt) ) ;
g[e[i].v].push_back( make_pair( e[i].u,e[i].cnt) ) ;
}
} int Run() {
int _ , cas = ; cin >> _ ;
while( _-- ) {
cout << "Case #" << cas++ << ":" << endl ;
Gao(); Work();
}
return ;
}
int main() {
ios::sync_with_stdio();
return Run();
}
 

HDU 4014 Jimmy’s travel plan(图计数)的更多相关文章

  1. hdu 4885 TIANKENG’s travel(bfs)

    题目链接:hdu 4885 TIANKENG's travel 题目大意:给定N,L,表示有N个加油站,每次加满油能够移动距离L,必须走直线,可是能够为斜线.然后给出sx,sy,ex,ey,以及N个加 ...

  2. PAT-1030 Travel Plan (30 分) 最短路最小边权 堆优化dijkstra+DFS

    PAT 1030 最短路最小边权 堆优化dijkstra+DFS 1030 Travel Plan (30 分) A traveler's map gives the distances betwee ...

  3. PAT1030 Travel Plan (30)---DFS

    (一)题意 题目链接:https://www.patest.cn/contests/pat-a-practise/1030 1030. Travel Plan (30) A traveler's ma ...

  4. 有标号的DAG图计数1~4

    前言 我什么都不会,菜的被关了起来. 有标号的DAG图I Solution 考虑递推,设\(f_i\)表示i个点的答案,显然这个东西是可以组合数+容斥递推? 设\(f_i\)表示i个点的答案,我们考虑 ...

  5. PAT 1030 Travel Plan[图论][难]

    1030 Travel Plan (30)(30 分) A traveler's map gives the distances between cities along the highways, ...

  6. 1030 Travel Plan (30 分)

    1030 Travel Plan (30 分) A traveler's map gives the distances between cities along the highways, toge ...

  7. [图算法] 1030. Travel Plan (30)

    1030. Travel Plan (30) A traveler's map gives the distances between cities along the highways, toget ...

  8. PAT_A1030#Travel Plan

    Source: PAT A1030 Travel Plan (30 分) Description: A traveler's map gives the distances between citie ...

  9. PAT 甲级 1030 Travel Plan (30 分)(dijstra,较简单,但要注意是从0到n-1)

    1030 Travel Plan (30 分)   A traveler's map gives the distances between cities along the highways, to ...

随机推荐

  1. MySQL把多条数据给汇总成一条数据

    用到的是这个函数: group_concat() select group_buying_id, group_concat(app_user_ids) from org_user_group grou ...

  2. 《x86汇编语言:从实模式到保护模式 》学习笔记之:第一次编写汇编语言

    1.汇编语言源文件:first.asm mov ax,0x3f add bx,ax add cx,ax 2.用nasm编译成二进制文件:first.bin nasm -f bin first.asm ...

  3. Linux内核设计与实现 总结笔记(第十四章)块I/O层

    一.剖析一个块设备 块设备最小的可寻址单元是扇区. 扇区大小一般是2的整数倍,最常见的是512字节. 因为各种软件的用途不同,所以他们都会用到自己的最小逻辑可寻址单元----块.块只能基于文件系统,是 ...

  4. Linux内核设计与实现 总结笔记(第十章)内核同步方法

    一.原子操作 原子操作可以保证指令以原子的方式执行----执行过程不被打断. 1.1 原子整数操作 针对整数的原子操作只能对atomic_t类型的数据进行处理. 首先,让原子函数只接收atomic_t ...

  5. 批量下载文件php

    做了个照片墙,要提供批量下载照片的功能,如果你会文件下载,那批量也是小菜一碟,就是把文件打包压缩为  zip 文件再下载,而php的内置类ZipArchive()让你很容易实现. 首先,配置php.i ...

  6. 【FJ省队训练&&NOIP夏令营】酱油&&滚粗记

    FJOI2016省队训练滚粗记 2016.07.03~2016.07.06(Day1~5) 在学校期末考.因为才省选二试too young too simple爆蛋了所以下半个学期只能滚回去读文化课, ...

  7. <知识整理>2019清北学堂提高储备D2

    简单数据结构: 一.二叉搜索树 1.前置技能: n/1+n/2+……+n/n=O(n log n)  (本天复杂度常涉及) 2.入门题引入: N<=100000. 这里多了一个删除的操作,因此要 ...

  8. Spring Cloud教程(九)应用程序上下文服务

    Spring Boot对于如何使用Spring构建应用程序有一个看法:例如它具有常规配置文件的常规位置,以及用于常见管理和监视任务的端点.Spring Cloud建立在此之上,并添加了一些可能系统中所 ...

  9. sql server 高版本数据还原到低版本sql server的注意事项

    生成的sql脚本不能大于100m,否则会报错(System.OutOfMemory) 所以要将较大的sql脚本文件进行分割

  10. commons-collections包中的常用的工具类

    commons-collections包中的常用的工具类 <dependency> <groupId>commons-collections</groupId> & ...