拓扑排序/DFS HDOJ 4324 Triangle LOVE
题意:判三角恋(三元环)。如果A喜欢B,那么B一定不喜欢A,任意两人一定有关系连接
分析:正解应该是拓扑排序判环,如果有环,一定是三元环,证明。 DFS:从任意一点开始搜索,搜索过的点标记,否则超时。看是否存在两点路程只差等于2,如果存在,则说明有上述的三角环。其他做法。
收获:DFS搜索一定要用vis数组啊,否则很容易超时的
代码(拓扑排序):
/************************************************
* Author :Running_Time
* Created Time :2015-8-25 19:24:24
* File Name :E_topo.cpp
************************************************/ #include <cstdio>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <cstring>
#include <cmath>
#include <string>
#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <list>
#include <map>
#include <set>
#include <bitset>
#include <cstdlib>
#include <ctime>
using namespace std; #define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
typedef long long ll;
const int N = 2e3 + 10;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
char s[N][N];
vector<int> G[N];
int in[N];
int n; bool Topo_Sort(void) {
memset (in, 0, sizeof (in));
for (int i=1; i<=n; ++i) {
for (int j=0; j<G[i].size (); ++j) in[G[i][j]]++;
}
queue<int> Q; int cnt = 0;
for (int i=1; i<=n; ++i) if (!in[i]) Q.push (i);
while (!Q.empty ()) {
int u = Q.front (); Q.pop ();
cnt++;
for (int i=0; i<G[u].size (); ++i) {
int v = G[u][i];
if (!(--in[v])) Q.push (v);
}
}
if (cnt == n) return false;
else return true;
} int main(void) {
int T, cas = 0; scanf ("%d", &T);
while (T--) {
scanf ("%d", &n);
for (int i=1; i<=n; ++i) G[i].clear ();
for (int i=1; i<=n; ++i) {
scanf ("%s", s[i] + 1);
for (int j=1; j<=n; ++j) {
if (s[i][j] == '1') G[i].push_back (j);
}
} printf ("Case #%d: %s\n", ++cas, (Topo_Sort () ? "Yes" : "No"));
} return 0;
}
代码(DFS):
/************************************************
* Author :Running_Time
* Created Time :2015-8-25 9:44:18
* File Name :E.cpp
************************************************/ #include <cstdio>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <cstring>
#include <cmath>
#include <string>
#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <list>
#include <map>
#include <set>
#include <bitset>
#include <cstdlib>
#include <ctime>
using namespace std; #define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
typedef long long ll;
const int N = 2e3 + 10;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
char s[N][N];
bool vis[N];
int d[N];
int n; bool DFS(int u) {
for (int i=1; i<=n; ++i) {
if (s[u][i] == '1') {
if (vis[i] && d[u] == d[i] + 2) return true;
else if (!vis[i]) {
vis[i] = true; d[i] = d[u] + 1;
if (DFS (i)) return true;
}
}
}
return false;
} int main(void) {
int T, cas = 0; scanf ("%d", &T);
while (T--) {
scanf ("%d", &n);
memset (vis, false, sizeof (vis));
memset (d, 0, sizeof (d));
for (int i=1; i<=n; ++i) {
scanf ("%s", s[i] + 1);
} vis[1] = true;
printf ("Case #%d: %s\n", ++cas, (DFS (1) ? "Yes" : "No"));
} return 0;
}
拓扑排序/DFS HDOJ 4324 Triangle LOVE的更多相关文章
- ACM/ICPC 之 拓扑排序+DFS(POJ1128(ZOJ1083)-POJ1270)
两道经典的同类型拓扑排序+DFS问题,第二题较第一题简单,其中的难点在于字典序输出+建立单向无环图,另外理解题意是最难的难点,没有之一... POJ1128(ZOJ1083)-Frame Stacki ...
- 拓扑排序+DFS(POJ1270)
[日后练手](非解题) 拓扑排序+DFS(POJ1270) #include<stdio.h> #include<iostream> #include<cstdio> ...
- hdoj 4324 Triangle LOVE【拓扑排序判断是否存在环】
Triangle LOVE Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Tot ...
- hdoj 4324 Triangle LOVE 【拓扑】
Triangle LOVE Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) To ...
- 拓扑排序-DFS
拓扑排序的DFS算法 输入:一个有向图 输出:顶点的拓扑序列 具体流程: (1) 调用DFS算法计算每一个顶点v的遍历完成时间f[v] (2) 当一个顶点完成遍历时,将该顶点放到一个链表的最前面 (3 ...
- Ordering Tasks(拓扑排序+dfs)
Ordering Tasks John has n tasks to do. Unfortunately, the tasks are not independent and the executio ...
- HDU 5438 拓扑排序+DFS
Ponds Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Sub ...
- POJ1128 Frame Stacking(拓扑排序+dfs)题解
Description Consider the following 5 picture frames placed on an 9 x 8 array. ........ ........ ... ...
- poj1270Following Orders(拓扑排序+dfs回溯)
题目链接: 啊哈哈.点我点我 题意是: 第一列给出全部的字母数,第二列给出一些先后顺序. 然后按字典序最小的方式输出全部的可能性.. . 思路: 整体来说是拓扑排序.可是又非常多细节要考虑.首先要按字 ...
随机推荐
- datatable使用介绍
Datatables是一款jquery表格插件.它是一个高度灵活的工具,可以将任何HTML表格添加高级的交互功能. 1.支持分页:前台分页和后台分页 前台分页:后台一次把数据传过来,交给前端渲染.缺点 ...
- Ulua_toLua_基本案例(一)
Ulua_toLua_基本案例 在Untiy中用Lua.必需要LuaInterface.LuaInterface的介绍请看:点击打开链接 能够先光写Lua,生成.lua的纯文件.再Unity中通过,l ...
- [Analytics] Add Tealium debugger in Chrome
It would be helpful once you can see what information have been tracking inside you web application, ...
- PDO防止SQL注入具体介绍
<span style="font-size:18px;"><?php $dbh = new PDO("mysql:host=localhost; db ...
- Angular2.x-主/细节组件
此刻,HeroesComponent显示heroes列表和所选heroes的详细信息. 随着应用程序的增长保持一个组件中的所有功能将不可维护.您需要将大型组件分成更小的子组件,每个组件都专注于特定的任 ...
- 打造极致性能数据库中间件丨LVS+Keepalive+华为云DDM之理论篇
背景说明 华为云分布式数据库中间件(Distributed Database Middleware,简称DDM),专注于解决数据库分布式扩展问题,突破了传统数据库的容量和性能瓶颈,实现海量数据高并发访 ...
- 链接脚本在编程中的高级运用之二——执行时库和C++特性支持
我们在链接脚本在编程中的高级运用之中的一个可变长数组中已经讲述了编译链接的原理,并且以uboot命令为例具体介绍链接脚本怎样实现可变长数组. 本章在前者的基础上继续讲述链接脚本在执行时库中的高级应用技 ...
- oracle sql 超长报ORA-01460错误
程序查找数据的时候报错了: ORA-01460: 转换请求无法实施或不合理 这是什么鬼?不合理你就提嘛,报错干什么. 程序原本好好的,现在突然报错了.数据库并没有什么更改. 后来猜测是因为执行的SQL ...
- 设计模式-(10)观察者模式 (swift版)
一,概念 观察者(Observer)模式又名发布-订阅(Publish/Subscribe)模式.GOF给观察者模式如下定义:定义对象间的一种一对多的依赖关系,当一个对象的状态发生改变时,所有依赖于它 ...
- Java 内部类理解
为什么使用内部类? 答:每个内部类都能独立地继承一个(接口的)实现,所以无论外围类是否已经继承了某个(接口的)实现,对于内部类都没有影响. 内部类有哪些? 答:内部类一般来说包括这四种:成员内部类.局 ...