• P1330 封锁阳光大学


    01染色,取染色个数较小的。
    注意图不一定联通。

    #include<cstdio>
    #include<iostream>
    #include<cstring>
    using namespace std;
    const int N=100005;
    int n,m;
    int head[N];
    int c[N];
    bool vis[N]; 
    int sum[2];
    int tot,ans;
    int read(){
    	int num=0,f=1;
    	char c=getchar();
    	while(c<'0'||c>'9'){
    		if(c=='-') f=-1;
    		c=getchar();
    	}
    	while(c>='0'&&c<='9'){
    		num=num*10+c-'0';
    		c=getchar();
    	}
    	return num*f;
    }
    struct node{
    	int to,next;
    }edge[N<<1];
    void add(int u,int v){
    	edge[tot].to=v;
    	edge[tot].next=head[u];
    	head[u]=tot++;
    }
    bool dfs(int u,int color){
    	if(vis[u]){
    		if(color!=c[u]) return 0;
    		return 1;
    	}
    	vis[u]=1;
    	c[u]=color;
    	sum[color]++;
    	bool flag=0;
    	for(int i=head[u];i!=-1;i=edge[i].next){
    		int v=edge[i].to;
    		if(!dfs(v,1-color)){
    			return 0;
    		}
    	}
    	return 1;
    }
    int main(){
    	memset(head,-1,sizeof(head));
    	n=read(); m=read();
    	for(int i=1;i<=m;i++){
    		int u=read(),v=read();
    		add(u,v); add(v,u);
    	}
    	for(int i=1;i<=n;i++){
    		if(vis[i]) continue;
    		if(!vis[i]){
    			sum[0]=sum[1]=0;
    			if(!dfs(i,0)){
    				printf("Impossible
    ");
    				return 0;
    			}
    			ans+=min(sum[1],sum[0]);
    		}
    	}
    	printf("%d",ans);
    	return 0;
    }
    
  • 相关阅读:
    E-Eating Together(POJ 3670)
    F-Dining Cows(POJ 3671)
    I-MooFest(POJ 1990)
    A-Apple Catching(POJ 2385)
    H-The Cow Lineup(POJ 1989)
    LOOPS(HDU 3853)
    Card Collector(HDU 4336)
    Help Me Escape (ZOJ 3640)
    Collecting Bugs(POJ 2096)
    c#大圣之路笔记——c# 从DataGrid中导出数据 Session
  • 原文地址:https://www.cnblogs.com/New-ljx/p/15256597.html
Copyright © 2020-2023  润新知