SDUT 3262 Colorful Cupcakes (2015年山东省第六届ACM大学生程序设计竞赛)

本文介绍了一个涉及社交网络中朋友关系的算法问题,并通过Tarjan算法求解强连通分量来找到最少请客次数帮助主角获得目标人物的帮助。文章详细解释了Tarjan算法的原理及其在解决此类问题的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Circle of Friends

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

题目描述

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

分析:网上题解都说是模板题,就学了学这个模板,Tarjan求强连通分量(环)+缩点+DFS求连通性,等明白透彻了再来补充吧

代码如下:

#include <stdio.h>
#include <string.h>
#include <vector>
#include <stack>
#define MAX 100010
using namespace std;
int n,m;
stack <int>s;
vector <int>map[MAX];
vector <int>new_map[MAX];
int low[MAX],dfn[MAX];
bool isStack[MAX];
int used[MAX],newflag,top;
int flag;

void init()
{
	int i;
	for(i=0;i<MAX;i++)
		map[i].clear();
	for(i=0;i<MAX;i++)
		new_map[i].clear();
	memset(isStack,0,sizeof(isStack));
	memset(used,0,sizeof(used));
	memset(dfn,0,sizeof(dfn));
	memset(low,0,sizeof(low));
	while(!s.empty())
		s.pop();
	newflag=0;top=0;
}

void tarjan(int u)
{
	dfn[u]=low[u]=++top;
	isStack[u]=true;
	s.push(u);
	for(int x=0;x<map[u].size();x++)
	{
		int v=map[u][x];
		if(!dfn[v])
		{
			tarjan(v);
			if(low[v]<low[u])
				low[u]=low[v];
		}
		else if(isStack[v] && dfn[v]<low[u])
			low[u]=dfn[v];
	}
	if(low[u]==dfn[u])
	{
		newflag++;
		int x;
		do{
			x=s.top();
			isStack[x]=false;
			used[x]=newflag;
			s.pop();
		}while(x!=u);
	}
	return ;
}

int dfs(int u)  
{//判断连通性 
    dfn[u]=1;  
    if(u==used[n-1])  
    {  
        flag=1;  
        return 0;  
    }  
    int ans=0x3f3f3f3f;  
    for(int i=0; i<(int)new_map[u].size(); ++i)  
        ans=min(ans,dfs(new_map[u][i])+1);  
    return ans;
}

int main()
{
	int T;
	int i,j;
	int a,b;
	scanf("%d",&T);
	while(T--)
	{
		init();
		scanf("%d %d",&n,&m);
		for(i=0;i<m;i++)
		{
			scanf("%d %d",&a,&b);
			map[a].push_back(b);						
		}
		for(i=0;i<n;i++)
		{//求有向强连通 
			if(!dfn[i])
				tarjan(i);
		}
		//缩点 
		for(i=0; i<n; ++i)  
            for(j=0; j<(int)map[i].size(); ++j)
                if(used[i]!=used[map[i][j]])  
                    new_map[used[i]].push_back(used[map[i][j]]);
        flag=0;
        int ans=dfs(used[0]);
        if(flag)
			printf("%d\n",ans);  
        else
			printf("-1\n");

	}
	return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值