拓扑排序/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回溯)
题目链接: 啊哈哈.点我点我 题意是: 第一列给出全部的字母数,第二列给出一些先后顺序. 然后按字典序最小的方式输出全部的可能性.. . 思路: 整体来说是拓扑排序.可是又非常多细节要考虑.首先要按字 ...
随机推荐
- 我和 HelloGitHub
我? 我是一个本科学历.无大厂经历,普通的 Python 程序员. 虽然是计算机专业,但是大学玩了四年(Dota)后,发现自己无一技能傍身,要饿死啦!偶然间接触了 Python 这门编程语言,发现编程 ...
- linux中的线程局部存储(TLS)
http://blog.csdn.net/cywosp/article/details/26469435
- 【Git使用具体解释】Egit的经常使用操作具体解释
经常使用操作 操作 说明 Fetch 从远程获取最新版本号到本地,不会自己主动merge Merge 能够把一个分支标签或某个commit的改动合并如今的分支上 Pull 从远程获取最新版本号并mer ...
- jquery 动态添加,降低input表单的方法
html代码例如以下 <html> <tr><button style="margin-left:10px" class="add_fiel ...
- SQLite Expert表分离和解决SQLite Expert删除表后大小不变的问题
最后要使用到号码归属地的查询,在网上找到一个数据库文件.大小有12M多,压缩成zip也有1.9M,这样对于一个apk的大小非常不利,后来看了一下数据库的内容,发现有非常多冗余.特别是中文字符占用非常大 ...
- setenv LD_LIBRARY_PATH
For most Linux binaries, NCL was built using gcc and gfortran. This may cause a dependency on a file ...
- MySQL基础笔记(六) 存储过程与函数
写在开头:本文所有的示例都是基于workers表,表中保存了某公司的员工姓名.性别.工资.年龄和居住城市,如下: +----+-----------+--------+--------+------+ ...
- XxPay支付系统-boot版本 使用
https://segmentfault.com/a/1190000016987391?utm_source=tag-newest 有三个版本: spring boot 版本: spring clou ...
- How to: Use Submix Voices
How to: Use Submix Voices:https://msdn.microsoft.com/en-us/library/windows/desktop/ee415794(v=vs.85) ...
- Java 中 泛型的限定
泛型 一般 出如今集合中,迭代器中 也会出现! 泛型 是为了 提高代码的 安全性. 泛型 确保数据类型的唯一性. 在我们经常使用的容器中. 越是 单一 约优点理啊! ...