Cogs 539. The lamp of the Barn

Source: Internet
Author: User
Tags cmath

★★☆ input file: lights.in output file: lights.out Simple comparison
Time limit: 1 s memory limit: MB

"Problem description"

Beth and her girlfriends were playing games in their bullpen. But the days do not from people, suddenly, the power of the barn trip, all the lights are closed. Beth is a very timid girl, in the endless darkness that reaches out her thumb, she feels frightened, miserable and desperate. She wants you to help her, to turn all the lights back on! She can continue to play games happily with her girlfriends!

A total of n (1 <= n <= 35) lamp in the barn, numbered 1 to N. These lights are placed in a very complex network. With M (1 <= m <= 595) strips are fantastic without a forward edge, each side connecting two lights.

Each lamp has a switch on it. When the switch of a lamp is pressed, the lamp itself, and all the lights that have the edge attached to the lamp, will be changed. State change means: When a lamp is on, the lamp is turned off; when a lamp is closed, the lamp is turned on.

Ask at least how many switches to press to turn all the lights back on.

The data guarantee has at least one push-to-switch scheme that allows all lights to be reopened.

Title Name: Lights

Input format:

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

* Second to m+1: each line has two integers separated by spaces, indicating that the two lamps are connected together by a non-forward edge.
No edge will appear two times.

Sample input (file lights.in):

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

Input Details:

A total of five lamps. Lamp 1, lamp 4 and lamp 5 are connected with light 2 and lamp 3.

Output format:

First line: A single integer that indicates the minimum number of switches to be pressed when all lights are turned on.

Sample output (file Lights.out):

3

Output Details:

Press the switch above the lamp 1, lamp 4 and lamp 5.

Exercises

  and POJ 1681 almost the same, Dfs do not understand the look at this ...

1#include <iostream>2#include <cstdio>3#include <cstdlib>4#include <cmath>5#include <algorithm>6#include <cmath>7#include <queue>8#include <vector>9 using namespacestd;Ten intN,m,ans; One inta[ -][ -]; A voidGauss () { -      for(intI=1; i<=n;i++){ -         inttmp=i; the          while(tmp<=n&&a[tmp][i]==0) tmp++; -         if(tmp>n)Continue; -         if(tmp!=i) { -              for(intj=1; j<=n+1; j + +) Swap (a[i][j],a[tmp][j]); +         } -          for(intj=1; j<=n;j++){ +             if(j!=i&&a[j][i]==1){ A                  for(intk=1; k<=n+1; k++) a[j][k]^=A[i][k]; at             } -         } -     } - } - ints[ -],cnt; - voidDfsintx) { in     if(Cnt>=ans)return; -     if(x==0){ toans=min (ans,cnt); +         return; -     } the     if(A[x][x]) { *         intnum=a[x][n+1]; $          for(inti=x+1; i<=n;i++)Panax Notoginseng             if(A[x][i]) -num=num^S[i]; thes[x]=num; +         if(num==1) cnt++; ADFS (X-1); the         if(num==1) cnt--; +     } -     if(!A[x][x]) { $s[x]=0; $DFS (X-1); s[x]=1; cnt++; -DFS (X-1); cnt--; -     } the } - intMain () {Wuyi     //freopen ("lights.in", "R", stdin); the     //freopen ("Lights.out", "w", stdout); -scanf"%d%d",&n,&M); Wu      for(intI=1; i<=n;i++) a[i][i]=1; -      for(intI=1, u,v;i<=m;i++){ Aboutscanf"%d%d",&u,&v); $a[u][v]=a[v][u]=1; -     } -      for(intI=1; i<=n;i++) a[i][n+1]=1; - Gauss (); Aans=1<< -; + DFS (N); theprintf"%d", ANS); -     return 0; $}

Cogs 539. The lamp of the Barn

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.