山东省第六届ACM程序设计竞赛

Circle of Friends

  2000 ms         65536 KiB
Submit Status My Status  Origin

Description

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.

Input

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.

Output

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.

Sample

Input

Copy3 
4 4 
0 1
1 2 
2 1 
2 3  

3 3 
0 1 
1 2 
2 1 
 
3 1 
0 1

Output

Copy2 
1 
-1
Submit Status My Status  Origin

看懂题目之后就觉得是一个板子题目,但是对于板子不熟悉,导致了一个点问题,那个low和dfn数组玄学存的啥

判断是否在一个强联通分块还是用Scc数组

题意:给你n个人和m个关系,问你最后0可以到n-1不

但是有个前提,假如某几个点成环,那么他们之间是不需要花费什么就可以问问题,如果不是成环的哪种真朋友关系就需要花费1来问问题问你最少花费是多少,如果不能到输出-1.  tarjan缩点,跑一下bfs就行,也可以缩点之后,同一个强联通分块距离改成0 其余都是1 然后跑一下spfa也行。

#include<bits/stdc++.h> 
using namespace std;
const int MAX=100005; 
struct node{  
    int s,t,next;  
}e[MAX]; 
struct Node{
	int v;
	int step;
}; 
int head[MAX],cnt;  
void add(int u,int v)  
{  
    e[cnt]=node{u,v,head[u]};  
    head[u]=cnt++;  
}  
int dfn[MAX];  //每个节点的访问时间编号  
int low[MAX];  //每个点能到达的最小编号  
int sta[MAX],top;  
int Scc[MAX];  //每个点所属的分量 序号  
int vis[MAX];
void tardfs(int u,int &lay,int &sig)  
{  
    low[u]=dfn[u]=lay++;  //到达此点的时间  
    sta[top++]=u;  //压入栈  
    for(int i=head[u];~i;i=e[i].next)  
    {  
        int t=e[i].t;  
        if(dfn[t]==0)  
        {  
            tardfs(t,lay,sig);  
            low[u]=min(low[u],low[t]);  
        }  
        else if(!Scc[t])//访问过了  
            low[u]=min(low[u],dfn[t]);//强连通求法可以用low  
    }  
    if(low[u]==dfn[u])//u不能到达任何之前走过的点  
    {  
        sig++;  
        while(1)  
        {  
            int j=sta[--top];//出栈  
            Scc[j]=sig;  
            if(j==u)
				break;  
        }//包含u的连通分量出栈  
    }  
}  
int tarjan(int n)  
{  
    int sig=0;//强连通数  
    int lay=1;//时间戳  
    top=0;  
    memset(Scc,0,sizeof(Scc));  
    memset(dfn,0,sizeof(dfn));//时间戳归0  
    for(int i=0;i<n;i++)  
    {  
        if(!dfn[i])
			tardfs(i,lay,sig);  
    }  
    return sig;//返回连通数  
}  
int main()  
{  
    int n,m,u,v,T;  
    cin>>T;  
    while(T--)  
    {  
        cin>>n>>m;  
        memset(head,-1,sizeof(head));  
        memset(vis,0,sizeof(vis));
        memset(low,0,sizeof(low));
        memset(sta,0,sizeof(low));
        memset(e,0,sizeof(e));
        cnt=0;  
        int flag=0;
        while(m--)  
        {  
            scanf("%d%d",&u,&v);  
            add(u,v); 
			if(u==0 && v==n-1)
				flag=1; 
        }  
        int sig=tarjan(n); 
           queue<Node>q;
           vis[0]=1;
		   int ans=1e9;
		   Node t;
		   t.v=0;
		   t.step=0;
		   q.push(t);
		   while(!q.empty())
		   {
		   		Node temp=q.front();
		   		q.pop();
		   		for(int i=head[temp.v];i!=-1;i=e[i].next)
		   		{
		   			int v=e[i].t;
		   			if(v==(n-1))
		   			{
		   				if(Scc[temp.v]==Scc[v])
		   					ans=min(ans,temp.step);
		   				else
		   					ans=min(ans,temp.step+1);
						continue;
					}
		   			if(vis[v]==0)
		   			{
					   	vis[v]=1;
			   			if(Scc[temp.v]==Scc[v])
			   			{
			   				Node la;
			   				la.step=temp.step;
			   				la.v=v;
			   				q.push(la);
						}
						else
						{
							Node la;
							la.step=temp.step+1;
							la.v=v;
							q.push(la); 
						}
					}
				}
		   }
		    if(ans==1e9)
		   		printf("-1\n");
		   	else
		   		printf("%d\n",ans);
    } 
	return 0; 
} 
#include<bits/stdc++.h> //spfa
using namespace std;
const int MAX=100005; 
const int INF=0x3f3f3f3f;
struct node{  
    int s,t,next;  
    int dist;
}e[MAX]; 
struct Node{
	int v;
	int step;
}; 
int head[MAX],cnt,n;  
void add(int u,int v)  
{  
    e[cnt]=node{u,v,head[u],1};  
    head[u]=cnt++;  
}  
int dfn[MAX];  //每个节点的访问时间编号  
int low[MAX];  //每个点能到达的最小编号  
int sta[MAX],top;  
int Scc[MAX];  //每个点所属的分量 序号  
int vis[MAX];
int dis[MAX];
void tardfs(int u,int &lay,int &sig)  
{  
    low[u]=dfn[u]=lay++;  //到达此点的时间  
    sta[top++]=u;  //压入栈  
    for(int i=head[u];~i;i=e[i].next)  
    {  
        int t=e[i].t;  
        if(dfn[t]==0)  
        {  
            tardfs(t,lay,sig);  
            low[u]=min(low[u],low[t]);  
        }  
        else if(!Scc[t])//访问过了  
            low[u]=min(low[u],dfn[t]);//强连通求法可以用low  
    }  
    if(low[u]==dfn[u])//u不能到达任何之前走过的点  
    {  
        sig++;  
        while(1)  
        {  
            int j=sta[--top];//出栈  
            Scc[j]=sig;  
            if(j==u)
				break;  
        }//包含u的连通分量出栈  
    }  
}  
int tarjan(int n)  
{  
    int sig=0;//强连通数  
    int lay=1;//时间戳  
    top=0;  
    memset(Scc,0,sizeof(Scc));  
    memset(dfn,0,sizeof(dfn));//时间戳归0  
    for(int i=0;i<n;i++)  
    {  
        if(!dfn[i])
			tardfs(i,lay,sig);  
    }  
    return sig;//返回连通数  
}  
void spfa()
{  
    memset(dis,INF,sizeof(dis)); 
	memset(vis,0,sizeof(vis)); 
    queue<int> Q;  
    Q.push(0);  
    vis[0]=1;  
    dis[0]=0;  
    while (!Q.empty())
	{  
        int u=Q.front();  
        Q.pop();  
        vis[u]=0;  
        for(int i=head[u];i!=-1;i=e[i].next)
		{  
            int v=e[i].t;  
            if(dis[v]>dis[u]+e[i].dist)
			{  
                dis[v]=dis[u]+e[i].dist;  
                if (!vis[v])
				{  
                    vis[v]=1;  
                    Q.push(v);  
                }  
            }  
        }  
    }  
    if (dis[n-1]==INF) printf ("-1\n");  
    else printf ("%d\n",dis[n-1]);  
}  
int main()  
{  
    int m,u,v,T;  
    cin>>T;  
    while(T--)  
    {  
        cin>>n>>m;  
        memset(head,-1,sizeof(head));  
        memset(vis,0,sizeof(vis));
        memset(low,0,sizeof(low));
        memset(sta,0,sizeof(low));
        memset(e,0,sizeof(e));
        cnt=0;  
        int flag=0;
        while(m--)  
        {  
            scanf("%d%d",&u,&v);  
            add(u,v); 
			if(u==0 && v==n-1)
				flag=1; 
        }  
        int sig=tarjan(n); 
        for(int i=0;i<cnt;i++)
        {
        	int u=e[i].s;
        	int v=e[i].t;
        	if(Scc[u]==Scc[v])
        		e[i].dist=0;
		}
		spfa();
    } 
	return 0; 
}  


猜你喜欢

转载自blog.csdn.net/passer__/article/details/80174657