题意 : 就是有n个人,m对关系,每对关系的两个人是好朋友,这个关系是相互的,如果有两个人的共同好朋友超过k个,那这两个人也会是好朋友的,给你m对关系,给你足够长的时间,问你还能增加几对关系。
思路 : 暴力,一开始忘了增加了新关系之后要回到第0个点开始找,因为时间足够长。
#include <iostream> #include <string.h> #include <stdlib.h> #include <stdio.h> #include <math.h> using namespace std; const int maxn = 110 ; int a[maxn],map[maxn][maxn] ; int main() { int T,n,m,k ; scanf("%d",&T) ; for(int i = 0 ; i < T ; i++) { memset(map,0,sizeof(map)) ; scanf("%d %d %d",&n,&m,&k) ; int x,y ; for(int j = 0 ; j < m ; j++) { scanf("%d %d",&x,&y) ; map[x][y] = map[y][x] = 1 ; } int cnt = 0,sum = 0 ,flag = 0,s; for(int j = 0 ; j < n ; j++) { for(int h = j+1 ; h < n ; h++) { if(!map[j][h]) { cnt = 0 ; for(s = 0 ; s < n ; s++) { if(map[j][s] && map[h][s]) cnt++ ; if(cnt >= k) break ; } if(s != n) { map[j][h] = map[h][j] = 1 ;//新增加的关系 j = -1 ;//因为你回去之后要++一次,所以这里要从-1开始,从0开始WA了一次 sum ++ ; break ; } } } } printf("%d ",sum) ; } return 0; }