封锁阳光大学

封锁阳光大学
二分图染色问题,整个图的染色情况实际上(等价于)只有一种,所以从黑色和白色中取最小的就可以了,因为图不联通,所以取最小的就可以了。

  1 #include<iostream>
  2 #include<cstdio>
  3 #include<queue>
  4 #include<algorithm>
  5 #include<cmath>
  6 #include<ctime>
  7 #include<set>
  8 #include<map>
  9 #include<stack>
 10 #include<cstring>
 11 #define inf 2147483647
 12 #define For(i,a,b) for(register int i=a;i<=b;i++)
 13 #define p(a) putchar(a)
 14 #define g() getchar()
 15 //by war
 16 //2017.11.4
 17 using namespace std;
 18 int sum[3];
 19 int n,m;
 20 int x,y;
 21 int ans;
 22 int vis[10010];
 23 struct node
 24 {
 25     int n;
 26     node *next;
 27 }*e[100010];
 28 
 29 void in(int &x)
 30 {
 31     int y=1;
 32     char c=g();x=0;
 33     while(c<'0'||c>'9')
 34     {
 35     if(c=='-')
 36     y=-1;
 37     c=g();
 38     }
 39     while(c<='9'&&c>='0')x=(x<<1)+(x<<3)+c-'0',c=g();
 40     x*=y;
 41 }
 42 void o(int x)
 43 {
 44     if(x<0)
 45     {
 46         p('-');
 47         x=-x;
 48     }
 49     if(x>9)o(x/10);
 50     p(x%10+'0');
 51 }
 52 
 53 void push(int x,int y)
 54 {
 55     node *p;
 56     p=new node();
 57     p->n=y;
 58     if(e[x]==NULL)
 59     e[x]=p;
 60     else
 61     {
 62         p->next=e[x]->next;
 63         e[x]->next=p;
 64     }
 65 }
 66 
 67 void dfs(int x,int color)
 68 {
 69     if(vis[x])
 70     {
 71         if(vis[x]!=color+1)
 72         {
 73         puts("Impossible");
 74         exit(0);    
 75         }
 76         else
 77         return;
 78     }
 79     
 80     vis[x]=color+1;
 81     sum[color]++;
 82     for(node *i=e[x];i!=NULL;i=i->next)
 83     dfs(i->n,1-color);
 84 }
 85 
 86 int main()
 87 {
 88     in(n),in(m);
 89     For(i,1,m)
 90     {
 91         in(x),in(y);
 92         push(x,y);
 93         push(y,x);
 94     }
 95     For(i,1,n)
 96       if(!vis[i])
 97       {
 98         sum[0]=sum[1]=0;
 99         dfs(i,0);
100         ans+=min(sum[0],sum[1]);    
101       }
102     o(ans);
103      return 0;
104 }

 

posted @ 2017-11-04 14:29  WeiAR  阅读(131)  评论(0编辑  收藏  举报