这题真的有毒
首先你忽略 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. RChain的Casper共识算法

    RChain的Casper共识算法是基于Vlad Zamfir的correct-by-construction共识协议和CTO Greg Meredith和其他RChain成员讨论而来的.他们还为Ca ...

  2. BZOJ 2938: [Poi2000]病毒 [AC自动机 拓扑排序]

    2938: [Poi2000]病毒 题意:判断是否存在无限长的不含模式串的字符串.只有01. 建出套路DP的转移图,判断有环就行了 练习一下拓扑排序 #include <iostream> ...

  3. .NET方面的框架的整理和总结

    自从学习.NET以来,优雅的编程风格,极度简单的可扩展性,足够强大开发工具,极小的学习曲线,让我对这个平台产生了浓厚的兴趣,在工作和学习中也积累了一些开源的组件,就目前想到的先整理于此,如果再想到,就 ...

  4. Delegate &&Lambda

    匿名函数及委托的使用: public delegate void NoReturnNoParaOutClass();//delegate can be defined in class or out ...

  5. pandas中的分组技术

    目录 1  分组操作 1.1  按照列进行分组 1.2  按照字典进行分组 1.3  根据函数进行分组 1.4  按照list组合 1.5  按照索引级别进行分组 2  分组运算 2.1  agg 2 ...

  6. Jmeter_实现操作postgresql数据库

    [环境] ①Jmeter版本:3.2,JDK:1.8: ②postgresql驱动包postgresql-9.3-1103.jdbc4,将该jar包置于..\apache-jmeter-3.2\lib ...

  7. 11个phpstorm 快捷键介绍

    11个phpstorm 快捷键介绍 http://phpstorm.tips/tips/11-toggling-case

  8. mysql 修改默认字符集为utf8

    MySQL 5.5, all you need is: [mysqld] character_set_client=utf8 character_set_server=utf8 collation_s ...

  9. iOS 应用开发,用户密码存储技术--KeyChain

    文/清雪飘香(简书作者)原文链接:http://www.jianshu.com/p/c41525172aee著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”. 这次的Xcode 事件,让我 ...

  10. 拥抱.NET Core系列:MemoryCache 初识

    Cache是一个绝大多数项目会用到的一个技术,说起到缓存可能就联想到 Set.Add.Get.Remove.Clear 这几个方法.那么在.NET Core中微软给我们带来了什么样的缓存体验呢?今天我 ...