这题真的有毒
首先你忽略 N-M < 100 的条件你就gg吧
其次就算你知道了怎么做之后
还有可能因为写vector或者各种常数大的原因被卡

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<map>
#include<cmath>
#include<cstring>
#include<queue>
#include<set>
using namespace std;
typedef long long ll;
typedef pair<int,int> pi;
const int MAXN = 1e5+205; int N,M,Q;
struct Node{
int to,nex;
}mip[MAXN*2], vic[MAXN*2];
int mp[MAXN]; int mot;
int vc[MAXN]; int vot; int vis[MAXN];
int pre[MAXN]; int tag[MAXN];
int po[MAXN]; int po_cc; int cc;
void add1(int x,int y) {
mip[mot].to = y; mip[mot].nex = mp[x]; mp[x] = mot++;
mip[mot].to = x; mip[mot].nex = mp[y]; mp[y] = mot++;
}
void add2(int x,int y) {
vic[vot].to = y; vic[vot].nex = vc[x]; vc[x] = vot++;
vic[vot].to = x; vic[vot].nex = vc[y]; vc[y] = vot++;
}
map< pair<int,int>, int> mmp;
int dis[205][MAXN];
/*************LCA***************/
const int DEG=20;
int fa[MAXN][DEG];
int deg[MAXN];
void BFS(int rt) {
queue<int> qq;
deg[rt] = 0;
fa[rt][0] = rt;
qq.push(rt);
while(!qq.empty()){
int tmp = qq.front(); qq.pop();
for(int i = 1; i < DEG; ++i)
fa[tmp][i] = fa[fa[tmp][i-1]][i-1];
for(int i = vc[tmp]; i !=-1; i = vic[i].nex) {
int v = vic[i].to; if(v == fa[tmp][0]) continue;
deg[v] = deg[tmp]+1; fa[v][0]=tmp;
qq.push(v);
}
}
}
int LCA(int u,int v) {
if(deg[u] > deg[v]) swap(u,v);
int hu = deg[u], hv = deg[v];
int tu = u, tv = v;
for(int det = hv-hu, i=0; det; det>>=1, ++i) {
if(det&1) {
tv = fa[tv][i];
}
}
if(tu == tv) return tu;
for(int i = DEG-1; i >= 0; --i) {
if(fa[tu][i] == fa[tv][i]) continue;
tu = fa[tu][i];
tv = fa[tv][i];
}
return fa[tu][0];
}
/*****makedis**********/
void solve(int x) {
memset(vis,0,sizeof(vis));
queue<int> qq;
qq.push(x); dis[cc][x] = 0; vis[x] = 1; pre[x]=x;
while(!qq.empty()) {
int b = qq.front(); qq.pop();
for(int i = mp[b]; i != -1; i = mip[i].nex) {
int y = mip[i].to;
if(!vis[y]) {
// if(!cc) printf("hh %d %d\n",b,y);
dis[cc][y] = dis[cc][b]+1; vis[y] = 1; pre[y]=b;
if(!cc) {
add2(b,y);
}
qq.push(y);
}else if(pre[b] != y && !cc) {
// printf("%d %d\n",b,y);
tag[b]++; if(tag[b] == 1) po[++po_cc] = b;
tag[y]++; if(tag[y] == 1) po[++po_cc] = y;
}
}
}
}
int doo(int x,int y) {
int tt = LCA(x,y);
int ans = -dis[0][tt]*2 + dis[0][x] + dis[0][y];
if(tt == x || tt == y) return ans;
for(int i = 1; i <= po_cc; ++i) {
ans = min(ans, dis[i][x]+dis[i][y]);
}
return ans;
}
int main(){
while(~scanf("%d %d %d",&N,&M,&Q)) {
memset(mp,-1,sizeof(mp)); mot = 0;
memset(vc,-1,sizeof(vc)); vot = 0;
memset(tag,0,sizeof(tag));
mmp.clear(); po_cc = 0; cc = 0;
for(int i = 1; i <= M; ++i) {
int a,b; scanf("%d %d",&a,&b);
if(a > b) swap(a,b);
if( a == b || mmp[pi(a,b)] ) continue;
add1(a,b);
mmp[pi(a,b)] ++;
} solve(1);
BFS(1);
for(int i = 1; i <= po_cc; ++i) {
cc++; solve(po[i]);
}
for(int i = 0; i < Q; ++i) {
int a,b; scanf("%d %d",&a,&b);
printf("%d\n",doo(a,b));
}
}
return 0;
}

2016四川省赛 Floyd-Warshall的更多相关文章

  1. 2016四川省赛A,C【写了1w个if的水题】

    A题: #include <iostream> #include <stdio.h> #include <string.h> #include <algori ...

  2. 图论之最短路径(1)——Floyd Warshall & Dijkstra算法

    开始图论学习的第二部分:最短路径. 由于知识储备还不充足,暂时不使用邻接表的方法来计算. 最短路径主要分为两部分:多源最短路径和单源最短路径问题 多源最短路径: 介绍最简单的Floyd Warshal ...

  3. 2016湖南省赛----G - Parenthesis (括号匹配)

    2016湖南省赛----G - Parenthesis (括号匹配)   Bobo has a balanced parenthesis sequence P=p 1 p 2…p n of lengt ...

  4. 2016湖南省赛----A 2016 (同余定理)

    2016湖南省赛----A 2016 (同余定理) Description  给出正整数 n 和 m,统计满足以下条件的正整数对 (a,b) 的数量: 1. 1≤a≤n,1≤b≤m; 2. a×b 是 ...

  5. 2016湖南省赛 I Tree Intersection(线段树合并,树链剖分)

    2016湖南省赛 I Tree Intersection(线段树合并,树链剖分) 传送门:https://ac.nowcoder.com/acm/contest/1112/I 题意: 给你一个n个结点 ...

  6. SCNU ACM 2016新生赛初赛 解题报告

    新生初赛题目.解题思路.参考代码一览 1001. 无聊的日常 Problem Description 两位小朋友小A和小B无聊时玩了个游戏,在限定时间内说出一排数字,那边说出的数大就赢,你的工作是帮他 ...

  7. 2016湖南省赛 [Cloned]

    A.2016 给出正整数 n 和 m,统计满足以下条件的正整数对 (a,b) 的数量: 1. 1≤a≤n,1≤b≤m; 2. a×b 是 2016 的倍数. Input   输入包含不超过 30 组数 ...

  8. SCNU ACM 2016新生赛决赛 解题报告

    新生初赛题目.解题思路.参考代码一览 A. 拒绝虐狗 Problem Description CZJ 去排队打饭的时候看到前面有几对情侣秀恩爱,作为单身狗的 CZJ 表示很难受. 现在给出一个字符串代 ...

  9. 2016湖南省赛--A题--2016

    2016 [TOC] Description 给出正整数 n 和 m,统计满足以下条件的正整数对 (a,b) 的数量: 1. 1≤a≤n,1≤b≤m; 2. a×b 是 2016 的倍数. Input ...

随机推荐

  1. BZOJ 3028: 食物 [生成函数 隔板法 | 广义二项式定理]

    3028: 食物 Time Limit: 3 Sec  Memory Limit: 128 MBSubmit: 497  Solved: 331[Submit][Status][Discuss] De ...

  2. BZOJ 1037: [ZJOI2008]生日聚会Party [序列DP]

    1037: [ZJOI2008]生日聚会Party Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 2249  Solved: 1337[Submit] ...

  3. python3加密解密模块 cryptography

    cryptography 的目标是成为"人类易于使用的密码学包cryptography for humans",就像 requests 是"人类易于使用的 HTTP 库H ...

  4. 实时滚动图表绘制方法: LightningChart教程 + 源码下载

    LightningChart图形控件彻底发挥了GPU加速和性能优化的最大效应,能够实时呈现超过10亿数据点的庞大数据,为大家提供先进与快速的图表库.这里的实时图实现的比较简单,大家先试一下这个效果,熟 ...

  5. Linux一些常用操作

    1.linux swap分区 可采用文件的方式 dd if=/dev/zero of=/var/swap bs=1024 count=2048000 mkswap /var/swap swapon / ...

  6. jQuery(function(){...})与(function($){...})(jQuery)的“兄弟”情结

    记得那时在学习写基于jQuery的插件时,了解到(function($){...})(jQuery)的代码结构,一开始还没发觉,后来百度了解它的语意时,从搜索结果中发现了jQuery(function ...

  7. 如何写出测不出bug的测试用例

    我们写测试用例的目的是为了能够整理思路,把要测试的地方列出来,做为知识的积淀,用例可以交给其他测试人员执行,或者是跟需求提出者进行讨论,对用例进行补充和修改. 理论上用例写的越多,越容易发现bug.但 ...

  8. 新人学习selenium哪些资源比较有帮助?

    为什么学习selenium? selenium现在基本上成了页面自动化测试的标配,具体理由我在selenium 3.0发布这篇文章里已经说明过了.当一个东西成为标准以后,那么它的能量和潜力都是巨大的. ...

  9. JPA数据懒加载LAZY配合事务@Transactional使用(三)

    上篇博文<JPA数据懒加载LAZY和实时加载EAGER(二)>讲到,如果使用懒加载来调用关联数据,必须要保证主查询session(数据库连接会话)的生命周期没有结束,否则,你是无法抽取到数 ...

  10. TP3.2 中使用 PHPMailer 发送邮件

    第一步.添加PHPMailer类库 http://pan.baidu.com/s/1o7Zc7V0 第二步.添加发送邮件函数 在common目录中的公共函数文件加入函数 <?php /***** ...