2012 Multi-University Training Contest 3:Triangle LOVE_判断有向图中是否含有仅由三个点组成的环

ains a single integer t (1 <= t <= 15), the number of test cases.
For each case, the first line contains one integer N (0 < N <= 2000).
In the next N lines contain the adjacency matrix A of the relationship (without spaces). A i,j = 1 means i-th people loves j-th people, otherwise A i,j = 0.
It is guaranteed that the given relationship is a tournament, that is, A i,i= 0, A i,j ≠ A j,i(1<=i, j<=n,i≠j).
 

Output
For each case, output the case number as shown and then print “Yes”, if there is a “Triangle Love” among these N people, otherwise print “No”.
Take the sample output for more details.
 

Sample Input
  
  
2 5 00100 10000 01001 11101 11000 5 01111 00000 01000 01100 01110
 

Sample Output
  
  
Case #1: Yes Case #2: No
 

Author
Zhaozhouyang
 

Source
 

Recommend
zhoujiaqi2010

//题意:给定一个有向图并规定:每两个点之间一定有边,同时A指向B则B定不能指向A,反之亦然。 询问是否存在仅有三个点构成的环。方法有很多种。
//法1:首先判断有向图中是否存在环马上有tarjan能够很好的解决。并且如果存在大于三个点以上的环的话肯定存在仅有三个点构成的环。 因为每两个点之间都有边,并且只有一个给定的指向,画几个图便可以推导出这样的结论,而在杭电人人上提供的解题报告下面评论也有人对上面的说法提出了证明:  假设存在一个n元环,因为a->b有边,b->a必定没边,反之也成立 所以假设有环上三个相邻的点a-> b-> c,那么如果c->a间有边,就已经形成了一个三元环,如果c->a没边,那么a->c肯定有边,这样就形成了一个n-1元环。。。。 所以只需证明n为4时一定有三元环即可,显然成立。 所以知道在tarjan后或中判断是否存在大于等于三个点的强连通图。复杂度为边数即O(n*n)。
//法2:其实还有一个更好的方法,因为每两个点一定有且仅有一条指向边,那么先假定点按顺序1,2,....n,每个点一定指向后面的点,即1指向2,3,4.....n,2指向3,4,5....n。。。。。(设为【元图】)那么每个点的出度分别为n-1,n-2,n-3,....0,而此时任意反向一条边都能够得到一个环,因为反向了一条边,则这条边的两个的出度一个加1一个减1,使得所有点中一定存在两个点的出度相同。那么对于其他有向图都可以由【元图】反向某些边得到,所以只要用反证法,如果所有点里面没有两个点的出度相同则肯定不存在环,否则存在。时间复杂度O(n);
//tarjan 判断是否存在大于等于三个点的强连通图
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<cstdlib>
#include<queue>
#include<map>
#include<stack>
#include<vector>
#include<ctime>
using namespace std;
#define N 2005
#define M 3000005
int p[N],low[N],pre[N],ans[N],s[N],tear,cur,num,e,sa;
bool f[N];
char str[N][N];
struct node
{
	int to,next;
}T[M];
void edge(int u,int v)
{
	T[e].to=v;
	T[e].next=p[u];
	p[u]=e++;
}
void tarja(int n)
{
	int k,i;
	low[n]=pre[n]=++cur;
	s[tear++]=n;
	f[n]=1;
	for(i=p[n];i!=-1;i=T[i].next)
	{
		k=T[i].to;
		if(!pre[k])
		{
			tarja(k);
			if(sa)return ;
			low[n]=min(low[n],low[k]);
		}
		if(f[k])
			low[n]=min(low[n],pre[k]);
	}	
	if(low[n]==pre[n])
	{
		num++;
		while(1)
		{
			k=s[--tear];
			f[k]=0;
			ans[num]++;
			if(ans[num]>=3){sa=1;return ;}  //存在大于等于三个点的强连通
			if(k==n)
				break;
		}
	}
	if(sa)return ;
}
int main()
{
	int i,j,k,n,m,T;
	scanf("%d",&T);
	char c;
	int t=1;
	while(T--)
	{
		sa=0;
		scanf("%d",&n);
		for(i=1;i<=n;i++)
			pre[i]=low[i]=ans[i]=f[i]=0,p[i]=-1;
		e=0;
		memset(f,0,sizeof(f));
		tear=num=cur=0;
		for(i=1;i<=n;i++)
			scanf("%s",str[i]+1);
		for(i=1;i<=n;i++)
			for(j=1;j<=n;j++)
				if(str[i][j]=='1')
					edge(i,j);
		printf("Case #%d: ",t++);
		for(i=1;i<=n;i++)
		{
			if(!pre[i])tarja(i);
			if(sa)break;
		}
		if(sa)printf("Yes\n");
		else printf("No\n");
	}
	return 0;
}

//判断是否不存在两个点的出度相同
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<cstdlib>
#include<queue>
#include<map>
#include<stack>
#include<vector>
#include<ctime>
using namespace std;
#define N 2500

int out[N];
char str[N][N];
int main()
{
	int i,j,k,n,m,T;
	scanf("%d",&T);
	char c;
	int t=1;
	while(T--)
	{
		scanf("%d",&n);
		memset(out,0,sizeof(out));
		for(i=0;i<n;i++)
			scanf("%s",str[i]);
		for(i=0;i<n;i++)
			for(j=0;j<n;j++)
				if(str[i][j]=='1')
					out[i]++;
		sort(out,out+n);
		bool flag=false;
		for(i=0;i<n-1;i++)
			if(out[i]==out[i+1])
			{
				flag=true;
				break;
			}
		printf("Case #%d: ",t++);
		printf("%s\n",flag?"Yes":"No");
	}
	return 0;
}	



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值