您的位置:首页 > 其它

HDU5441 Travel 有秩并查集

2015-09-15 21:46 267 查看


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


Travel

Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)

Total Submission(s): 1173 Accepted Submission(s): 431



Problem Description

Jack likes to travel around the world, but he doesn’t like to wait. Now, he is traveling in the Undirected Kingdom. There are n cities
and m bidirectional
roads connecting the cities. Jack hates waiting too long on the bus, but he can rest at every city. Jack can only stand staying on the bus for a limited time and will go berserk after that. Assuming you know the time it takes to go from one city to another
and that the time Jack can stand staying on a bus is x minutes,
how many pairs of city (a,b) are
there that Jack can travel from city a to b without
going berserk?

Input

The first line contains one integer T,T≤5,
which represents the number of test case.

For each test case, the first line consists of three integers n,m and q where n≤20000,m≤100000,q≤5000.
The Undirected Kingdom has n cities
and mbidirectional
roads, and there are q queries.

Each of the following m lines
consists of three integers a,b and d where a,b∈{1,...,n} and d≤100000.
It takes Jack d minutes
to travel from city a to
city b and
vice versa.

Then q lines
follow. Each of them is a query consisting of an integer x where x is
the time limit before Jack goes berserk.

Output

You should print q lines
for each test case. Each of them contains one integer as the number of pair of cities (a,b) which
Jack may travel from a to b within
the time limit x.

Note that (a,b) and (b,a) are
counted as different pairs and a and b must
be different cities.

Sample Input

1
5 5 3
2 3 6334
1 5 15724
3 5 5705
4 3 12382
1 3 21726
6000
10000
13000


Sample Output

2
6
12


Source

2015 ACM/ICPC Asia Regional Changchun Online

n个城市,m条路,每条路有一个权值w; q个询问每次输入一个数temp,这个人可以走任何权值<=temp的路,

问这个人能走多少对城市 (城市(a,b)和(b,a)算两对); 就是算这个图有多少连通分量(不符合<=temp的边不加入图中),

对于每个连通分量,例如某连通分量有ABC三个城市,则有AB BA AC CA BC CB六个城市对;即有n个城市的连通分量

有n(n-1)个城市对;每个连通分量的城市数就是该集合根节点的秩(代码中的num[]数组);

计算结果时,每新加入一条边后的结果ans_now,可以由为加入该条边之前的结果ans递推得到;

例如两个连通分量的节点数为 fu ,fv;则join这两个连通分量后的结果ans_now=ans+(fu+fv)*(fu+fv-1)-fu*(fu-1)-fv*(fv-1)=2*fu*fv;

第一个程序没这么推也过了 就是程序时间复杂度较高

//time 873MS
//memery 2988K

#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<algorithm>
#define mem(a) memset(a,0,sizeof(a))
using namespace std;

struct node
{
int u,v;
int w;
}st[110000];

struct nod
{
int val;
int id;
friend bool operator<(nod a,nod b)
{
return a.val<b.val;
}
}qu[6000];
bool cmp(node a,node b)
{
return a.w<b.w;
}
int father[30000];
int num[30000];
void initial(int n)
{
int i;
for(i=0;i<=n;++i)
{
father[i]=i;
num[i]=1;
}
}
int find(int x)
{
int t=x;
while(x!=father[x])
{
x=father[x];
}
while(t!=father[t])
{
int temp=t;
t=father[t];
father[temp]=x;
}
return x;
}
bool join(int x,int y)
{
x=find(x);
y=find(y);
if(y<x)
{
swap(x,y);
}
if(x!=y)
{
father[y]=x;
num[x]+=num[y];
}
}
int ans[6000];
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
int n,m,q;
scanf("%d%d%d",&n,&m,&q);
int i;
for(i=0;i<m;++i)
{
scanf("%d%d%d",&st[i].u,&st[i].v,&st[i].w);
}
sort(st,st+m,cmp);
for(i=0;i<q;++i)
{
scanf("%d",&qu[i].val);
qu[i].id=i;
}
sort(qu,qu+q);
int t=0;
initial(n);
for(i=0;i<q;++i)
{
int temp;
temp=qu[i].val;
while(t<m&&st[t].w<=temp)
{
int u=st[t].u;
int v=st[t].v;
join(u,v);
t++;
}
int ans_temp=0;
for(int j=1;j<=n;++j)
{
if(father[j]==j&&num[j]>1)
{
ans_temp+=(num[j]*(num[j]-1));
}
}
ans[qu[i].id]=ans_temp;
}
for(i=0;i<q;++i)
{
printf("%d\n",ans[i]);
}
}
}


//time 312MS
//memery 2992K

#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<algorithm>
#define mem(a) memset(a,0,sizeof(a))
using namespace std;

struct node
{
int u,v;
int w;
}st[110000];

struct nod
{
int val;
int id;
friend bool operator<(nod a,nod b)
{
return a.val<b.val;
}
}qu[6000];

bool cmp(node a,node b)
{
return a.w<b.w;
}
int father[30000];
int num[30000];
void initial(int n)
{
int i;
for(i=0;i<=n;++i)
{
father[i]=i;
num[i]=1;
}
}
int find(int x)
{
int t=x;
while(x!=father[x])
{
x=father[x];
}
while(t!=father[t])
{
int temp=t;
t=father[t];
father[temp]=x;
}
return x;
}
bool join(int x,int y)
{
x=find(x);
y=find(y);
if(y<x)
{
swap(x,y);
}
if(x!=y)
{
father[y]=x;
num[x]+=num[y];
return 1;
}
return 0;
}
int ans[6000];
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
int n,m,q;
scanf("%d%d%d",&n,&m,&q);
int i;
for(i=0;i<m;++i)
{
scanf("%d%d%d",&st[i].u,&st[i].v,&st[i].w);
}
sort(st,st+m,cmp);
for(i=0;i<q;++i)
{
scanf("%d",&qu[i].val);
qu[i].id=i;
}
sort(qu,qu+q);
int t=0;
initial(n);
int ans_temp=0;
for(i=0;i<q;++i)
{
int temp;
temp=qu[i].val;
while(t<m&&st[t].w<=temp)
{
int u=st[t].u;
int v=st[t].v;
int u_r=find(u);
int v_r=find(v);
int num_u=num[u_r];
int num_v=num[v_r];
if(join(u,v))
{
ans_temp+=(2*num_u*num_v);
}
t++;
}

ans[qu[i].id]=ans_temp;
}
for(i=0;i<q;++i)
{
printf("%d\n",ans[i]);
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: