码迷,mamicode.com
首页 > 编程语言 > 详细

杭电4324--Triangle LOVE(拓扑排序)

时间:2015-08-10 19:29:35      阅读:166      评论:0      收藏:0      [点我收藏+]

标签:

Triangle LOVE

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 3479    Accepted Submission(s): 1350


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
 

 

Author
BJTU
 

 

Source
 

 

Recommend
zhoujiaqi2010   |   We have carefully selected several similar problems for you:  1068 4320 4325 3309 3712 
RE:三角恋 → → 判断是否成环。
 1 #include <queue>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <iostream>
 5 using namespace std;
 6 char map[2020][2020]; 
 7 int indegree[2020];
 8 int n; 
 9 bool Solve()
10 {
11     queue<int> q;
12     for(int i = 0; i < n; i++)
13     {
14         if(indegree[i] == 1)
15             q.push(i);
16     }
17     int num = 0;
18     while(!q.empty())
19     {
20         int temp = q.front();
21         q.pop();
22         num++;
23         for(int i = 0; i < n; i++)
24         {
25             if(map[temp][i])
26             {
27                 indegree[i]--;
28                 if(indegree[i] == 0)    
29                 q.push(i);
30             }
31         } 
32     }
33     if(num == n)
34         return false;
35     else
36         return true;
37 } 
38 int main()
39 {
40     int t, temp = 1;
41     scanf("%d", &t);
42     while(t--)
43     {
44         scanf("%d", &n);
45         memset(indegree, 0, sizeof(indegree));
46         for(int i = 0; i < n; i++)
47         {
48             scanf("%s", map[i]);
49             for(int j = 0; j < n; j++)
50                 if(map[i][j] == 1)
51                     indegree[j]++;
52         }
53         if(Solve())
54             printf("Case #%d: Yes\n", temp++);
55         else
56             printf("Case #%d: No\n", temp++);
57     } 
58     return 0;    
59 }  

 

技术分享
 1 #include <cstdio>
 2 #include <cstring> 
 3 #include <iostream>
 4 using namespace std;
 5 char map[2020][2020]; int indegree[2020];
 6 int main()
 7 {
 8     int t, temp = 1;
 9     scanf("%d", &t);
10     while(t--)
11     {
12         int n;
13         scanf("%d", &n);
14         bool flag = false;
15         memset(indegree, 0, sizeof(indegree));
16         for(int i = 0; i < n; i++)   // 计算每个点入度; 
17         {
18             scanf("%s", map[i]);
19             for(int j = 0; j < n; j++)
20                 if(map[i][j] == 1)
21                     indegree[j]++;
22         }
23         for(int i = 0; i < n; i++)
24         {
25             int j;
26             for(j = 0; j < n; j++)
27                 if(indegree[j] == 0)
28                     break;
29             if(j == n)      //没有入度为零的点, 存在环。 
30             {
31                 flag = true;
32                 break;    
33             }    
34             else
35             {
36                 indegree[j]--;
37                 for(int  k = 0; k < n; k++)    //与 入度为0的点相接点的入度-1; 
38                 {
39                     if(map[j][k] == 1 && indegree[k] != 0)
40                         indegree[k]--;
41                 }
42             }
43         } 
44         if(flag)
45             printf("Case #%d: Yes\n", temp++);
46         else
47             printf("Case #%d: No\n", temp++);
48     }
49     return 0;    
50 } 
数组模拟

 

 
 

杭电4324--Triangle LOVE(拓扑排序)

标签:

原文地址:http://www.cnblogs.com/fengshun/p/4718866.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!