hdu 4324 Triangle LOVE
题目连接
http://acm.hdu.edu.cn/showproblem.php?pid=4324
Triangle LOVE
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
拓扑排序。。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 | #include<algorithm> #include<iostream> #include<cstdlib> #include<cstring> #include<cstdio> #include<vector> #include<queue> #include<map> using std::map; using std::min; using std::find; using std::pair; using std::queue; using std::vector; using std::multimap; #define pb(e) push_back(e) #define sz(c) (int)(c).size() #define mp(a, b) make_pair(a, b) #define all(c) (c).begin(), (c).end() #define iter(c) __typeof((c).begin()) #define cls(arr, val) memset(arr, val, sizeof(arr)) #define cpresent(c, e) (find(all(c), (e)) != (c).end()) #define rep(i, n) for(int i = 0; i < (int)n; i++) #define tr(c, i) for(iter(c) i = (c).begin(); i != (c).end(); ++i) const int N = 2010; const int INF = 0x3f3f3f3f; struct TopSort { struct edge { int to, next; }G[N * N]; int tot, inq[N], head[N]; inline void init() { tot = 0, cls(inq, 0), cls(head, -1); } inline void add_edge( int u, int v) { G[tot].to = v; G[tot].next = head[u]; head[u] = tot++; } void built( int n) { char buf[N]; rep(i, n) { scanf ( "%s" , buf); rep(j, n) { int f = buf[j] - '0' ; if (!f) continue ; inq[j + 1]++; add_edge(i + 1, j + 1); } } } inline bool bfs( int n) { int topNum = 0; queue< int > q; rep(i, n) { if (!inq[i + 1]) { q.push(i + 1); } } while (!q.empty()) { int u = q.front(); q.pop(); topNum++; for ( int i = head[u]; ~i; i = G[i].next) { if (--inq[G[i].to] == 0) { q.push(G[i].to); } } } return topNum == n; } inline void solve( int n) { static int k = 1; init(), built(n); printf ( "Case #%d: %s\n" , k++, !bfs(n) ? "Yes" : "No" ); } }go; int main() { #ifdef LOCAL freopen ( "in.txt" , "r" , stdin); freopen ( "out.txt" , "w+" , stdout); #endif int t, n; scanf ( "%d" , &t); while (t--) { scanf ( "%d" , &n); go.solve(n); } return 0; } |
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET 9 new features-C#13新的锁类型和语义
· Linux系统下SQL Server数据库镜像配置全流程详解
· 现代计算机视觉入门之:什么是视频
· 你所不知道的 C/C++ 宏知识
· 聊一聊 操作系统蓝屏 c0000102 的故障分析
· 回顾我的软件开发经历(1)
· 不到万不得已,千万不要去外包
· C# WebAPI 插件热插拔(持续更新中)
· .NET 9 new features-C#13新的锁类型和语义
· 会议真的有必要吗?我们产品开发9年了,但从来没开过会