3632: 外太空旅行
Time Limit: 5 Sec Memory Limit: 128 MBSubmit: 779 Solved: 320
[ Submit][ Status][ Discuss]
Description
在人类的触角伸向银河系的边缘之际,普通人上太空旅行已经变得稀松平常了。某理科试验班有n个人,现在班主任要从中选出尽量多的人去参加一次太空旅行活动。
可是n名同学并不是和平相处的。有的人,比如小A和小B整天狼狈为奸,是好朋友;但还有的人,比如杜鲁门和赫鲁晓夫就水火不相容。这n名同学,由于是理科生,都非常的理性,所以“朋友的朋友就是朋友”和“敌人的朋友就是敌人”这两句话对这些同学无效。换句话说,有可能小A和小B是朋友,小B和小C是朋友,但是小A和小C两人势如水火。
任意两个人之间要不就是敌人,要不就是朋友。
因为在太空船上发生人员斗殴事件是很恶劣也很危险的,因此选出来参加旅行活动的同学必须互相之间都是朋友。你的任务就是确定最多可以选多少人参加旅行。
Input
第一行一个整数n(1<=n<=50)。所有的同学按照1~n编号。
接下来若干行,每行两个用空格隔开的整数a, b(1<=a,b<=n),表示a和b是朋友。
注意:如果一个数对(x,y)(或者(y,x))没有在文件中出现,那么编号为x和y的两个同学就是敌人。
Output
仅仅一个数,即最多可以选多少人参加活动。
Sample Input
4
1 2
2 3
3 1
1 4
1 2
2 3
3 1
1 4
Sample Output
3
说明:选编号为1,2,3的同学参加,他们互相都是朋友。
说明:选编号为1,2,3的同学参加,他们互相都是朋友。
HINT
Source
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<vector>
#include<queue>
#include<set>
#include<map>
#include<stack>
#include<bitset>
#include<ext/pb_ds/priority_queue.hpp>
using namespace std;
const int N = 55;
int n,Ans,Max[N];
bool G[N][N];
bool Dfs(vector <int> &g,int tot)
{
int res = g.size();
if (!res)
{
if (tot > Ans) {Ans = tot; return 1;}
return 0;
}
for (int i = 0; i < res; i++)
{
if (tot + res - i <= Ans) return 0;
if (tot + Max[g[i]] <= Ans) return 0;
vector <int> v; v.clear();
for (int j = i + 1; j < res; j++)
if (G[g[i]][g[j]]) v.push_back(g[j]);
if (Dfs(v,tot + 1)) return 1;
}
return 0;
}
int getint()
{
char ch = getchar(); int ret = 0;
while (ch < '0' || '9' < ch)
{
if (ch == EOF) return -1;
ch = getchar();
}
while ('0' <= ch && ch <= '9')
ret = ret*10 + ch - '0',ch = getchar();
return ret;
}
int main()
{
#ifdef DMC
freopen("DMC.txt","r",stdin);
#endif
n = getint();
for (;;)
{
int x = getint(),y = getint();
if (x == -1) break; --x; --y;
G[x][y] = G[y][x] = 1;
}
for (int i = n - 1; i >= 0; i--)
{
vector <int> v; v.clear();
for (int j = i + 1; j < n; j++)
if (G[i][j]) v.push_back(j);
Dfs(v,1); Max[i] = Ans;
}
cout << Ans;
return 0;
}