"Gaussian elimination" Bzoj 1770: [Usaco2009 nov]lights

Source: Internet
Author: User

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, to start all the lights again! She can 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.

is actually similar to POJ1222.

But there are some solutions to this problem that are free (whatever doesn't affect the solution).

So we Gaussian elimination after the BFS.

Enumerate the free elements (press and not) to update the optimal solution.

  

1#include <cstdio>2#include <cstring>3#include <cmath>4#include <algorithm>5 6 using namespacestd;7 8 intHat -][ -],cnt=0, ans,s[ -],n;9 Ten voidGuass () One { A      for(intI=1; i<=n;i++) -     { -         intj=i; the          while(ga[j][i]==0&&j<=n) J + +; -         if(j>n)Continue; -         if(j!=i) -         { +              for(intk=1; k<=n+1; k++) - swap (ga[i][k],ga[j][k]); +         } A         if(!ga[i][i])Continue; at          for(j=1; j<=n;j++) -              if(ga[j][i]&&j!=i) -                   for(intk=1; k<=n+1; k++) -ga[j][k]=ga[j][k]^Ga[i][k]; -     } - } in  - voidDfsintx) to { +     if(Cnt>=ans)return; -     if(x==0) the     { *ans=min (ans,cnt); $         return;Panax Notoginseng     } -     if(Ga[x][x]) the     { +         intnum=ga[x][n+1]; A          for(inti=x+1; i<=n;i++) the             if(Ga[x][i]) +num=num^S[i]; -s[x]=num; $         if(num==1) cnt++; $DFS (X-1); -         if(num==1) cnt--; -     } the     if(!Ga[x][x]) -     {Wuyis[x]=0;d FS (X-1); thes[x]=1; cnt++;d FS (X-1); cnt--; -     } Wu } -  About intMain () $ { -     intm,x,y; -scanf"%d%d",&n,&m); -      for(intI=1; i<=n;i++) Aga[i][i]=1, ga[i][n+1]=1; +      for(intI=1; i<=m;i++) thescanf"%d%d", &x,&y), ga[x][y]=ga[y][x]=1; - Guass (); $ans=1<< -;d FS (n); theprintf"%d", ans); the     return 0; the}
View Code

Ignore function name 2333:

"Gaussian elimination" Bzoj 1770: [Usaco2009 nov]lights

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.