C - C

题意:首先判断所有的人可不可以分成两部分,每部分内的所有人都相互不认识。如果可以分成 则求两部分最多相互认识的对数。
There are a group of students. Some of them may know each other, while others don't. For example, A and B know each other, B and C know each other. But this may not imply that A and C know each other.

Now you are given all pairs of students who know each other. Your task is to divide the students into two groups so that any two students in the same group don't know each other.If this goal can be achieved, then arrange them into double rooms. Remember, only paris appearing in the previous given set can live in the same room, which means only known students can live in the same room.

Calculate the maximum number of pairs that can be arranged into these double rooms.

InputFor each data set:
The first line gives two integers, n and m(1
Proceed to the end of file.

OutputIf these students cannot be divided into two groups, print "No". Otherwise, print the maximum number of pairs that can be arranged in those rooms.
Sample Input

4 4
1 2
1 3
1 4
2 3
6 5
1 2
1 3
1 4
2 5
3 6

Sample Output

No
3
解题: 能否分成两部分 则是判断是否是一个二分图
  无向图G为二分图的充分必要条件是:G至少有两个顶点,且当存在回路时,其所有回路的长度均为偶数。回路就是环路,也就是判断是否存在奇数环。
  判断二分图方法:用染色法,把图中的点染成黑色和白色。
   首先取一个点染成白色,然后将其相邻的点染成黑色,如果发现有相邻且同色的点,那么就退出,可知这个图并非二分图。
#include 
#include 
#include <string>
#include 
#include 
#include 
#include <set>
#include 
#include 
#define inf 0x3f3f3f3f
#define maxn 205
#define mod 10001
using namespace std;
int vis[maxn];
int n,m;
int dis[maxn],dp[maxn][maxn];
/* 匈 牙 利 */
int find(int x)
{
    for(int i=1;i<=n;i++)
    {
        if(!vis[i]&&dp[x][i])
        {
            vis[i]=1;
            if(dis[i]==0||find(dis[i]))
            {
                dis[i]=x;
                return 1;
            }
        }
    }
    return 0;
}
int book[maxn];
/* 判 断 二 分 图 */
int bfs()
{
    memset(book,-1,sizeof(book));
    queue<int>q;
    q.push(1);
    book[1]=0;
    while(!q.empty())
    {
        int v=q.front();
        q.pop();
        for(int i=1;i<=n;i++)
        {
            if(dp[v][i])//v和i相邻
            {
                if(book[i]==-1)//不认识,放在对立边集
                {
                    book[i]=(book[v]+1)%2;
                    q.push(i);
                }
                else//认识,不是二分图
                {
                    if(book[i]==book[v])
                    return 0;
                }
            }
        }
    }
    return 1;
}
int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        memset(dis,0,sizeof(dis));
        memset(dp,0,sizeof(dp));
        for(int i=1;i<=m;i++)
        {
            int a,b;
            scanf("%d%d",&a,&b);
            dp[a][b]=dp[b][a]=1;
        }
        if(bfs()==0)
        {
            printf("No\n");
            continue;
        }
        int ans=0;
        for(int i=1;i<=n;i++)
        {
            memset(vis,0,sizeof(vis));
            if(find(i)) ans++;
        }
        printf("%d\n",ans/2);//除2是因为对称,1认识2 与 2认识1 属同一情况
    }
}

 

你可能感兴趣的:(C - C)