第6届山东省浪潮杯 Circle of Friends SDUT3262

Circle of Friends

Time Limit: 2000ms   Memory limit: 65536K  有疑问?点这里^_^

强连通缩点+BFS(找最短距离);

题目描述

Nowadays, "Circle of Friends" is a very popular social networking platform in WeChat. We can share our life to friends through it or get other‘s situation.

Similarly, in real life, there is also a circle of friends, friends would often get together communicating and playing to maintain friendship. And when you have difficulties, friends will generally come to
help and ask nothing for return.

However, the friendship above is true friend relationship while sometimes you may regard someone as your friend but he doesn‘t agree.In this way when you ask him for help, he often asks you for a meal, and
then he will help you.

If two people think they are friends mutually,they will become true friend,then once one of them has a problem or makes a query, the other one will offer help for free.What‘s more,if one relationship is similar
to “A regards B as friend, B regards C as friend and C regards A as friend”,they will make a friends circle and become true friends too with each other. Besides, people will not ask those who they don’t regard as friends for help. If one person received a
question and he can not solve it, he will ask his friends for help.

Now, Nias encounters a big problem, and he wants to look for Selina‘s help. Given the network of friends, please return the minimum number of meals Nias must offer. Of course Nias is lavish enough, so he
will pay for all the meals in the network of friends.

输入

The first line of input contains an integer T, indicating the number of test cases (T<=30).

For each test case, the first line contains two integers, N and M represent the number of friends in the Nias’s network and the number of relationships in that network. N and M are less than 100000 and you
can assume that 0 is Nias and n-1 is Selina.

Next M lines each contains two integers A and B, represent a relationship that A regards B as his friend, A and B are between 0 and n-1.

输出

For each test case, please output the minimum number of meals Nias need to offer; if Nias can’t get Selina’s help, please output -1.

示例输入

3
4 4
0 1
1 2
2 1
2 3  

3 3
0 1
1 2
2 1 

3 1
0 1

示例输出

2
1
-1

提示

来源

示例程序

#include <cstring>
#include <bits/stdc++.h>
using namespace std;
const int N=1e5+10;
const int INF =0x3f3f3f3f;
int low[N];
int DFN[N];
int Instack[N];
stack<int>s;
int Incomp[N];
vector<int>gra[N];
vector<int>newgra[N];
int numcomp,cc;
int n,m;
void init()
{
    memset(low,0,sizeof(low));
    memset(DFN,0,sizeof(DFN));
    memset(Instack,0,sizeof(Instack));
    numcomp=cc=0;
    for(int i=0;i<n;i++)
        gra[i].clear();
    while(!s.empty())
        s.pop();
        int u,v;
    scanf("%d%d",&n,&m);
    for(int i=0;i<m;i++)
    {
        scanf("%d%d",&u,&v);
        gra[u].push_back(v);
    }

}
void tarjan(int u)
{
    low[u]=DFN[u]=++cc;
    s.push(u);
    Instack[u]=1;
    int len=gra[u].size();
    int v;
    for(int i=0;i<len;i++)
    {
        v=gra[u][i];
        if(!DFN[v])
        {
            tarjan(v);
            low[u]=min(low[v],low[u]);
        }
        else if(Instack[v])
            low[u]=min(low[u],low[v]);
    }
    if(low[u]==DFN[u])
    {
        numcomp++;
        while(!s.empty())
        {
            v=s.top(),s.pop();
            Incomp[v]=numcomp;
            Instack[v]=0;
            if(u==v)
                break;
        }
    }
}
int dp(int s)
{
    if(s==Incomp[n-1])
        return 0;
    int ans=INF;
    int len=newgra[s].size();
    for(int i=0;i<len;i++)
        ans=min(ans,dp(newgra[s][i])+1);
    return ans;
}
void solve()
{
    for(int i=0;i<n;i++)
    {
        if(!DFN[i])
            tarjan(i);
    }
    if(Incomp[0]==Incomp[n-1])
    {
        printf("0\n");
        return ;
    }
    int u,v;
    for(int i=0;i<n;i++)
        newgra[i].clear();
    for(int i=0;i<n;i++)
    {
        int len=gra[i].size();
        u=Incomp[i];
        for(int j=0;j<len;j++)
        {
            v=Incomp[gra[i][j]];
            if(u!=v)
            {
                newgra[u].push_back(v);
            }
        }
    }
    int gg=dp(Incomp[0]);
    if(gg==INF)
        printf("-1\n");
    else
        printf("%d\n",gg);
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        init();
        solve();
    }
    return 0;
}
时间: 2024-08-09 14:35:31

第6届山东省浪潮杯 Circle of Friends SDUT3262的相关文章

2014山东省“浪潮杯”第五届ACM省赛总结

一次比赛做一次总结,弱菜又来总结了-- 我这种大四的又死皮赖来混省赛了,貌似就我和山大威海的某哥们(不详其大名)了吧.颁奖前和他聊天,得知他去百度了,真是不错,ORZ之. 比赛流水账: 题目目前不知道哪有,过几天填坑. 没发题目前,我们赌A题可能是水题,由于我是主coder,我去读A,剩下的一个从前往后,一个从后往前. 结果--,看到A有一个貌似是几何的图--,我还是硬头皮读了.读到一半,3分钟刷榜,发现E有出,让ZK读E,ZK先告诉我了B题题意,转而读E.B题是一个有环形关系的期望,我扫了一下

第六届华为创新杯编程大赛-进阶1第1轮

洞穴逃生 描述: 精灵王子爱好冒险,在一次探险历程中,他进入了一个神秘的山洞.在洞穴深处,精灵王子不小心触动了洞穴内暗藏的机关,整个洞穴将很快塌陷,精灵王子必须尽快逃离洞穴.精灵王子的跑步速度为17m/s,以这样的速度可能是无法逃出洞穴的.庆幸的是精灵王子拥有闪烁法术,可在1s内移动60m,不过每次使用闪烁法术都会消耗魔法值10点.精灵王子的魔法值恢复的速度为4点/s,只有处在原地休息状态时才能恢复. 现在已知精灵王子的魔法初值M,他所在洞穴中的位置与洞穴出口之间的距离S,距离洞穴塌陷的时间T.

【大坑特坑】第六届华为“创新杯”编程大赛初赛题目(第五场)

简直没法吐槽华为的服务器质量了,登一次半小时,提交一次等半小时,然后第二个题的结果还不知道呢,服务器就挂了... 题目本身倒是还挺简单的. A题是鸡兔同笼问题: 给出鸡和兔子的头数和脚数(可能不对),让你判断鸡的个数和兔子的个数..小学经典问题. 关键是不合法的情况,总共有4种. 1.头比脚多.. 2.脚是奇数= = 3.脚>4*头或者脚<2*头. so.出来了. #include <iostream> #include <cstring> #include <c

第六届华为创新杯编程大赛第二轮(2014.4.29)

第一题:外星人比数的大小 来自星星的都教授除了所有感官比地球人高出七倍,始终容颜不老以外,还拥有一项在地球人看来特别神奇的能力,他会瞬间(0.00000000000000000000001s以内)按照他的规则比较地球人熟悉的两个十进制数字的大小,他比较的规则如下:1.将要比较的两个数字分别转换成二进制数字:2.计算两个二进制数字中1的个数,个数多的数字为两者中的大者:3.负数按照其绝对值进行比较:请利用地球人发明的计算机程序逼近都教授的特异功能,实现时可以有以下约束:1.输入数据为范围在-327

hdu1212 Big Number &amp;第六届山东省赛Single Round Math (同余定理,大数取模)

题目链接:Big Number 题目大意:每次输入两个数,第一个是高精度,第二个数小于100000:求 a mod b 根据同余定理: (a+b)% c = (a%c+ b%c)%c (a*b)%c =  ( a%c* b%c)%c 所以 对于大数,例如 :123 可以这样分解 123 =  (1*10+2)*10 + 3: 123 % c =   (  (  (  1%c *  10%c)%c + 2%c) %c  * 10%c) + 3 %c  ) %c; 因此,我们用字符串处理这个数,通过

第十届山东省acm省赛补题(2)

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=4124 L Median Time Limit: 1 Second      Memory Limit: 65536 KB Recall the definition of the median of  elements where  is odd: sort these elements and the median is the -th largest element.

&amp;quot;浪潮杯&amp;quot;第六届ACM山东省省赛山科场总结

从空间拷过来的.尽管已经过去一个月了.记忆犹新 也算是又一次拾起这个blog Just begin 看着一群群大牛还有队友男神的省赛总结都出了 我最终也耐不住寂寞 来做个流水账抒抒情好了 第一次省赛 心情非常激动 也掺杂着一点小紧张 事实上最想吐槽的是伙食 真的真的真的真的吃不饱.. . 只是非常感谢我们xh老师提供的x牛和x力架 正式赛的时候根本没心思吃午饭 x牛确实提供了非常多能量(我真的没有打广告 你看 都打码了 嗯) 转回正题 说好的是来做流水账的 九号早七点集合从淄博出发 睡到了大青岛

山东省第六届“浪潮杯”ACM程序设计大赛:D:Square Number

Description: In mathematics, a square number is an integer that is the square of an integer. In other words, it is the product of some integer with itself. For example, 9 is a square number, since it can be written as 3 * 3. Given an array of distinc

【总结】&quot;浪潮杯&quot;第七届ACM山东省省赛山师场总结

翻了翻blog 还能看到去年的省赛第一战的帖子 也是ACM第一战的帖子 很感慨也很愤懑 更多的应该是遗憾.?喵个咪 哥还没退役 还能再战(图:扶老夫起来) Just second 这次的总结帖挺惭愧的.时隔一个月才写.从比完赛后到现在,几个周事情太多太多了--送行 考试 实训 送行 送行--过的我都悲观了=,= 还是正八景回忆省赛细节吧-- 老规矩,十成的总结,吃喝拉撒睡占九成,比赛占一成.精华都在那一成,想看的直接快进好嘞>> 不要点啦,那不是超链接(无奈脸 其实这次比赛挺匆忙的,因为第一天