拓扑排序/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;
}
时间: 2024-12-29 01:46:58

拓扑排序/DFS HDOJ 4324 Triangle LOVE的相关文章

poj 1270 Following Orders(拓扑排序+dfs)

大致题意:每个样例包含两行,第一行输入n个字符,可能是无序的.第二行输入成对的a b,代表a要在b前面.输出所有的符合这样的序列. 思路:很明显的拓扑排序.要输出所有的序列,那么就从入度为0的点进行dfs,每次选择一个入度为0的点,加入输出序列并把与它相邻的点的入度减一.dfs结束后要把状态再改回来. #include <stdio.h> #include <algorithm> #include <set> #include <map> #include

poj1270Following Orders(拓扑排序+dfs回溯)

题目链接: 啊哈哈,点我点我 题意是: 第一列给出所有的字母数,第二列给出一些先后顺序.然后按字典序最小的方式输出所有的可能性... 思路: 总体来说是拓扑排序,但是又很多细节要考虑,首先要按字典序最小的方式输出,所以自然输入后要对这些字母进行排列,然后就是输入了,用scanf不能读空格,所以怎么建图呢??设置一个变量判断读入的先后顺序,那么建图完毕后,就拓扑排序了,那么多种方式自然就是dfs回溯了..那么这个问题就得到了解决.. 题目: Following Orders Time Limit:

ACM/ICPC 之 拓扑排序+DFS(POJ1128(ZOJ1083))

经典的拓扑排序问题,难点在于字典序输出和建立拓扑图,另外理解题意是最难的难点,没有之一... POJ1128(ZOJ1083)-Frame Stacking 题意:每个图片由同一字母组成的边框表示,每个图片的字母都不同: 在一个最多30*30的区域放置这些图片,问底层向顶层叠加的图片次序,多选时按字典序输出 注:每个图片的四边都会有字符显示,其中顶点显示两边. 题解:题意的理解是难点,题目对图片的范围确定说得有点含糊不清,博主一开始就被出现的五张图片的样例迷惑,理解重心放错了.题目最需要理解的是

hdoj 4324 Triangle LOVE【拓扑排序判断是否存在环】

Triangle LOVE Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 3566    Accepted Submission(s): 1395 Problem Description Recently, scientists find that there is love between any of two people. For

hdoj 4324 Triangle LOVE 【拓扑】

Triangle LOVE Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 2586    Accepted Submission(s): 1051 Problem Description Recently, scientists find that there is love between any of two people. Fo

HDU 5438 拓扑排序+DFS

Ponds Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 3234    Accepted Submission(s): 997 Problem Description Betty owns a lot of ponds, some of them are connected with other ponds by pipes, a

Codeforces Round #292 (Div. 2) D. Drazil and Tiles [拓扑排序 dfs]

传送门 D. Drazil and Tiles time limit per test 2 seconds memory limit per test 256 megabytes Drazil created a following problem about putting 1 × 2 tiles into an n × m grid: "There is a grid with some cells that are empty and some cells that are occupie

HDU 5438 Ponds (拓扑排序+DFS)2015 ACM/ICPC Asia Regional Changchun Online

[题目链接]:click here~~ [题目大意]: 题意:在一个无向图中有 p 个点, m 条边,每个点有一个值 vi .不断的删去度数小于2的点直到不能删为止.求新图中所有点个数为奇数的连通分量的点值的和. 1<p<10^4,1<m<10^5 [思路]删边考虑类似拓扑排序的写法,不过topsort是循环一遍1到n结点入度为0的结点,然后加入到队列中,这里只要改一下度数小于等于1,最后DFS 判断一下 挫挫的代码: /* * Problem: HDU No.5438 * Run

hdoj 4324 Triangle LOVE

Triangle LOVE Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 3903    Accepted Submission(s): 1537 Problem Description Recently, scientists find that there is love between any of two people. Fo