拓扑排序/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回溯)
题目链接: 啊哈哈.点我点我 题意是: 第一列给出全部的字母数,第二列给出一些先后顺序. 然后按字典序最小的方式输出全部的可能性.. . 思路: 整体来说是拓扑排序.可是又非常多细节要考虑.首先要按字 ...
随机推荐
- Highways POJ 2485【Prim】
Description The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has no public h ...
- Deepin-我为什么推荐它!
针对Win上的开发软件,大部分都需要密匙或者破解,而Deepin不敢说一应俱全,但全沾边是没问题的 无论是编程.娱乐还是其它的,基本上都可以做到,而且它还应用了Crossover来兼容大部分的Win软 ...
- Android与设计模式——代理(Proxy)模式
在阎宏博士的<JAVA与模式>一书中开头是这样描写叙述代理(Proxy)模式的: 代理模式是对象的结构模式.代理模式给某一个对象提供一个代理对象,并由代理对象控制对原对象的引用. 代理模式 ...
- python的线程thread笔记
python的线程是用thread和threading来实现的.其中利用threading会更好,因为thread没有线程保护,当主线程退出了之后,子线程也会被强行退出.threading支持守护线程 ...
- C++问题记录
问题idx: 1) 怎么在VS2010下新建一个像VC6.0 中那样的控制台C++程序. cdate: 2014-4-24 A1: VC6.0 对标准C++集的支持不是太好, VS2010也有一些吧, ...
- 3 TypeScript 语法特性
一.类型注解(Type annotations) TypeScript 通过类型注解提供静态类型以在编译时启动类型检查,简单来说,就是指定数据类型,它会在代码运行的时候,对传入的数据进行数据类型匹配检 ...
- git 删除目录
1. 查看本地已经被删除的文件 2. 删除 目录以及目录下的文件 [root@test01 h2_mopub_replace]# git rm ../test_code_driver -r 3. [r ...
- HQL语句详解
4.3 使用HQL查询 Hibernate提供了异常强大的查询体系,使用Hibernate有多种查询方式.可以选择使用Hibernate的HQL查询,或者使用条件查询,甚至可以使用原生的SQL查询语句 ...
- Element is not clickable at point SeleniumWebdriverException
Element is not clickable at point SeleniumWebdriverException | Selenium Easy http://www.seleniumeasy ...
- APP界面常用的五种颜色搭配
众所周知,每一种颜色带给用户的视觉感受也是不同的.现在人们对手机的依赖程度,就能看到手机中APP的发展前景,那今天就跟大家聊聊如何通过颜色搭配的不同来进行移动端APP界面的布局和排版设计.移动端UI界 ...