# HDU 2874 - Connections between cities(LCA‘离线算法Tarjan)

275人阅读 评论(0)

AC.

#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
using namespace std;
const int INF = 0x3f3f3f;
const int maxn = 10010;
const int maxq = 1000010;

int far[maxn], rnk[maxn];
int fg(int x)
{
if(far[x] == x) return x;
return far[x] = fg(far[x]);
}
void unite(int a, int b)
{
int x = fg(a);
int y = fg(b);
if(x == y) return;
if(rnk[x] <= rnk[y]) far[x] = y, rnk[y] += rnk[x];
else far[y] = x, rnk[x] += rnk[y];
}

struct Edge {
int to, next, w;
}edge[maxn*4];
void addedge(int u, int v, int w)
{
edge[tot].to = v;
edge[tot].w = w;
}

int tt;

{
h[u] = tt++;
}

int n, m;
int ans[maxq], dis[maxn], anc[maxn];
bool vis[maxn];

void init()
{
tot = 0;
tt = 0;
memset(h, -1, sizeof(h));
memset(vis, 0, sizeof(vis));
memset(dis, 0, sizeof(dis));
memset(anc, 0, sizeof(anc));
memset(ans, 0, sizeof(ans));

for(int i = 0; i <= n+1; ++i) {
far[i] = i;
rnk[i] = 1;
}
}

void LCA(int u)
{
anc[u] = u;
vis[u] = 1;
for(int i = head[u] ; i != -1 ; i = edge[i].next){
int v = edge[i].to;
if(!vis[v]){
dis[v] = dis[u] + edge[i].w;
LCA(v);
unite(u, v);
anc[fg(u)] = u;
}
}

for(int i = h[u] ; i != -1 ; i = asknext[i]){
if(vis[v]){
}
}
}

int main()
{
//freopen("in", "r", stdin);
int k;
while(~scanf("%d %d %d", &n, &m, &k)) {
init();
int u, v, w;
for(int i = 0; i < m; ++i) {
scanf("%d %d %d", &u, &v, &w);
}

for(int i = 0; i < k; ++i) {
scanf("%d %d", &u, &v);
}

for(int i = 1; i <= n; ++i) {
if(far[i] == i) {
}
}

LCA(n+1);

for(int i = 0 ; i < tt; i+=2){
else
printf("Not connected\n");
}
}
}


0
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
Dreaming
Double Fighting
个人资料
• 访问：165787次
• 积分：5894
• 等级：
• 排名：第4300名
• 原创：438篇
• 转载：2篇
• 译文：7篇
• 评论：22条
最新评论