#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <vector>
#include <map>
#define INF 0x3f3f3f3f
#define rep0(i, n) for (int i = 0; i < n; i++)
#define rep1(i, n) for (int i = 1; i <= n; i++)
#define rep_0(i, n) for (int i = n - 1; i >= 0; i--)
#define rep_1(i, n) for (int i = n; i > 0; i--)
#define MAX(x, y) (((x) > (y)) ? (x) : (y))
#define MIN(x, y) (((x) < (y)) ? (x) : (y))
#define mem(x, y) memset(x, y, sizeof(x))
#define MAXN (10000 + 10)
#define MAXC (1000000 + 10)
/**
题目大意
给一个森林,多次询问节点之间的距离
思路
节点数的1e4 询问数1e6
离线tarjan的话怎么存询问很关键
用邻接表的边表存很省空间同时查询的复杂度很低,很巧妙
当访问一个节点和它有询问关系的节点时很方便
一个询问要存两次 但存放时是有序的q[0, 1, 2, 3, 4, 5, 6, 7...]
没计算出一个答案可以通过 i >> 1 存入时询问的答案仍然有序
HINT
每次用宏定义表示数组大小总是被坑 一定记得加括号
明明时下表越界用G++交却报称tle 很迷惑啊
*/
using namespace std;
typedef long long LL;
struct query
{
int to;
int next;
} q[MAXC * 2];
LL head[MAXN], ans[MAXC], t;
struct Edge
{
int to, w;
int next;
} edges[MAXN * 2];
int g[MAXN];
LL dis[MAXN];
int n, root, pa[MAXN];
int vis[MAXN];
void addEdge(int u, int v, int w)
{
edges[t].to = v;
edges[t].w = w;
edges[t].next = g[u];
g[u] = t++;
edges[t].to = u;
edges[t].w = w;
edges[t].next = g[v];
g[v] = t++;
}
int find_set(int x)
{
if (pa[x] == x)
return x;
pa[x] = find_set(pa[x]);
return pa[x];
}
void tarjan(int u, int fa)
{
vis[u] = root;
for (int i = g[u]; i != -1; i = edges[i].next)
{
Edge e = edges[i];
int v = e.to;
if (v == fa)
continue;
dis[v] = dis[u] + e.w;
tarjan(v, u);
pa[v] = u;
}
for (int i = head[u]; i != -1; i = q[i].next)
{
int v = q[i].to;
if (vis[v] != root)
continue;
int lca = find_set(v);
ans[i >> 1] = dis[u] + dis[v] - 2 * dis[lca];
}
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
#endif // ONLINE_JUDGE
int m;
LL c;
while (scanf("%d %d %lld", &n, &m, &c) != EOF)
{
mem(head, -1);
for (int i = 1; i <= n; i++)
{
g[i] = -1;
pa[i] = i;
vis[i] = 0;
}
int u, v;
int w;
t = 0;
for (int i = 0; i < m; i++)
{
scanf("%d %d %d", &u, &v, &w);
addEdge(u, v, w);
}
t = 0;
for (int i = 0; i < c; i++)
{
ans[i] = -1;
scanf("%d %d", &u, &v);
q[t].to = v;
q[t].next = head[u];
head[u] = t++;
q[t].to = u;
q[t].next = head[v];
head[v] = t++;
}
for (int i = 1; i <= n; i++)
{
if (vis[i] > 0)
continue;
root = i;
dis[i] = 0;
tarjan(i, -1);
}
for (int i = 0; i < c; i++)
{
if (ans[i] >= 0)
printf("%lld\n", ans[i]);
else
printf("Not connected\n");
}
}
return 0;
}
hdu2874_Connections between cities(tarjan/lca/边表)
最新推荐文章于 2018-09-13 01:08:51 发布