2 1 1 4 1 2
1
思路:简单并查集,比赛的时候一直A不了,比赛结束5分钟就A了,改动了一点
AC代码:
#include <iostream> #include <cstdio> #include <cstdlib> #include <algorithm> #include <queue> #include <stack> #include <map> #include <cstring> #include <climits> #include <cmath> #include <cctype> typedef long long ll; using namespace std; int n,m; ll a[50]; ll b[1010]; int pre[1010]; int num[1010]; int find(int x) { return x == pre[x] ? x : pre[x] = find(pre[x]); } void merge(int x,int y) { int a = find(x); int b = find(y); if(a != b) { pre[a] = b; num[b] += num[a]; } } void cf() { ll i; a[0] = 1; a[1] = 1; for(i=2; i<=44; i++) { a[i] = a[i-1] + a[i-2]; } } int main() { int i,j; cf(); while(scanf("%d%d",&n,&m) != EOF) { memset(num,0,sizeof(num)); for(i=1; i<=n; i++) { pre[i] = i; scanf("%I64d",&b[i]); for(j=1; j<=44; j++) { if(b[i] == a[j]) num[i] = 1; } } for(i=0; i<m; i++) { int c,d; scanf("%d%d",&c,&d); merge(c,d); } int max1 = 0; for(i=1; i<=n; i++) { if(pre[i] == i) { if(num[i] > max1) max1 = num[i]; } } printf("%d\n",max1); } return 0; }
#include <iostream> #include <cstdio> #include <cstdlib> #include <algorithm> #include <queue> #include <stack> #include <map> #include <cstring> #include <climits> #include <cmath> #include <cctype> typedef long long ll; using namespace std; int n,m; ll a[50]; ll b[1010]; int pre[1010]; int num[1010]; int find(int x) { return x == pre[x] ? x : pre[x] = find(pre[x]); } void merge(int x,int y) { int a = find(x); int b = find(y); if(a != b) { pre[a] = b; num[b] += num[a]; } } void cf() { ll i; a[0] = 1; a[1] = 1; for(i=2; i<=44; i++) { a[i] = a[i-1] + a[i-2]; } } int main() { int i,j; cf(); while(scanf("%d%d",&n,&m) != EOF) { memset(num,0,sizeof(num)); for(i=1; i<=n; i++) { pre[i] = i; scanf("%I64d",&b[i]); } for(i=0; i<m; i++) { int c,d; scanf("%d%d",&c,&d); for(j=1; j<=50; j++) { if(b[c] == a[j]) { num[c] = 1; } if(b[d] == a[j]) { num[d] = 1; } } merge(c,d); } int max1 = 0; for(i=1; i<=n; i++) { if(pre[i] == i) { if(num[i] > max1) max1 = num[i]; } } printf("%d\n",max1); } return 0; }