Topological sort/dfs hdoj 4324 Triangle Love

Source: Internet
Author: User
Tags bitset cas cmath

Topic Portal

Test instructions: Sentence triangle Love (ternary ring). If a likes B, then B must not like a, any two people must have a relationship connection

Analysis: The positive solution should be a topological sequencing of the ring, if there is a ring, must be ternary ring, proof. DFS: Search from any point, search for point markers, otherwise time out. See if there is a two-point distance difference equal to 2, if present, then the above triangle ring. other practices.

Harvest: DFS search must use the VIS array ah, otherwise it is easy to timeout

Code (topology Sort):

/************************************************* author:running_time* Created time:2015-8-25 19:24:24* File Na Me: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 | 1typedef 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;}

Code (DFS):

/************************************************* author:running_time* Created time:2015-8-25 9:44:18* File Nam E: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 | 1typedef long ll;const int N = 2e3 + 10;const int INF = 0x3f3f3f3f;const int MOD = 1e9 + 7;char s[n][n];bool vis[n];i NT D[n];int n;bool DFS (int u) {for (int i=1; i<=n; ++i) {if (s[u][i] = = ' 1 ') {if (Vis[i] &amp            ;& 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;}

Topological sort/dfs hdoj 4324 Triangle Love

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.