国产探花免费观看_亚洲丰满少妇自慰呻吟_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;}


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 平潭县| 荆州市| 通江县| 济南市| 灵川县| 克拉玛依市| 文成县| 平舆县| 阿坝| 晋中市| 招远市| 湖南省| 循化| 镇巴县| 车致| 南漳县| 阳谷县| 上犹县| 正镶白旗| 阿图什市| 龙井市| 峨山| 铜山县| 繁昌县| 福贡县| 万载县| 东宁县| 东台市| 辽中县| 井冈山市| 江西省| 遂川县| 万州区| 云梦县| 长岭县| 渑池县| 兴仁县| 甘谷县| 昌平区| 甘谷县| 富民县|