hdu3118Arbiter

hdu3118Arbiter
http://acm.hdu.edu.cn/showproblem.php?pid=3118
二分图的性质:一个图十二分图,当且仅当图中不存在奇数环。
把点分成两个集合,把每个集合中相连的边删除即可。枚举所有集合,找出删除边最小的那个。

#include<stdio.h>
#include
<stdlib.h>
#include
<string.h>
#define LEN 20
#define MAX 0xfffffff
int mp[LEN][LEN];
int N, M;
int rs;
int tt;
int gd[LEN];
void DFS(int n)
{
    
if(n >= N)
    
{
        
int i, j;
        tt 
= 0;
        
for(i = 0; i < n; i++)
            
for(j = i + 1; j < n; j++)
            
{
                
if(gd[i] == gd[j])
                    tt 
+= mp[i][j];
            }

        
if(tt < rs)
            rs 
= tt;
    }

    
else
    
{
        gd[n] 
= 1;
        DFS(n 
+ 1);
        gd[n] 
= 0;
        DFS(n 
+ 1);
    }

}

int main()
{
    
int i, j;
    
int T;
    
int x, y;
    scanf(
"%d"&T);
    
while(T--)
    
{
        scanf(
"%d%d"&N, &M);
        memset(mp, 
0sizeof(mp));
        
for(i = 0; i < M; i++)
        
{
            scanf(
"%d%d"&x, &y);
            mp[x][y]
++;
            mp[y][x]
++;
        }

        memset(gd, 
0sizeof(gd));
        rs 
= MAX;
        DFS(
0);
        printf(
"%d\n", rs);
    }

    
//system("pause");
    return 0;
}


你可能感兴趣的:(hdu3118Arbiter)