uva 11825 Hackers' Crackdown

原题:
Miracle Corporations has a number of system services running in a distributed computer system which
is a prime target for hackers. The system is basically a set of N computer nodes with each of them
running a set of N services. Note that, the set of services running on every node is same everywhere
in the network. A hacker can destroy a service by running a specialized exploit for that service in all
the nodes.
One day, a smart hacker collects necessary exploits for all these N services and launches an attack
on the system. He finds a security hole that gives him just enough time to run a single exploit in each
computer. These exploits have the characteristic that, its successfully infects the computer where it
was originally run and all the neighbor computers of that node.
Given a network description, find the maximum number of services that the hacker can damage.
Input
There will be multiple test cases in the input file. A test case begins with an integer N (1 ≤ N ≤ 16),
the number of nodes in the network. The nodes are denoted by 0 to N − 1. Each of the following
N lines describes the neighbors of a node. Line i (0 ≤ i < N) represents the description of node i.
The description for node i starts with an integer m (Number of neighbors for node i), followed by m
integers in the range of 0 to N − 1, each denoting a neighboring node of node i.
The end of input will be denoted by a case with N = 0. This case should not be processed.
Output
For each test case, print a line in the format, ‘Case X: Y ’, where X is the case number & Y is the
maximum possible number of services that can be damaged.
Sample Input
3
2 1 2
2 0 2
2 0 1
4
1 1
1 0
1 3
1 2
0
Sample Output
Case 1: 3
Case 2: 2

英文:
Give you n servers, each with the same n processes running on them. Now you can choose a computer to shut down one of the processes, and if this computer is connected to some computers, the process of the computer connected to it will also be shut down. Each computer can only actively close one process, and now you are asked how many processes can be closed at most, that is, the number of processes that are not running on any computer.

Code:

#include<bits/stdc++.h>
using namespace std;
const int maxn=17;
int dp[1<<maxn];
int p[maxn],covers[1<<maxn];
int n,m,x;
int main()
{
    ios::sync_with_stdio(false);
    int t=1;
    while(cin>>n,n)
    {
        memset(dp,0,sizeof(dp));
        memset(p,0,sizeof(p));
        memset(covers,0,sizeof(covers));
        for(int i=0;i<n;i++)
        {
            p[i]=(1<<i);
            cin>>m;
            for(int j=1;j<=m;j++)
            {
                cin>>x;
                p[i]|=(1<<x);//与i号电脑相连的电脑加上第i号电脑的集合记录在p[i]
            }
        }
        for(int S=0;S<(1<<n);S++)
        {
            covers[S]=0;
            for(int i=0;i<n;i++)
            {
                if(S&(1<<i))
                    covers[S]|=p[i];//与电脑集合S所连接的电脑加上电脑集合S
            }
        }
        dp[0]=0;
        int ALL=(1<<n)-1;
        for(int S=1;S<(1<<n);S++)
        {
            dp[S]=0;
            for(int S0=S;S0;S0=(S0-1)&S)
            {
                if(covers[S0]==ALL)//如果电脑集合S0能连接所有电脑,那么必定能让一个进程完全瘫痪
                    dp[S]=max(dp[S],dp[S^S0]+1);//此处由于S0是S的子集,所以异或符号为取补集
            }
        }
        cout<<"Case "<<t++<<": "<<dp[ALL]<<endl;
    }
    return 0;
}












answer:

The example problems in the big white book, I have no idea how to do it~ The solution in the book is very clever


Consider two pieces of information when thinking about the state yourself, either enumerate computers with sets or enumerate processes with sets. If the record state is a collection of computers, how do you know what the process in the previous state is like when the state transitions? Consider adding a state, but still unable to find the process information of the computer set.

Consider saving the process as state, enumerating the computer. This is even more impossible since computers are processed in sets.

Consider whether it is possible to preprocess the computer set in the form of a connected graph, so as to avoid the problem of considering the running state of each computer process....

Then I didn't think about it -_-|||


The setting state dp[S] indicates how many groups can the computer set S be divided into?
Then, under the circumstance of limiting some conditions, as many groups can be divided into as many processes can be paralyzed.

The transfer equation is as follows
d p [ S ] = m a x ( d p [ S S 0 ] + 1 , d p [ S ] ) That middle S 0 Yes S of son collection c O v e r s [ S 0 ] want beg Electricity brain collection combine S 0 Can by even catch Complete Department Electricity brain

Then, if covers[S0] can be a set of all computers, that is, the computer set S0 can be connected to all computers, then it must be possible for S0 to close a process, so that the process of all computers is closed.

This avoids the problem of considering the previous state of the computer process, and the solution is very clever

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324719627&siteId=291194637