P1330 blocks sunshine University and p1330 blocks sunlight

Source: Internet
Author: User

P1330 blocks sunshine University and p1330 blocks sunlight
Description

Cao is an old Cao who loves to brush the street. During the summer vacation, he scrubbed the street happily on the campus of sunshine university every day. He was upset when he saw the cheerful Cao. He crab decided to block sunshine University and refused to allow Cao Shujie.

The campus of sunshine University is an undirected graph composed of N points, and M roads are connected between N points. Each crab can block a point. When a point is blocked, the road connected to the point is blocked, and Cao cannot brush the street on the road. The tragedy is that crab is an incompatible creature. When the two crabs block two adjacent points, they will conflict.

Ask: the minimum number of crabs required. All roads can be blocked without conflict.

Input/Output Format Input Format:

First line: two integers N, M

Next M rows: each row has two integers A and B, indicating that there is A road connection between point A and point B.

Output Format:

Only one line: If the crab cannot block all roads, "Impossible" is output; otherwise, an integer is output, indicating the minimum number of crabs required.

Input and Output sample Input example #1:
[Input Example 1] 3 31 21 32 3 [input Example 2] 3 21 22 3
Output sample #1:
[Output Example 1] Impossible [Output Example 2] 1
Description

[Data scale]

1 <= N <= random, 1 <= M <= 100000, there is a maximum of one road between any two points.

 

Okay, it means that this article does not understand what is black and white dyeing, so the problem is basically incomprehensible ..

But I used BFS + DFS and AC at the same time.

Ideas:

Because each point is not necessarily connected

So we enumerate all vertices on one side and run DFS on each unused vertex,

In the DFS process, access the points connected to the point at the same time,

Then perform DFS in each DFS process,

Calculates the quantity to be placed from this point in the UNICOM component.

1 # include <iostream> 2 # include <cstdio> 3 # include <cstring> 4 # include <cmath> 5 # include <cstdlib> 6 # include <queue> 7 using namespace std; 8 void read (int & n) 9 {10 char c = '+'; int x = 0; 11 while (c <'0' | c> '9 ') 12 c = getchar (); 13 while (c> = '0' & c <= '9') 14 {15 x = x * 10 + (c-48 ); 16 c = getchar (); 17} 18 n = x; 19} 20 const int MAXN = 10101; 21 struct node 22 {23 int u, v, nxt; 24} edge [MAXN * 10 + 101]; 25 struct Dian 26 {27 int bh; 28 int how; // 0 do not put, 1 put 29} sz [MAXN]; 30 int n, m; 31 int head [MAXN]; 32 int vis1 [MAXN]; 33 int vis2 [MAXN]; 34 int fang [MAXN]; // record whether 35 int num = 1 at this point; 36 int ans1 = 0x7fffff, ans2 = 0, out = 0; 37 void add_edge (int x, int y) 38 {39 edge [num]. u = x; 40 edge [num]. v = y; 41 edge [num]. nxt = head [x]; 42 head [x] = num ++; 43} 44 void bfs (int p, int fbf) 45 {46 memset (vis2, 0, sizeof (vis2); 47 dian bg; 48 bg. bh = p; 49 bg. how = 1; 50 queue <dian> q; 51 q. push (bg); 52 while (q. size ()! = 0) 53 {54 dian now = q. front (); 55 vis2 [now. bh] = now. how; 56 q. pop (); 57 if (now. how = 1) 58 ans2 ++; 59 for (int I = head [now. bh]; I! =-1; I = edge [I]. nxt) 60 {61 dian will; 62 will. bh = edge [I]. v; 63 if (now. how = 1) will. how = 2; 64 else will. how = 1; 65 if (vis2 [edge [I]. v]) 66 {67 if (vis2 [edge [I]. v] = now. how) 68 {69 printf ("Impossible"); 70 exit (0); 71} 72 else continue; 73} 74 75 q. push (will); 76} 77} 78 ans1 = min (ans1, ans2); 79} 80 void dfs (int p) 81 {82 ans2 = 0; 83 vis1 [p] = 1; 84 bfs (p, 1); 85 for (int I = head [p]; I! =-1; I = edge [I]. nxt) 86 {87 if (vis1 [edge [I]. v] = 0) 88 {89 ans2 = 0; 90 dfs (edge [I]. v); 91} 92} 93} 94 int main () 95 {96 read (n); read (m); 97 for (int I = 1; I <= n; I ++) 98 head [I] =-1; 99 for (int I = 1; I <= m; I ++) 100 {101 int x, y; 102 read (x); read (y); 103 add_edge (x, y); 104 add_edge (y, x); 105} 106 int ans = 0; 107 for (int I = 1; I <= n; I ++) 108 {109 if (vis1 [I] = 0 & head [I]! =-1) 110 {111 ans1 = 0x7ffff; 112 dfs (I); 113 out + = ans1; 114} 115 116} 117 printf ("% d", out ); 118 return 0; 119}

 

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.