NOJ1119 Xianlin dingshan amusement park, noj1119 forest dingshan

Source: Internet
Author: User

NOJ1119 Xianlin dingshan amusement park, noj1119 forest dingshan
Question

If a directed and untitled graph is given, check whether a ring exists in the graph.

Ideas

Perform preprocessing in the form of Floyd. Eg [I] [j] not only indicates that an edge is connected to I and j, but also indicates that there is an I-to-j Path.
In this way, we traverse all the situations. If we find that both the positive and the opposite are acceptable, there will be loops.

Code
# Include <cstdio> const int maxn = 55; int n; bool eg [maxn] [maxn]; int main () {// freopen ("in.txt", "r ", stdin); int t; scanf ("% d", & t); while (t --) {for (int I = 0; I <maxn; I ++) {for (int j = 0; j <maxn; j ++) eg [I] [j] = false;} int c; scanf ("% d ", & n, & c); while (c --) {int a, B; scanf ("% d", & a, & B ); eg [a] [B] = true ;}for (int k = 0; k <n; k ++) {for (int I = 0; I <n; I ++) {for (int j = 0; j <n; j ++) {if (eg [k] [I] & eg [I] [j]) eg [k] [j] = true; // all possible routes are displayed.} bool flag = false; for (int I = 0; I <n; I ++) {for (int j = 0; j <n; j ++) {if (eg [I] [j] & eg [j] [I]) {flag = true; break ;}} if (flag) break ;}if (flag) printf ("YES \ n"); else printf ("NO \ n ");} return 0 ;}

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.