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 ;}