HDU5385 图、贪心

http://acm.hdu.edu.cn/showproblem.php?pid=5385



Problem Description
You have a connected directed graph.Let  d(x)  be the length of the shortest path from  1  to  x .Specially  d(1)=0 .A graph is good if there exist  x  satisfy  d(1)<d(2)<....d(x)>d(x+1)>...d(n) .Now you need to set the length of every edge satisfy that the graph is good.Specially,if  d(1)<d(2)<..d(n) ,the graph is good too.

The length of one edge must    [1,n]

It's guaranteed that there exists solution.
 

Input
There are multiple test cases. The first line of input contains an integer  T , indicating the number of test cases. For each test case:
The first line contains two integers n and m,the number of vertexs and the number of edges.Next m lines contain two integers each,  ui  and  vi   (1ui,vin) , indicating there is a link between nodes  ui  and  vi  and the direction is from  ui  to  vi .

n3105 , m6105
1n,m105
 

Output
For each test case,print  m  lines.The i-th line includes one integer:the length of edge from  ui  to  vi
 

Sample Input
   
   
   
   
2 4 6 1 2 2 4 1 3 1 2 2 2 2 3 4 6 1 2 2 3 1 4 2 1 2 1 2 1
 

Sample Output
   
   
   
   
1 2 2 1 4 4 1 1 3 4 4 4

/**
HDU5385  图、贪心
题目大意:给定一个图,dis表示第i个点到1点的最短路,dis1=0,给有向图上的边赋权值(1~n)满足dis1<dis2<dis3<……<disk>disk+1>……disn
解题思路;思路不够活跃比赛时没有想出来,我们可以采取贪心做法,然后左边从1开始,右边从n开始,只要之前加入的点有边连向他们就加入
          这样一个点加入的时间就是他的dis值,最短路径树上的父亲也可以确定,于是输出时非树边长度为n,树边长度为两个端点dis之差
*/
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream>
#include <queue>
using namespace std;
const int maxn=100050;
int head[maxn],ip;
int fa[maxn],dis[maxn],n,m;

void init()
{
    memset(head,-1,sizeof(head));
    ip=0;
}

struct note
{
    int from,v,next;
}edge[maxn*2];

void addedge(int u,int v)
{
    edge[ip].from=u,edge[ip].v=v,edge[ip].next=head[u],head[u]=ip++;
}

void expand(int u)
{
    for(int i=head[u];i!=-1;i=edge[i].next)
    {
        int v=edge[i].v;
        if(!fa[v])fa[v]=u;
    }
}
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        init();
        scanf("%d%d",&n,&m);
        for(int i=0;i<m;i++)
        {
            int u,v;
            scanf("%d%d",&u,&v);
            addedge(u,v);
        }
        int l=1,r=n,s=1;
        memset(fa,0,sizeof(fa));
        memset(dis,0,sizeof(dis));
        fa[1]=-1;
        while(l<=r)
        {
            if(fa[l])
            {
                expand(l);
                dis[l++]=s++;
            }
            if(fa[r])
            {
                expand(r);
                dis[r--]=s++;
            }
        }
        for(int i=0;i<ip;i++)
        {
            int u=edge[i].from;
            int v=edge[i].v;
            if(fa[v]!=u)
                printf("%d\n",n);
            else
                printf("%d\n",dis[v]-dis[u]);
        }
    }
    return 0;
}


Problem Description
You have a connected directed graph.Let  d(x)  be the length of the shortest path from  1  to  x .Specially  d(1)=0 .A graph is good if there exist  x  satisfy  d(1)<d(2)<....d(x)>d(x+1)>...d(n) .Now you need to set the length of every edge satisfy that the graph is good.Specially,if  d(1)<d(2)<..d(n) ,the graph is good too.

The length of one edge must    [1,n]

It's guaranteed that there exists solution.
 

Input
There are multiple test cases. The first line of input contains an integer  T , indicating the number of test cases. For each test case:
The first line contains two integers n and m,the number of vertexs and the number of edges.Next m lines contain two integers each,  ui  and  vi   (1ui,vin) , indicating there is a link between nodes  ui  and  vi  and the direction is from  ui  to  vi .

n3105 , m6105
1n,m105
 

Output
For each test case,print  m  lines.The i-th line includes one integer:the length of edge from  ui  to  vi
 

Sample Input
    
    
    
    
2 4 6 1 2 2 4 1 3 1 2 2 2 2 3 4 6 1 2 2 3 1 4 2 1 2 1 2 1
 

Sample Output
    
    
    
    
1 2 2 1 4 4 1 1 3 4 4 4

你可能感兴趣的:(HDU5385 图、贪心)