题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2874

这题有不连通的情况,特别注意。

觉得是存query的姿势不对,用前向星存了一遍,还是T……

 /*
━━━━━┒ギリギリ♂ eye!
┓┏┓┏┓┃キリキリ♂ mind!
┛┗┛┗┛┃\○/
┓┏┓┏┓┃ /
┛┗┛┗┛┃ノ)
┓┏┓┏┓┃
┛┗┛┗┛┃
┓┏┓┏┓┃
┛┗┛┗┛┃
┓┏┓┏┓┃
┛┗┛┗┛┃
┓┏┓┏┓┃
┃┃┃┃┃┃
┻┻┻┻┻┻
*/
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <cstring>
#include <climits>
#include <complex>
#include <fstream>
#include <cassert>
#include <cstdio>
#include <bitset>
#include <vector>
#include <deque>
#include <queue>
#include <stack>
#include <ctime>
#include <set>
#include <map>
#include <cmath>
using namespace std;
#define fr first
#define sc second
#define cl clear
#define BUG puts("here!!!")
#define W(a) while(a--)
#define pb(a) push_back(a)
#define Rint(a) scanf("%d", &a)
#define Rll(a) scanf("%lld", &a)
#define Rs(a) scanf("%s", a)
#define Cin(a) cin >> a
#define FRead() freopen("in", "r", stdin)
#define FWrite() freopen("out", "w", stdout)
#define Rep(i, len) for(int i = 0; i < (len); i++)
#define For(i, a, len) for(int i = (a); i < (len); i++)
#define Cls(a) memset((a), 0, sizeof(a))
#define Clr(a, x) memset((a), (x), sizeof(a))
#define Full(a) memset((a), 0x7f7f, sizeof(a))
#define lp p << 1
#define rp p << 1 | 1
#define pi 3.14159265359
#define RT return
#define lowbit(x) x & (-x)
#define onenum(x) __builtin_popcount(x)
typedef long long LL;
typedef long double LD;
typedef unsigned long long ULL;
typedef pair<int, int> pii;
typedef pair<string, int> psi;
typedef map<string, int> msi;
typedef vector<int> vi;
typedef vector<LL> vl;
typedef vector<vl> vvl;
typedef vector<bool> vb; typedef struct Query {
int idx;
int u, v;
Query() {}
Query(int uu, int vv, int ii) : u(uu), v(vv), idx(ii) {}
}Query; typedef struct Edge {
int u, v, w, idx;
int next;
Edge() {}
Edge(int uu, int vv, int ww) : u(uu), v(vv), w(ww) {}
}Edge; typedef struct Ans {
int idx;
int ans;
Ans() {}
Ans(int aa, int ii) :ans(aa), idx(ii) {}
}Ans; const int maxn = ;
const int maxm = ;
int n, m, c, qcnt;
int depth[maxn];
bool vis[maxn];
int pre[maxn];
Ans ans[maxm];
Edge edge[maxn];
Edge q[maxm];
Query qq[maxm];
int qhead[maxm];
int head[maxn];
int ecnt;
int u, v, w; void adde(int u, int v, int w) {
edge[ecnt].u = u;
edge[ecnt].v = v;
edge[ecnt].w = w;
edge[ecnt].next = head[u];
head[u] = ecnt++;
} void addq(int u, int v, int i) {
q[qcnt].u = u;
q[qcnt].v = v;
q[qcnt].idx = i;
q[qcnt].next = qhead[u];
qhead[u] = ecnt++;
} int find(int x) {
return x == pre[x] ? x : pre[x] = find(pre[x]);
} void unite(int x, int y) {
x = find(x);
y = find(y);
if(x != y) pre[y] = x;
} void dfs(int u, int p, int d) {
depth[u] = d;
for(int i = head[u]; ~i; i=edge[i].next) {
int v = edge[i].v;
if(!vis[v] && v != p) {
dfs(v, u, d+edge[i].w);
unite(u, v);
}
}
vis[u] = ;
for(int i = qhead[u]; ~i; i=q[i].next) {
int uu = q[i].u;
int vv = q[i].v;
int idx = q[i].idx;
if((vis[vv] && uu == u) || (vis[uu] && vv == u)) {
ans[idx].ans = depth[vv] + depth[uu];
}
}
} bool cmp(Ans x, Ans y) {
return x.idx < y.idx;
} int ufs[maxn]; int find1(int x) {
return ufs[x] == x ? x : ufs[x] = find1(ufs[x]);
} void unite1(int x, int y) {
x = find1(x);
y = find1(y);
if(x != y) ufs[y] = x;
} inline bool scan_d(int &num) {
char in;bool IsN=false;
in=getchar();
if(in==EOF) return false;
while(in!='-'&&(in<''||in>'')) in=getchar();
if(in=='-'){ IsN=true;num=;}
else num=in-'';
while(in=getchar(),in>=''&&in<=''){
num*=,num+=in-'';
}
if(IsN) num=-num;
return true;
} int main() {
// FRead();
while(~scanf("%d%d%d",&n,&m,&c)) {
Cls(vis); Cls(depth); Cls(ans); Clr(head, -); Clr(qhead, -);
qcnt = ; Rep(i, n+) pre[i] = i, ufs[i] = i;
Rep(i, m) {
scan_d(u); scan_d(v); scan_d(w);
adde(u, v, w); adde(v, u, w);
unite1(u, v);
}
Rep(i, c) {
scan_d(u); scan_d(v);
addq(u, v, i);
qq[i] = Query(u, v, i);
}
For(i, , n+) if(!vis[i]) dfs(i, -, );
Rep(i, c) {
if(find1(qq[ans[i].idx].u) != find1(qq[ans[i].idx].v)) puts("Not connected");
else printf("%d\n", ans[i].ans);
}
}
RT ;
}

……TARJAN也T了……

 /*
━━━━━┒ギリギリ♂ eye!
┓┏┓┏┓┃キリキリ♂ mind!
┛┗┛┗┛┃\○/
┓┏┓┏┓┃ /
┛┗┛┗┛┃ノ)
┓┏┓┏┓┃
┛┗┛┗┛┃
┓┏┓┏┓┃
┛┗┛┗┛┃
┓┏┓┏┓┃
┛┗┛┗┛┃
┓┏┓┏┓┃
┃┃┃┃┃┃
┻┻┻┻┻┻
*/
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <cstring>
#include <climits>
#include <complex>
#include <fstream>
#include <cassert>
#include <cstdio>
#include <bitset>
#include <vector>
#include <deque>
#include <queue>
#include <stack>
#include <ctime>
#include <set>
#include <map>
#include <cmath>
using namespace std;
#define fr first
#define sc second
#define cl clear
#define BUG puts("here!!!")
#define W(a) while(a--)
#define pb(a) push_back(a)
#define Rint(a) scanf("%d", &a)
#define Rll(a) scanf("%lld", &a)
#define Rs(a) scanf("%s", a)
#define Cin(a) cin >> a
#define FRead() freopen("in", "r", stdin)
#define FWrite() freopen("out", "w", stdout)
#define Rep(i, len) for(int i = 0; i < (len); i++)
#define For(i, a, len) for(int i = (a); i < (len); i++)
#define Cls(a) memset((a), 0, sizeof(a))
#define Clr(a, x) memset((a), (x), sizeof(a))
#define Full(a) memset((a), 0x7f7f, sizeof(a))
#define lp p << 1
#define rp p << 1 | 1
#define pi 3.14159265359
#define RT return
#define lowbit(x) x & (-x)
#define onenum(x) __builtin_popcount(x)
typedef long long LL;
typedef long double LD;
typedef unsigned long long ULL;
typedef pair<int, int> pii;
typedef pair<string, int> psi;
typedef map<string, int> msi;
typedef vector<int> vi;
typedef vector<LL> vl;
typedef vector<vl> vvl;
typedef vector<bool> vb; typedef struct Query {
int idx;
int u, v;
Query() {}
Query(int uu, int vv, int ii) : u(uu), v(vv), idx(ii) {}
}Query; typedef struct Edge {
int u, v, w;
int next;
Edge() {}
Edge(int uu, int vv, int ww) : u(uu), v(vv), w(ww) {}
}Edge; typedef struct Ans {
int idx;
int ans;
Ans() {}
Ans(int aa, int ii) :ans(aa), idx(ii) {}
}Ans; const int maxn = ;
const int maxm = ;
int n, m, c, qcnt;
int depth[maxn];
bool vis[maxn];
int pre[maxn];
Query q[maxm];
Ans ans[maxm];
Edge edge[maxn];
int head[maxn];
int ecnt;
int u, v, w; void adde(int u, int v, int w) {
edge[ecnt].u = u;
edge[ecnt].v = v;
edge[ecnt].w = w;
edge[ecnt].next = head[u];
head[u] = ecnt++;
} int find(int x) {
return x == pre[x] ? x : pre[x] = find(pre[x]);
} void unite(int x, int y) {
x = find(x);
y = find(y);
if(x != y) pre[y] = x;
} void dfs(int u, int p, int d) {
depth[u] = d;
for(int i = head[u]; ~i; i=edge[i].next) {
int v = edge[i].v;
if(!vis[v] && v != p) {
dfs(v, u, d+edge[i].w);
unite(u, v);
}
}
vis[u] = ;
Rep(i, qcnt) {
int uu = q[i].u;
int vv = q[i].v;
int idx = q[i].idx;
if((vis[vv] && uu == u) || (vis[uu] && vv == u)) {
ans[idx].idx = idx;
ans[idx].ans = depth[vv] + depth[uu];
}
}
} bool cmp(Ans x, Ans y) {
return x.idx < y.idx;
} int ufs[maxn]; int find1(int x) {
return ufs[x] == x ? x : ufs[x] = find1(ufs[x]);
} void unite1(int x, int y) {
x = find1(x);
y = find1(y);
if(x != y) ufs[y] = x;
} inline bool scan_d(int &num) {
char in;bool IsN=false;
in=getchar();
if(in==EOF) return false;
while(in!='-'&&(in<''||in>'')) in=getchar();
if(in=='-'){ IsN=true;num=;}
else num=in-'';
while(in=getchar(),in>=''&&in<=''){
num*=,num+=in-'';
}
if(IsN) num=-num;
return true;
} int main() {
// FRead();
while(~scanf("%d%d%d",&n,&m,&c)) {
Cls(vis); Cls(depth); Cls(ans); Clr(head, -);
qcnt = ; Rep(i, n+) pre[i] = i, ufs[i] = i;
Rep(i, m) {
scan_d(u); scan_d(v); scan_d(w);
adde(u, v, w); adde(v, u, w);
unite1(u, v);
}
Rep(i, c) {
scan_d(u); scan_d(v);
q[qcnt++] = Query(u, v, i);
}
For(i, , n+) if(!vis[i]) dfs(i, -, );
Rep(i, c) {
if(find1(q[ans[i].idx].u) != find1(q[ans[i].idx].v)) puts("Not connected");
else printf("%d\n", ans[i].ans);
}
}
RT ;
}

在线胡搞T了,等下写个离线的。

TLE代码:

 /*
━━━━━┒ギリギリ♂ eye!
┓┏┓┏┓┃キリキリ♂ mind!
┛┗┛┗┛┃\○/
┓┏┓┏┓┃ /
┛┗┛┗┛┃ノ)
┓┏┓┏┓┃
┛┗┛┗┛┃
┓┏┓┏┓┃
┛┗┛┗┛┃
┓┏┓┏┓┃
┛┗┛┗┛┃
┓┏┓┏┓┃
┃┃┃┃┃┃
┻┻┻┻┻┻
*/
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <cstring>
#include <climits>
#include <complex>
#include <fstream>
#include <cassert>
#include <cstdio>
#include <bitset>
#include <vector>
#include <deque>
#include <queue>
#include <stack>
#include <ctime>
#include <set>
#include <map>
#include <cmath>
using namespace std;
#define fr first
#define sc second
#define cl clear
#define BUG puts("here!!!")
#define W(a) while(a--)
#define pb(a) push_back(a)
#define Rint(a) scanf("%d", &a)
#define Rll(a) scanf("%I64d", &a)
#define Rs(a) scanf("%s", a)
#define Cin(a) cin >> a
#define FRead() freopen("in", "r", stdin)
#define FWrite() freopen("out", "w", stdout)
#define Rep(i, len) for(int i = 0; i < (len); i++)
#define For(i, a, len) for(int i = (a); i < (len); i++)
#define Cls(a) memset((a), 0, sizeof(a))
#define Clr(a, x) memset((a), (x), sizeof(a))
#define Full(a) memset((a), 0x7f7f, sizeof(a))
#define pi 3.14159265359
#define RT return
#define lowbit(x) x & (-x)
#define onenum(x) __builtin_popcount(x)
typedef long long LL;
typedef long double LD;
typedef unsigned long long ULL;
typedef pair<int, int> pii;
typedef pair<string, int> psi;
typedef map<string, int> msi;
typedef vector<int> vi;
typedef vector<LL> vl;
typedef vector<vl> vvl;
typedef vector<bool> vb; typedef struct Edge {
int u, v, w;
int next;
}Edge;
const int maxn = ;
const int maxm = ;
int n, m, c;
int depth[maxn], fa[maxn];
int pre[maxn];
Edge edge[maxm];
int head[maxn];
int ecnt;
bool vis[maxn]; void adde(int u, int v, int w) {
edge[ecnt].u = u;
edge[ecnt].v = v;
edge[ecnt].w = w;
edge[ecnt].next = head[u];
head[u] = ecnt++;
} int find(int x) {
x == pre[x] ? x : pre[x] = find(pre[x]);
} void unite(int x, int y) {
x = find(x);
y = find(y);
if(x != y) pre[y] = x;
} void dfs(int u, int p, int d) {
vis[u] = ;
depth[u] = d; fa[u] = p;
for(int i = head[u]; ~i; i=edge[i].next) {
int v = edge[i].v;
int w = edge[i].w;
if(!vis[v]) dfs(v, u, d+w);
}
} int lca(int u, int v) {
int s = ;
while(depth[u] > depth[v]) {
s += depth[u];
u = pre[u];
}
while(depth[v] > depth[u]) {
s += depth[v];
v = pre[v];
}
while(u != v) {
s += (depth[u] + depth[v]);
u = pre[u];
v = pre[v];
}
return s;
} inline bool scan_d(int &num) {
char in;bool IsN=false;
in=getchar();
if(in==EOF) return false;
while(in!='-'&&(in<''||in>'')) in=getchar();
if(in=='-'){ IsN=true;num=;}
else num=in-'';
while(in=getchar(),in>=''&&in<=''){
num*=,num+=in-'';
}
if(IsN) num=-num;
return true;
} int main() {
FRead();
int u, v, w;
while(~scanf("%d%d%d",&n,&m,&c)) {
Clr(head, -); ecnt = ;
Rep(i, n+) pre[i] = i;
Rep(i, m) {
scan_d(u); scan_d(v); scan_d(w);
adde(u, v, w); adde(v, u, w);
unite(u, v);
}
For(i, , n+) {
if(!vis[i]) {
dfs(i, -, );
}
}
W(c) {
scan_d(u); scan_d(v);
if(find(u) != find(v)) puts("Not connected");
else printf("%d\n", lca(u, v));
}
}
RT ;
}

[HDOJ2874]Connections between cities(LCA, 离线tarjan)的更多相关文章

  1. hdu-2874 Connections between cities(lca+tarjan+并查集)

    题目链接: Connections between cities Time Limit: 10000/5000 MS (Java/Others)     Memory Limit: 32768/327 ...

  2. HDU 2874 Connections between cities(LCA Tarjan)

    Connections between cities [题目链接]Connections between cities [题目类型]LCA Tarjan &题意: 输入一个森林,总节点不超过N ...

  3. hdu 2874 Connections between cities [LCA] (lca->rmq)

    Connections between cities Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (J ...

  4. [POJ1330]Nearest Common Ancestors(LCA, 离线tarjan)

    题目链接:http://poj.org/problem?id=1330 题意就是求一组最近公共祖先,昨晚学了离线tarjan,今天来实现一下. 个人感觉tarjan算法是利用了dfs序和节点深度的关系 ...

  5. 近期公共祖先(LCA)——离线Tarjan算法+并查集优化

    一. 离线Tarjan算法 LCA问题(lowest common ancestors):在一个有根树T中.两个节点和 e&sig=3136f1d5fcf75709d9ac882bd8cfe0 ...

  6. LCA离线Tarjan,树上倍增入门题

    离线Tarjian,来个JVxie大佬博客最近公共祖先LCA(Tarjan算法)的思考和算法实现,还有zhouzhendong大佬的LCA算法解析-Tarjan&倍增&RMQ(其实你们 ...

  7. Connections between cities LCA

    Problem Description After World War X, a lot of cities have been seriously damaged, and we need to r ...

  8. HDU 2874 Connections between cities (LCA)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2874 题意是给你n个点,m条边(无向),q个询问.接下来m行,每行两个点一个边权,而且这个图不能有环路 ...

  9. [hdu2874]Connections between cities(LCA+并查集)

    题意:n棵树,求任意两点的最短距离. 解题关键:并查集判断两点是否位于一棵树上,然后求最短距离即可.此题可以直接对全部区间直接进行st表,因为first数组会将连接的两点的区间表示出来. //#pra ...

随机推荐

  1. 分布式文件系统 - FastDFS

    分布式文件系统 - FastDFS 别问我在哪里 也许我早已不是我自己,别问我在哪里,我一直在这里. 突然不知道说些什么了... 初识 FastDFS 记得那是我刚毕业后进入的第一家公司,一个技术小白 ...

  2. Notes of the scrum meeting(2013/10/23)

    ps:本来是10月23号周三下午开的会,这几天由于各种事情忙,忘记写博客了,现在补上. 软工项目组buaa_smile开始项目第一次scrum meeting meeting time:4:00~5: ...

  3. C语言基础:指针类型与指针和数组、字符串的关系

    //指针变量就是用来存储地址的,只能存储地址 格式:  int  *p;  这个p为指针变量:指针变量占8个字节 类型是用来说明这个指针指向的类型: 比如上边的int代表这个指针变量会指向int类型的 ...

  4. [resource-]Python 网页爬虫 & 文本处理 & 科学计算 & 机器学习 & 数据挖掘兵器谱

    reference: http://www.52nlp.cn/python-%e7%bd%91%e9%a1%b5%e7%88%ac%e8%99%ab-%e6%96%87%e6%9c%ac%e5%a4% ...

  5. javaScript基础之闭包

    不管是Jquery还是EXTJS,现代的js框架中大量应用了js的一些特性,比如:匿名函数,闭包等等,导致源代码非常难以阅读. 不过要想真正的使用好前台的UI技术,还是需要去深入的理解这些概念.   ...

  6. 用hibernate自动创建mysql表,添加失败org.hibernate.exception.SQLGrammarException

    今天遇到了一个很坑人的问题,从昨晚一直搞到今天早上,终于发现了,先整理下: [背景]:利用hibernate自动给mysql创建一个表,然后为表里面添加一行记录,非常的简单(当然其中还涉及到sprin ...

  7. list 去掉重复的值

    去除List列表中重复值(3种解决方法)public static void main(String[] args) { String[] ar = { "dd", "c ...

  8. Linq查询Count、Sum、Min、Max、Average

    原文地址:Linq——Count.Sum.Min.Max.Average作者:mousekitty Linq查询之Count.Sum.Min.Max.Average using System; usi ...

  9. CentOS安装视频播放器SMPlayer

    首先下载rpmforg,下载对应的版本,就是对应CentOS版本,还有32位与64位也要对应上.地址如下: http://wiki.centos.org/AdditionalResources/Rep ...

  10. Sudoku Solver

    Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...