1770. [USACO09 NOV] lights [Gaussian Elimination + Search]

Description

Bessie and her besties play games in their bullpen. But it didn't work out, and suddenly, the power to the bullpen tripped and all the lights were turned off. Bessie is a very timid girl. In the endless darkness where she can't see her thumb, she feels panic, pain and despair. She wants you to help her get all the lights back on! Only then can she continue to happily continue playing games with her besties! There are a total of N (1 <= N <= 35) lamps in the bullpen, numbered 1 to N. These lights are placed in a very complex network. There are M (1 <= M <= 595) magical undirected edges, each connecting two lights. Each light has a switch on it. When the switch of a light is pressed, the state of the light itself, as well as all lights connected to that light, is changed. State change means: when a light is on, the light is turned off; when a light is off, the light is turned on. Ask how many switches must be pressed to turn all the lights back on. The data guarantees that there is at least one scheme for pressing the switch so that all the lights are turned back on.

Input

* First line: integers separated by two spaces: N and M.

*Lines 2 to M+1: Each line has two integers separated by spaces, indicating that the two lamps are connected by an undirected edge. No edge will appear twice.

Output

Line 1: A single integer representing the minimum number of switches that need to be pressed to turn all lights on.

Sample Input

5 6
1 2
1 3
4 2
3 4
2 5
5 3

Enter the details:

There are five lights in total. Lamp 1, Lamp 4 and Lamp 5 are all connected to Lamp 2 and Lamp 3.

Sample Output

3

Output Details:

Press the switches above Lamp 1, Lamp 4 and Lamp 5.
 
I can't be so sloppy anymore, or I'm really going to get cold.
And why is this question in traditional Chinese? It's so powerful to

kill the small circle Gaussian elimination. The method of QAQ is also amazing
. It's easy to find that the order of the switches will not affect The end result

So what should we do?
For example, there are 3 lamps in a chestnut , where 1 and 2 are connected, and 1 and 3 are not connected, then we can list an equation
(1*ans[1])^(1*ans[2])^(0*ans[ 3])=1
ans[] indicates whether the light is pressed or not. The coefficient of lamp 1 itself or the lamp connected to lamp 1 is 1, otherwise it is 0.
Why is it listed like this? It is easy to find that if the XOR sum of the terms of 1 is 1, then the final result must be 1.
Understand it perceptually, it should be easy to understand and
so on. We can list n such equations, and then we can use Gaussian Elimination of the
matrix is ​​the same as the elimination of ordinary matrix, except that the operation of addition and subtraction is replaced by XOR. After
 
elimination , do not remember to ask for the answer, because there may be free variables in the answer.
How to do it? You can use burst search to replace the answer of ordinary Gaussian elimination.
If the answer of the current line is fixed, it will be calculated, otherwise, 0/1 will be enumerated and recorded as the answer. Continue to the next level.
 
 1 #include<iostream>
 2 #include<cstring>
 3 #include<cstdio>
 4 #define N (50)
 5 using namespace std;
 6 
 7 int n,m,minn=0x7fffffff,u,v;
 8 int f[N][N],ans[N],head[N],num_edge;
 9 
10 void Gauss()
11 {
12     for (int i=1; i<=n; ++i)
13     {
14         int num=i;
15         for (int j=i+1; j<=n; ++j)
16             if (f[j][i]>f[i][i])
17                 num=j;
18         if (num!=i)
19             for (int j=1; j<=n+1; ++j)
20                 swap(f[i][j],f[num][j]);
21         for (int j=i+1; j<=n; ++j)
22             if (f[j][i])
23                 for (int k=i; k<=n+1; ++k)
24                     f[j][k]^=f[i][k];
25     }
26 }
27 
28 void Dfs(int x,int now)
29 {
30     if (now>=minn) return;
31     if (x==0) {minn=now; return;}
32     
33     if (f[x][x])
34     {
35         int t=f[x][n+1];
36         for (int i=x+1; i<=n; ++i) t^=f[x][i]*ans[i];
37         ans[x]=t;
38         Dfs(x-1,now+(t==1));
39     }
40     else
41     {
42         ans[x]=0; Dfs(x-1,now);
43         ans[x]=1; Dfs(x-1,now+1);
44     }
45 }
46 
47 int main()
48 {
49     scanf("%d%d",&n,&m);
50     for (int i=1; i<=m; ++i)
51     {
52         scanf("%d%d",&u,&v);
53         f[u][v]=f[v][u]=1;
54     }
55     for (int i=1; i<=n; ++i) f[i][n+1]=f[i][i]=1;
56     Gauss();
57     Dfs(n,0);
58     printf("%d",minn);
59 }

Guess you like

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