最小生成树(削减某条边后该边能否构成最小生成树的成分)

http://poj.org/problem?id=2831

Can We Build This One?
Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 1163   Accepted: 435
Case Time Limit: 2000MS

Description

“Highways are built, then life is rich.” Now people of Big Town want to become rich, so they are planning to build highways to connect their villages.

Big Town is really big and has many villages. Its people plan to build some highways between some pairs of villages so that every pair of villages is connected by the highways either directly or indirectly. After surveying the geographical surroundings, they find that there are some paths along with highways can be built. Every path is denoted by a triplet (abc) which means a highway can built between the a-th village and the b-th village with a cost of c. In order to save money, they will select only part of the paths to build highways along so that the total cost to build highways along the selected paths is minimal under the condition that every pair of villages is connected.

It is possible that multiple such selections exist. People from every village want to have those highways of good interest to them built. But some highways can never appear in the selection since they are much too costly. So people ask whether a certain highway can be selected if they agree to cut the cost. Your task is to design a program to answer their queries.

Input

The first line of input contains three integers NM and Q (1 < N ≤ 1,000, N − 1 ≤ M ≤ 100,000, 0 < Q ≤ 100,000), where N is the number of villages, M is the number of paths, and Q is the number of queries. Each of the next M lines contains three integers ab, and c (1 ≤ ab ≤ Na ≠ b, 0 ≤ c ≤ 1,000,000). The triplet (abc) describes a path. Each of following Q lines contains two integer i and x (1 ≤ i ≤ M, 0 ≤ x) describing a query, “Can a highway be built along the i-th path if the cost of is reduced to x?” x is strictly lower than the original cost of building a highway along the i-th path. It is assumed that every pair of village will be connected either directly or indirectly if all possible highways are built. And there may be more than one highway that can be built between a pair of villages.

Output

Output one line for each query. Output either “Yes” or “No” as the answer to the the query.

Sample Input

3 4 3
1 2 10
1 3 6
2 3 4
1 3 7
4 6
1 7
1 5

Sample Output

Yes
No
Yes

题意:村庄之间要修高速公路,让这n个村庄可以连通,给出可以建公路的建议,然后让你选择一些公路,其实就是最小生成树,但是询问的是如果把某条边的费用削减成x,问x是否可以有被选择的可能;

分析:首先任意选择一个最小生成树,对于没有选择的边枚举,每次加入一条边权值w,然后肯定会构成一个环,找出该环中除了这条边之外费用最大的边p;如果削减后的边w小于等于p,则可以构成最小生成树的组成成分;

程序:

#include"stdio.h"
#include"string.h"
#include"stack"
#include"math.h"
#include"algorithm"
#include"stdlib.h"
#define M 1009
#define inf 100000000
#define eps 1e-12
using namespace std;
struct node
{
    int u,v,w,next,id,mark;
}e[100009],edge[M*2];
int f[M],head[M],t,pre[M],deep[M],prep[M];
int num[100009],use[100009];
void init()
{
    t=0;
    memset(head,-1,sizeof(head));
}
void add(int u,int v,int w)
{
    edge[t].u=u;
    edge[t].v=v;
    edge[t].w=w;
    edge[t].next=head[u];
    head[u]=t++;
}
int finde(int x)
{
    if(x!=f[x])
        f[x]=finde(f[x]);
    return f[x];
}
void dfs(int u,int f,int id)
{
    deep[u]=id;
    for(int i=head[u];i!=-1;i=edge[i].next)
    {
        int v=edge[i].v;
        if(v==f)continue;
        pre[v]=u;
        prep[v]=edge[i].w;
        dfs(v,u,id+1);
    }
}
int Lca(int u,int v//Lca搜最大的边
{
    int maxi=-inf;
    while(u!=v)
    {
        if(deep[u]>deep[v])
        {
            maxi=max(maxi,prep[u]);
            u=pre[u];
        }
        else
        {
            maxi=max(maxi,prep[v]);
            v=pre[v];
        }
    }
    return maxi;
}
int cmp(node a,node b)
{
    return a.w<b.w;
}
int main()
{
    int n,m,k,i,x;
    while(scanf("%d%d%d",&n,&m,&k)!=-1)
    {
        for(i=0;i<m;i++)
        {
            scanf("%d%d%d",&e[i].u,&e[i].v,&e[i].w);
            e[i].id=i;
        }
        sort(e,e+m,cmp);
        for(i=0;i<m;i++)
            num[e[i].id]=i;
        for(i=1;i<=n;i++)
            f[i]=i;
        memset(use,0,sizeof(use));
        init();
        for(i=0;i<m;i++)
        {
            int x=finde(e[i].u);
            int y=finde(e[i].v);
            if(x!=y)
            {
                f[x]=y;
                use[i]=1;
                add(e[i].u,e[i].v,e[i].w);
                add(e[i].v,e[i].u,e[i].w);
            }
        }
        dfs(1,1,1);
        for(i=0;i<m;i++)//枚举不在最小生成树里面的边
            if(!use[i])
            e[i].mark=Lca(e[i].u,e[i].v);//记录可以成为最小生成树的边的上限
        while(k--)
        {
            scanf("%d%d",&i,&x);
            i--;
            if(use[num[i]])
                printf("Yes\n");
            else
            {
                if(x<=e[num[i]].mark)
                    printf("Yes\n");
                else
                    printf("No\n");
            }
        }
    }
    return 0;
}


posted @ 2014-08-15 19:51  一样菜  阅读(597)  评论(0编辑  收藏  举报