hdu5385(2015多校8)--The path(贪心,搜索)

题目链接:点击打开链接

题目大意:给出一个有向图,求1到其它点的最短距离,要求dis[1] < dis[2]....dis[x] ... > dis[n-1] > dis[n] (1 < x <= n)

给出符合条件的每条边的边长(1<=边长<=n)

设置vis标记点是否已经存在,从左边开始逐个搜索,如果点已经存在那么继续遍历,否则换方向遍历(从右开始遍历),因为题目保证存在输出,所以一定可以遍历完所有的。

按照被遍历到的时间也就是dis的值,然后在按照(u,v) = abs(dis[u]-dis[v]),推出边的长度。

注意边的长度是1到n,如果值是0,也是设为n

#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std ;
#define maxn 100010
struct node{
    int u , v , next ;
}edge[maxn] , p[maxn] ;
int head[maxn] , cnt ;
int vis[maxn] , dis[maxn] , num , n , l , r ;
void add(int u,int v) {
    edge[cnt].v = v ; edge[cnt].next = head[u] ;
    head[u] = cnt++ ;
}
void solve(int &i,int k) {
    while( i > 0 && i <= n ) {
        if( !vis[i] ) return ;
        if( dis[i] != -1 ) return ;
        dis[i] = num++ ;
        for(int j = head[i] ; j != -1 ; j = edge[j].next) {
            vis[edge[j].v] = 1 ;
        }
        i += k ;
    }
}
int main() {
    int t , m , v , i , j , k ;
    //freopen("1006.in","r",stdin) ;
    //freopen("111.out","w",stdout) ;
    scanf("%d", &t) ;
    while( t-- ) {
        scanf("%d %d", &n, &m) ;
        memset(head,-1,sizeof(head)) ;
        memset(vis,0,sizeof(vis)) ;
        memset(dis,-1,sizeof(dis)) ;
        cnt = 0 ;
        for(i = 0 ; i < m ; i++) {
            scanf("%d %d", &p[i].u, &p[i].v) ;
            add(p[i].u,p[i].v) ;
        }
        vis[1] = 1 ;
        l = 1 ; r = n ;
        num = 0 ;
        while(l <= r) {
            solve(l,1) ;
            solve(r,-1) ;
        }
        for(i = 0 ; i < m ; i++) {
            k = abs(dis[ p[i].u ]-dis[ p[i].v ]) ;
            if( !k ) k = n ;
            printf("%d\n", k ) ;
        }
    }
    return 0 ;
}

版权声明:转载请注明出处:http://blog.csdn.net/winddreams

时间: 2024-08-09 01:57:36

hdu5385(2015多校8)--The path(贪心,搜索)的相关文章

hdu5353(2015多校6)--Average(贪心)

题目链接:点击打开链接 题目大意:有n个人围城一个环,每一个人手里都有一些糖果,第i个人有ai块. 现在有三种操作: 第i个人给第i+1个人一块.如果i有 第i+1个人给第i个人一块.如果i+1有 什么都不做. 第i个人和第i+1个人之间,可以选择一种操作并执行,问最终能不能让所有人手里的糖相等. 当n = 1 时,永远是YES 当n = 2 时,注意1和2之间只能有一种操作,不存在循环. 当n > 2 时: 糖的总数不能均分到n个人手中,直接NO 可以分开,存在一个循环,那么枚举第1个人对第2

HDU 5355 Cake(2015多校第六场,搜索 + 剪枝)

Cake Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 965    Accepted Submission(s): 119 Special Judge Problem Description There are m soda and today is their birthday. The 1-st soda has prepa

hdu5289||2015多校联合第一场1002贪心+RMQ

http://acm.hdu.edu.cn/showproblem.php?pid=5289 Problem Description Tom owns a company and he is the boss. There are n staffs which are numbered from 1 to n in this company, and every staff has a ability. Now, Tom is going to assign a special task to

hdu5353||2015多校联合第六场1001 贪心

http://acm.hdu.edu.cn/showproblem.php?pid=5353 Problem Description There are n soda sitting around a round table. soda are numbered from 1 to n and i-th soda is adjacent to (i+1)-th soda, 1-st soda is adjacent to n-th soda. Each soda has some candies

HDU 5371 (2015多校联合训练赛第七场1003)Hotaru&#39;s problem(manacher+二分/枚举)

HDU 5371 题意: 定义一个序列为N序列:这个序列按分作三部分,第一部分与第三部分相同,第一部分与第二部分对称. 现在给你一个长为n(n<10^5)的序列,求出该序列中N序列的最大长度. 思路: 来自官方题解:修正了一些题解错别字(误 先用求回文串的Manacher算法,求出以第i个点为中心的回文串长度,记录到数组p中 要满足题目所要求的内容,需要使得两个相邻的回文串,共享中间的一部分,也就是说,左边的回文串长度的一半,要大于等于共享部分的长度,右边回文串也是一样. 因为我们已经记录下来以

HDU 5371 (2015多校联合训练赛第七场1003)Hotaru&amp;#39;s problem(manacher+二分/枚举)

pid=5371">HDU 5371 题意: 定义一个序列为N序列:这个序列按分作三部分,第一部分与第三部分同样,第一部分与第二部分对称. 如今给你一个长为n(n<10^5)的序列,求出该序列中N序列的最大长度. 思路: 来自官方题解:修正了一些题解错别字(误 先用求回文串的Manacher算法.求出以第i个点为中心的回文串长度.记录到数组p中 要满足题目所要求的内容.须要使得两个相邻的回文串,共享中间的一部分,也就是说.左边的回文串长度的一半,要大于等于共享部分的长度,右边回文串也

多校第九场:贪心+矩阵快速幂中间优化+线性递推&amp;线段树递推

HDU 4968 Improving the GPA 思路:贪心的搞吧!比赛的时候想了好久,然后才发现了点规律,然后乱搞1A. 因为贪心嘛!大的情况就是刚开始每个人的分数都是最大的最小值,即绩点4.0的最低分数85,然后最后一个数设为剩余的分数,然后如果小于60就从第一个分数补到这个分数来,然后最后一个分数还小于60,那就用第二个补--依次往下搞,那时我也不知道这样就搞出答案了,我还没证明这个对不对呢,哈哈. 小的情况:小的情况就是先假设每个人都是绩点最小的最大分数,即绩点2.0的最大分数69,

hdu 5288||2015多校联合第一场1001题

http://acm.hdu.edu.cn/showproblem.php?pid=5288 Problem Description OO has got a array A of size n ,defined a function f(l,r) represent the number of i (l<=i<=r) , that there's no j(l<=j<=r,j<>i) satisfy ai mod aj=0,now OO want to know ∑i

hdu5348(2015多校5)--MZL&#39;s endless loop(搜索)

题目链接:点击打开链接 题目大意:给出n个点,m条无向边,现在要求将无向边变为有向边,要保证每个点的出度和入度的差不超过1 直接进行搜索,对每个点进行出度和入度的判断,如果出度大,就先进行反向的搜索(每搜索一条边u,v就认为这是一条v到u的有向边),反之,进行正向搜索(每搜到一条边u,v认为这是一条u到v的有向边),一直搜索到找不到边能继续为止. 注意: 1.已经使用过的边为了防止再次被遍历,可以修改head,head[u] = edge[i].next 2.注意自环,因为搜索是判断能不能继续向

hdu5379||2015多校联合第7场1011 树形统计

http://acm.hdu.edu.cn/showproblem.php? pid=5379 Problem Description Little sun is an artist. Today he is playing mahjong alone. He suddenly feels that the tree in the yard doesn't look good. So he wants to decorate the tree.(The tree has n vertexs, i