Triangle LOVE

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)

Total Submission(s): 3574    Accepted Submission(s): 1401

Problem Description

Recently, scientists find that there is love between any of two people. For example, between A and B, if A don’t love B, then B must love A, vice versa. And there is no possibility that two people love each other, what a crazy world!

Now, scientists want to know whether or not there is a “Triangle Love” among N people. “Triangle Love” means that among any three people (A,B and C) , A loves B, B loves C and C loves A.

Your problem is writing a program to read the relationship among N people firstly, and return whether or not there is a “Triangle Love”.

Input

The first line contains 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). Ai,j = 1 means i-th people loves j-th people, otherwise Ai,j = 0.

It is guaranteed that the given relationship is a tournament, that is, Ai,i= 0, Ai,j ≠ Aj,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
#include<stdio.h>
#include<string.h>
char p[2001][2001];
int s[2001],m;
void TO(int n)
{
	int flag=0;
	int i,j,k;
	for(i=0;i<n;i++)
	{
		for(j=0;j<n;j++)
		if(s[j]==0)
		break;
		if(j==n)
		{
			flag=1;
			break;
		}
		s[j]=-1;
		for(k=0;k<n;k++)
		if(p[j][k]=='1'&&s[k]!=0)
		s[k]--;
	}
	printf("Case #%d: ",++m);
	if(flag) printf("Yes\n");
	else printf("No\n");

}
int main()
{
	int t,n;
	m=0;
	scanf("%d",&t);
	while(t--)
	{
		memset(s,0,sizeof(s));
		scanf("%d",&n);
		for(int i=0;i<n;i++)
		{
			scanf("%s",p[i]);
			for(int j=0;j<n;j++)
			{
				if(p[i][j]=='1')
				s[j]++;
			}
		}
		TO(n);
	}
	return 0;
}

再贴一个代码:

#include<stdio.h>
#include<string.h>
char p[2001][2001];
int s[2001],m;
void TO(int n)
{
	int flag=0,i,j,k,w=1000000,h;
	for(i=0;i<n;i++)
	{
		for(j=0;j<n;j++)
		if(s[j]==0)
		{
			w=j;break;
		}
		else w=1000000;
		if(w==1000000)
		{
			printf("Case #%d: ",++m);
			printf("Yes\n");
			return ;
	     }
		s[w]=-1;
		for(k=0;k<n;k++)
		if(p[w][k]=='1')
		s[k]--;
	}
	printf("Case #%d: ",++m);
	printf("No\n");
	return ;
}
int main()
{
	int t,n;
	m=0;
	scanf("%d",&t);
	while(t--)
	{
		memset(s,0,sizeof(s));
		scanf("%d",&n);
		for(int i=0;i<n;i++)
		{
			scanf("%s",p[i]);
			for(int j=0;j<n;j++)
			{
				if(p[i][j]=='1')
				s[j]++;
			}
		}
		TO(n);
	}
	return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-07-28 20:37:44

Triangle LOVE的相关文章

(leetcode题解)Pascal&#39;s Triangle

Pascal's Triangle  Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Return [ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ] 题意实现一个杨辉三角. 这道题只要注意了边界条件应该很好实现出来,C++实现如下 vector<vector<int>> generate(int

Lab 1: Write a java program for the triangle problem and test the program with Junit.

Tasks: 1. Install Junit(4.12), Hamcrest(1.3) with Eclipse 将两个jar包添加到工程中 2. Install Eclemma with Eclipse 3. Write a java program for the triangle problem and test the program with Junit. [Description of triangle problem]Function triangle takes three i

Solution to Triangle by Codility

question: https://codility.com/programmers/lessons/4 we need two parts to prove our solution. on one hand, there is no false triangular. Given the array has been sorted, if A[i]+A[i+1]>A[i+2], we can prove the existence of the triangle. for array A i

LeetCode (13) Pascal&#39;s Triangle (杨辉三角 )

题目描述 Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5, Return 从第三行开始,每行除了最左边和最右边两个数为1,其他数字都是上一行中相邻两个数字之和.根据上述规则可以写出下面的代码: class Solution { public: vector<vector<int> > generateRow1() { vector<in

UVA - 11437 - Triangle Fun (计算几何~)

UVA - 11437 Triangle Fun Time Limit: 1000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu Submit Status Description Problem A Triangle Fun Input: Standard Input Output: Standard Output In the picture below you can see a triangle ABC. Point D, E

POJ 1163 The Triangle

题目链接:http://poj.org/problem?id=1163 The Triangle Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 39022   Accepted: 23430 Description 7 3 8 8 1 0 2 7 4 4 4 5 2 6 5 (Figure 1) Figure 1 shows a number triangle. Write a program that calculat

LeetCode--Pascal&#39;s Triangle

Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5, Return [ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ] class Solution { public: vector<vector<int> > generate(int numRows) { vector<vector<in

? (triangle)

2.1 题目描述 给定一个无自环重边的无向图,求这个图的三元环1的个数以及补图2的三元环个数. 2.2 输入格式 第一行 2 个数 n, m ,分别表示图的点数.边数. 接下来 m 行,每行两个数 u, v ,表示一条连接 u, v 的无向边. 2.3 输出格式 一行两个数,依次表示原图的三元环个数以及补图的三元环的个数. 2.4 样例输入 5 5 1 2 1 3 2 3 2 4 3 4 2.5样例输出 2 1 2.6数据范围 对于 30% 的数据:n ≤ 100 对于 60% 的数据:m ≤

POJ 2079 Triangle [旋转卡壳]

Triangle Time Limit: 3000MS   Memory Limit: 30000K Total Submissions: 9525   Accepted: 2845 Description Given n distinct points on a plane, your task is to find the triangle that have the maximum area, whose vertices are from the given points. Input

The Triangle

针对如下形式的ACM试题,大多出自南阳理工学院的在线ACM试题(网址: 南阳理工在线评测系统),在此非常感谢,同时也非常感谢作者的分享! 时间限制:1000 ms  |  内存限制:65535 KB 难度:4 描述 73 88 1 02 7 4 44 5 2 6 5(Figure 1)Figure 1 shows a number triangle. Write a program that calculates the highest sum of numbers passed on a ro