zoukankan      html  css  js  c++  java
  • 【最大点独立集】【poj1419】【Graph Coloring】

    题意:

    最多能选取多少点,没有边相连。


    解法:

    取反图,求最大团


    代码:

    #include<cstdio>
    #include<cstring>
    #include<iostream>
    using namespace std;
    const int maxn=11000;
    int e,ans,res,n,m,head[110],nxt[maxn],pnt[maxn],color[110],ansa[110];
    bool vis[110];
    void AddEdge(int u,int v)
    {
        nxt[e]=head[u];pnt[e]=v;head[u]=e++;
        nxt[e]=head[v];pnt[e]=u;head[v]=e++;
    }
    void DFS(int u,int cnt)
    {
        if(u==n+1)
        {
    	if(cnt>ans)
    	{
    	    ans=cnt;
    	    memcpy(ansa,color,sizeof(color));
    	}
    	return;
        }
        bool can=true;
        for(int i=head[u];i!=-1;i=nxt[i])
    	if(color[pnt[i]])
    	{
    	    can=false;
    	    break;
    	}
        if(can)
        {
    	color[u]=1;
    	DFS(u+1,cnt+1);
    	color[u]=0;
        }
        if(cnt+n-u>ans)
    	DFS(u+1,cnt);
    }
    int main()
    {
        int T;
        scanf("%d",&T);
        while(T--)
        {
    	ans=e=0;
    	memset(head,-1,sizeof(head));
    	memset(vis,0,sizeof(vis));
    	scanf("%d%d",&n,&m);
    	for(int i=0;i<m;i++)
    	{
    	    int u,v;
    	    scanf("%d%d",&u,&v);
    	    AddEdge(u,v);
    	}
    	ans=0;
    	DFS(1,0);
    	printf("%d
    ",ans);
    	bool first=1;
    	for(int i=1;i<=n;i++)
    	    if(ansa[i])
    	    {
    		if(first)
    		{
    		    printf("%d",i);
    		    first=0;
    		}
    		else
    		    printf(" %d",i);
    	    }
    	printf("
    ");
        }
        return 0;
    }
    


  • 相关阅读:
    Linux基础命令mv
    Linux基础命令cp
    闭包函数
    函数的嵌套
    函数对象
    global与nonlocal
    名称空间与作用域
    函数的参数(总结)
    函数的基本使用
    文件的操作之指针移动
  • 原文地址:https://www.cnblogs.com/zy691357966/p/5480343.html
Copyright © 2011-2022 走看看