Description

   Good morning, agent W-12. Your mission, should you choose to accept it, is as follows. We are infiltrating the ever so insidious Association of Chaos and Mischief (ACM) in order to take down their command structure. Unfortunately, they appear to be prepared for such an eventuality, and have given their command structure an annoyingly complex design which makes our infiltration quite difficult. The ACM command structure is divided into several cells. For each pair of cells A and B, either A controls B or B controls A. But this “control” relation can be cyclic, so it could happen that A controls B and B controls C and C controls A. We can send in agents to infiltrate any particular cell, which gives us control over that cell and the cells that it controls, but not any other cells. So in the example above, infiltrating A would give us control over A and B, but not C. For a successful infiltration of the ACM, we must obtain control over all of its cells, otherwise the cells that are out of our control will discover us and start causing some of their trademark chaos and mischief. As you know, we’re on a tight spending leash from higher authority these days, so we need to execute this mission as efficiently as possible. Your mission is to figure out the minimum number of cells we need to infiltrate in order to succeed. This mission briefing will self-destruct in five hours. Good luck!

Solution

 暴搜剪枝不解释,不懂bitset的可以百度一下,还是很好用的。

Code

 

 1 #include <cstdio>
 2 #include <bitset>
 3 int n,Num,ans;
 4 int b[10];
 5 std::bitset<100> f[105];
 6 char s[105];
 7 bool dfs(int x,int last,std::bitset<100> a)
 8 {
 9     if(x>ans) return a.count()==n;
10     for(int i=last+1;i<=n;i++)
11      {
12         if(dfs(x+1,b[x]=i,f[i]|a)) return true;
13      }
14     return false;
15 }
16 int main()
17 {
18     while(~scanf("%d",&n))
19      {
20         for(int i=1;i<=n;++i)
21          {
22             scanf("%s",s);
23             for(int j=1;j<=n;++j)
24              f[i][j]=s[j-1]-'0';
25             f[i][i]=1;
26          }
27         ans=1;
28         while(!dfs(1,0,0))    
29          ans++;
30         printf("Case %d: %d",++Num,ans);
31         for(int i=1;i<=ans;i++)    
32          printf(" %d",b[i]);
33         printf("\n");
34         for(int i=1;i<=n;i++)    
35          f[i].reset();
36      }
37 }
View Code

 

posted on 2018-03-10 10:42  。林  阅读(85)  评论(0)    收藏  举报