链接: https://vjudge.net/problem/CodeForces-939A 题意: As you could know there are no male planes nor female planes. However, each plane on Earth likes some other plane. There are n planes on Earth, numbered from 1 to n, and the plane w
链接:
https://vjudge.net/problem/CodeForces-939A
题意:
As you could know there are no male planes nor female planes. However, each plane on Earth likes some other plane. There are n planes on Earth, numbered from 1 to n, and the plane with number i likes the plane with number fi, where 1?≤?fi?≤?n and fi?≠?i.
We call a love triangle a situation in which plane A likes plane B, plane B likes plane C and plane C likes plane A. Find out if there is any love triangle on Earth.
思路:
刚开始以为拓扑排序,想了下直接找就行了,判断i 和 F[F[F[i]]].
代码:
#include <iostream> #include <cstdio> #include <cstring> #include <vector> //#include <memory.h> #include <queue> #include <set> #include <map> #include <algorithm> #include <math.h> #include <stack> #include <string> #include <assert.h> #define MINF 0x3f3f3f3f using namespace std; typedef long long LL; const int MAXN = 5e3+10; int F[MAXN]; int n; int main() { cin >> n; for (int i = 1;i <= n;i++) cin >> F[i]; bool flag = false; for (int i = 1;i <= n;i++) if (F[F[F[i]]] == i) flag = true; if (flag) cout << "YES" << endl; else cout << "NO" << endl; return 0; }