http://acm.hdu.edu.cn/contests/contest_showproblem.php?cid=638&pid=1007
2 1 1 4 1 2
1
最后循环找到最大的值
#include <iostream> #include <cstring> #include <cstdio> #include <algorithm> #include <cmath> #include <cstdlib> #include <limits> #include <queue> #include <stack> #include <vector> #include <map> using namespace std; typedef long long LL; #define N 110000 #define INF 0x3f3f3f3f #define PI acos (-1.0) #define EPS 1e-8 #define met(a, b) memset (a, b, sizeof (a)) LL val[N], sum[N], f[N], ff[N], vis[N], id; void Init () { f[1] = 1; f[2] = 2; for (int i=3; i<=1000000000; i++) { f[i] = f[i-1] + f[i-2]; id = i; if (f[i] >= 1000000000) break; } } int Find (int x) { if (x != ff[x]) ff[x] = Find (ff[x]); return ff[x]; } int main () { int n, m; Init (); while (scanf ("%d %d", &n, &m) != EOF) { met (sum, 0); met (vis, 0); for (int i=1; i<=n; i++) { ff[i] = i; scanf ("%I64d", &val[i]); int flag = 0; for (int j=1; j<=id; j++) { if (val[i] == f[j])//判断是否是斐波那契数 { val[i] = 1; flag = 1; break; } } if (!flag) val[i] = 0; } for (int i=1; i<=m; i++) { int u, v; scanf ("%d %d", &u, &v); int x = Find (u), y = Find (v); if (x != y)//判断两个值是否在一个集合内 { val[x] += val[y]; ff[y] = x; } } LL maxn = -INF; for (int i=1; i<=n; i++) maxn = max (maxn, val[i]); printf ("%I64d\n", maxn); } return 0; }