1770: [Usaco2009 nov]lights Light
Time Limit:10 Sec Memory limit:64 MB
submit:888 solved:417
[Submit] [Status] [Discuss]
Description
Becky and her bedroom are playing games in their bullpen. But the day does not from the person, suddenly, the source of the barn jump shutter, all the lights are closed. Baby is a very small girl, in the endless darkness that reaches her thumb, she is horrified, miserable and hopeless. She wants you to help her and turn all the lights back on. She was able to continue to play the game with her bedroom. A total of n (1 <= n <= 35) Beacon lights in the bullpen, 1 to N. These lights are placed in a very complex network. With M (1 <= m <= 595) It's a magical infinity, with two beacon lights attached to each side. Every beacon Light has a start. When a beacon light is pressed, the beacon light itself, and all the lights that are connected to the beacon light, are changed. State change means that when a beacon light is open, the beacon Light is turned off, and when a beacon is concerned, the beacon light is opened. Ask at least how many to press to get all the lights back open. There is at least one push-to-open scheme that allows all lights to be reopened.
Input
* First line: The whole number of two spaces separated: N and M.
* Second to m+1: each line has two integers separated by a space, indicating that the two beacon lights are connected together by an endless line. There is not a single side that will appear twice.
Output
The first line: A single integer that indicates that you need to press the number of open items at least when you want to turn all the lights on.
Sample Input
5 6
1 2
1 3
4 2
3 4
2 5
5 3
Input Details:
A total of five beacon lights. The light 1, the light 4 and the light 5 are connected with the light 2 and the light 3.
Sample Output
3
Input Details:
Press the Open on the light 1, the light 4, and the light 5.
HINT
Source
Gold
Analysis
Gaussian elimination solution or equation set ...
And then you search the free-element state.
Code
#include <iostream> #include <cstring> #include <cstdio> #define P (i,j) (i-1) *6+j #define FO (i,j,k) for
(i=j;i<=k;i++) using namespace std;
const int MXN=50;
int x[mxn],a[mxn][mxn],n,m,ans=1e9;
inline void Guass () {int i,j,k,x; Fo (i,1,n) {fo (k,i+1,n) if (A[k][i]) {fo (j,1,n+1) swap (A[k]
[J],a[i][j]);
Break
} fo (k,1,n) if (A[k][i] && k!=i) fo (j,1,n+1) a[k][j]^=a[i][j];
}} inline void Dfs (int v,int now) {int i,j,k;
if (Now>=ans) return;
if (!v) {Ans=now;return;}
if (A[v][v]) {x[v]=a[v][n+1];
Fo (k,v+1,n) x[v]^= (A[v][k]&x[k]);
DFS (V-1,now+x[v]);
When it's all comes to a end, but the world keeps spinning around.
} else {X[v]=0,dfs (v-1,now);
X[v]=1,dfs (v-1,now+1);
}} int main () {int i,j,u,v;
scanf ("%d%d", &n,&m); Fo (i,1,n) a[i][i]=a[i][n+1]=1;
Fo (i,1,m) scanf ("%d%d", &u,&v), a[u][v]=a[v][u]=1;
Guass (), DFS (n,0);
printf ("%d\n", ans);
return 0; }