P1330 封锁阳光大学

标准的二分图判定,没什么好说的(很久以前打的,很丑)

 1 #include <bits/stdc++.h>
 2 #define read read()
 3 #define up(i,l,r) for(register int i = (l);i <= (r);i++)
 4 #define down(i,l,r) for(register int i = (l);i >= (r);i--)
 5 #define traversal_vedge(i) for(register int i = head[u]; i ;i = e[i].nxt)
 6 #define ll long long
 7 using namespace std;
 8 int read
 9 {
10     int x = 0, f = 1; char ch = getchar();
11     while(ch < 48 || ch > 57) {if(ch == '-')f = -1; ch = getchar();}
12     while(ch >=48 && ch <=57) {x = 10 * x + ch - 48;ch = getchar();}
13     return x * f; 
14 }
15 const int N = 1e5 + 5;
16 struct edge{
17     int v,nxt;
18 }e[N<<1];int size,head[N];
19 
20 bool vis[N],color[N];
21 int n,m,ans,sum[2];
22 
23 void add(int u,int v) { e[++size] = (edge){v,head[u]}; head[u] = size; }
24 
25 bool dfs(int u,int col)
26 {
27     if(vis[u])
28     {
29         if(color[u] == col) return true;
30         else printf("Impossible
"),exit(0);    
31     }
32     vis[u] = 1;
33     color[u] = col;
34     sum[col]++;
35     traversal_vedge(i)
36     {
37         int v = e[i].v;
38         dfs(v,col^1);
39     }
40     return true;
41 }
42 
43 int main()
44 {
45     n = read; m = read;
46     int u,v;
47     up(i,1,m)
48     {
49         u = read; v = read;
50         add(u,v);
51         add(v,u);
52     }
53     up(i,1,n)
54     {
55         sum[0] = sum[1] = 0;
56         if(vis[i]) continue;
57         dfs(i,1);
58         ans += min(sum[0],sum[1]);
59     } 
60     printf("%d
",ans);
61     return 0;
62 }
原文地址:https://www.cnblogs.com/mzg1805/p/11166279.html