题目描述
Nancy, the leader of a group, always participates in many different kinds of competitions. One day, she decides to choose some of her group members to take part in an important game. In order to win, she has to make sure that each two person chosen could get along well with each other. In this sense, she wants to choose the biggest sub group from this group. And how many people could be contained in the biggest group.
输入
The first line is a number T which refers to the test cases. For each case, the first line is an integer n (0<n<50), which represent the number of person in the group. Then follow n lines, each line contains n intergers,1 or 0.The jth integer in ith line represent the relationship between the ith and the jth person.1 represent they can get well long with each other, otherwise 0. And we will make sure that the jth integer in ith line is the same with the ith integer in jth line. And the ith integer in ith line is 1
.输出
Output the number of biggest sub group. And one line for each case
样例输入
3 1 1 2 10 01 2 11 11
样例输出
1 1 2
看到这个题已经很久了,都没有想法,呵呵,只怪自己算法知识还没有学到家,这个题就是一个最大团问题,最大团的知识不用我说了,网上多的是,直接给代码
#include<map> #include<set> #include<stack> #include<queue> #include<cmath> #include<vector> #include<cstdio> #include<string> #include<cstring> #include<cstdlib> #include<iostream> #include<algorithm> #define inf 0x0f0f0f0f using namespace std; const double pi=acos(-1.0); const double eps=1e-8; typedef pair<int,int>pii; struct max_clique { static const int N=100; bool G[N][N]; int n,Max[N],Alt[N][N],ans; bool DFS(int cur,int tot) { if (cur==0) { if (tot>ans) { ans=tot; return true; } return false; } for (int i=0;i<cur;i++) { if (cur-i+tot<=ans) return false; int u=Alt[tot][i]; if (Max[u]+tot<=ans) return false; int nxt=0; for (int j=i+1;j<cur;j++) if (G[u][Alt[tot][j]]) Alt[tot+1][nxt++]=Alt[tot][j]; if (DFS(nxt,tot+1)) return true; } return false; } int maxclique() { ans=0; memset(Max,0,sizeof(Max)); for (int i=n-1;i>=0;i--) { int cur=0; for (int j=i+1;j<n;j++) if (G[i][j]) Alt[1][cur++]=j; DFS(cur,1); Max[i]=ans; } return ans; } }; int main() { //freopen("in.txt","r",stdin); int T; char str[100][100]; scanf("%d",&T); while (T--) { max_clique friends; scanf("%d",&friends.n); for (int i=0;i<friends.n;i++) scanf("%s",str[i]); for (int i=0;i<friends.n;i++) for (int j=0;j<friends.n;j++) { if (str[i][j]=='0') friends.G[i][j]=false; else friends.G[i][j]=true; } printf("%d ",friends.maxclique()); } //fclose(stdin); return 0; }