【比赛链接】
【题解链接】
【A】Love Triangle
【思路要点】
- 按照题意寻找三角形即可。即若存在一个点\(i\)使得\(f[f[f[i]]]==i\),答案为Yes,否则为No。
- 时间复杂度\(O(N)\)。
【代码】
#include<bits/stdc++.h> using namespace std; const int MAXN = 5005; template <typename T> void read(T &x) { x = 0; int f = 1; char c = getchar(); for (; !isdigit(c); c = getchar()) if (c == '-') f = -f; for (; isdigit(c); c = getchar()) x = x * 10 + c - '0'; x *= f; } template <typename T> void write(T x) { if (x < 0) x = -x, putchar('-'); if (x > 9) write(x / 10); putchar(x % 10 + '0'); } template <typename T> void writeln(T x) { write(x); puts(""); } int f[MAXN]; int main() { int n; read(n); for (int i = 1; i <= n; i++) read(f[i]); bool ans = false; for (int i = 1; i <= n; i++) ans |= f[f[f[i]]] == i; if (ans) printf("YES\n"); else printf("NO\n"); return 0; }
【B】Hamster Farm
【思路要点】
- 按照题意模拟即可。
- 注意答案为0的情况。
- 时间复杂度\(O(K)\)。
【代码】
#include<bits/stdc++.h> using namespace std; const int MAXN = 5005; template <typename T> void read(T &x) { x = 0; int f = 1;