zoukankan      html  css  js  c++  java
  • 【P1330】 封锁阳光大学

    两个和谐河蟹不能在同一条边的两端。所以对于每条边。只有一个节点有和谐河蟹

    所以说,我们可以将有和谐河蟹的看做一种颜色,或则是状态。没有河蟹看做另一种颜色

    这样边变成了二分图染色

    所以嗯~(・∀・)

    就可以dfs暴力染色,不过要注意。有可能有多个图

    #include<iostream> 
    #include<cstdio> 
    #include<algorithm>
    using namespace std;
    struct node
    {
    	int point;
    	int nxt;
    };//链表
    node line[201000];
    int head[10100],tail;
    void add(int a,int b)
    {
    	line[++tail].point=b;
    	line[tail].nxt=head[a];
    	head[a]=tail;
    }
    int color[10100];//染色
    int turn[3]={0,2,1};//颜色转换
    int t[3];//每种颜色的使用次数
    bool flag=false;
    void dfs(int x,int c)
    {
    	color[x]=c;
    	t[c]+=1;
    	int need=head[x];
    	while(need!=0)
    	{
    		if(!color[line[need].point])
    			dfs(line[need].point,turn[c]);
    		if(color[line[need].point]==c||flag)
    		{
    			flag=true;
    			t[1]=-0x7fffffff;
    			return ;//返回负无穷
    		}
    		need=line[need].nxt;
    	}
    }
    int main()
    {
    	int n,m;
    	scanf("%d%d",&n,&m);
    	int a,b;
    	for(int i=1;i<=m;i++)
    	{
    		scanf("%d%d",&a,&b);
    		add(a,b);
    		add(b,a);
    	}
    	int ans=0;
    	for(int i=1;i<=n;i++)
    	{
    		if(!color[i])
    		{
    			t[1]=t[2]=0;
    			dfs(i,1);
    			ans+=min(t[1],t[2]);//挑一个小的加
    			if(ans<0)//就是dfs中无法二分图染色
    			{
    				printf("Impossible");
    				return 0;
    			}
    		}
    	}
    	printf("%d",ans);
    }
    
  • 相关阅读:
    plsql中查看sql执行计划
    数据库连接查询
    Class 的基本语法
    javascript中的描述对象(Descriptor)
    Nop源码分析三 周三 晴 天气不错
    NOP源码分析 二 周二
    NOP源码分析 一
    react-wow
    8、vue中得监听属性:watch--- /////watch、computed、methods得区别
    7.Vue_____keep-alive(结合路由)
  • 原文地址:https://www.cnblogs.com/Lance1ot/p/8535379.html
Copyright © 2011-2022 走看看