国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

HDU2444-判斷是否能構成二分圖

2019-11-08 19:32:08
字體:
來源:轉載
供稿:網友

The Accomodation of Students

Time Limit: 5000/1000 MS (java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 5931    Accepted Submission(s): 2703PRoblem DescriptionThere 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<n<=200), indicating there are n students and m pairs of students who know each other. The next m lines give such pairs.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 41 21 31 42 36 51 21 31 42 53 6 Sample Output
No3 

題目大意:

                      給你一張圖,判斷是否能構成二分圖,如果能求出最大匹配

題目思路:

                可以直接bfs進行染色,如果在同一集合中的點的顏色不相同則說名不能構成,否則能夠成二分圖然后進行最大匹配

AC代碼:

#include<cstring>#include<cstdio>#include<vector>#include<queue>const int maxn = 250;using std::vector;using std::queue;vector<int>edge[maxn];bool vis[maxn];int link[maxn];int n,m;bool dfs(int u){    for(int i=0;i<edge[u].size();i++){        int v = edge[u][i];        if(!vis[v]){            vis[v]=true;            if(link[v]==-1||dfs(link[v])){                link[v]=u;                return true;            }        }    }    return false;}bool bfs(int u){           //bfs染色判斷    link[u]=0;    queue<int>q;    q.push(u);    while(!q.empty()){        u = q.front();q.pop();        for(int i=0;i<edge[u].size();i++){            int v = edge[u][i];            if(link[v]==-1){link[v]=link[u]^1;q.push(v);}            else {                if(link[v]==link[u])return false;   //在不同集合中且顏色相同            }        }    }    return true;}int main(){    while(~scanf("%d%d",&n,&m)){        memset(link,-1,sizeof(link));        memset(edge,0,sizeof(edge));        while(m--){            int u,v;scanf("%d%d",&u,&v);            edge[u].push_back(v);            edge[v].push_back(u);        }        int flag=0;        for(int i=1;i<=n;i++){            if(link[i]==-1){                if(!bfs(i)){                    flag=1;                    break;                }            }        }        if(flag){            printf("No/n");            continue;        }        memset(link,-1,sizeof(link));        int ans = 0;        for(int i=1;i<=n;i++){            memset(vis,false,sizeof(vis));            if(dfs(i))ans++;        }        printf("%d/n",ans/2);    }    return 0;}


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 沙河市| 和平区| 大方县| 仙游县| 桦川县| 青铜峡市| 定日县| 密山市| 吴堡县| 河东区| 泗水县| 瓮安县| 永仁县| 中超| 武安市| 曲周县| 台南市| 大冶市| 荔浦县| 永福县| 定兴县| 营山县| 山阴县| 汤阴县| 平安县| 永吉县| 永昌县| 霍州市| 通河县| 鹤峰县| 奉新县| 丹江口市| 三明市| 彭泽县| 土默特左旗| 利津县| 湘潭市| 盐边县| 壶关县| 灵寿县| 曲阳县|