The accomodation of Students
Time limit:5000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
DescriptionThere is a group of students. Some of them may know, and while others don ' t. For example, A and b know each other, B and C know each other. It is not imply, A and C know each other.
Now is given all pairs of students who know each of the other. Your task is to divide, the students into, groups so, any, and students in the same group don ' t know each other. If This goal can is achieved, then arrange them into double rooms. Remember, only Paris appearing in the previous given set can live in the same, which means only known students can Li ve in the same.
Calculate the maximum number of pairs that can is arranged into these double rooms.
InputFor each data set:
The first line gives integers, N and M (1<n<=200), indicating there is n students and m pairs of students who kn Ow each of the other. The next m lines give such pairs.
Proceed to the end of file.
OutputIf These students cannot is divided into the groups, print "No". Otherwise, print the maximum number of pairs that can is arranged in those rooms.
Sample Input4 41 21 31 42 36 51 21 31 42 53 6
Sample OutputNo3
Test Instructions: There are n students, m to acquaintances, ask you whether you can divide them into 2 parts, which in the same part of the people do not know, if you can, and then put them into the bedroom, only mutual understanding of the people can be divided into the same bedroom, asked the maximum number of bedrooms
Analysis: First to determine whether it is two, not, output no, otherwise the maximum match. There are two ways to save a diagram.
/* #include <stdio.h> #include <string.h> #include <iostream>using namespace Std;const int maxn = 210;struct edge{int to,next;} Edge[maxn*maxn];int head[maxn],tol;int un,vn;int linker[maxn];bool used[maxn];int col[MAXN];void init () {tol=0; memset (head,-1,sizeof (Head));} void Addedge (int u,int v) {edge[tol].to=v; Edge[tol].next=head[u]; head[u]=tol++;} bool Dfs (int u) {for (int i=head[u];i!=-1;i=edge[i].next) {int v=edge[i].to; if (!used[v]) {used[v]=true; if (linker[v]==-1| | DFS (Linker[v])) {linker[v]=u; return true; }}} return false;} int Hungary () {int res=0; int u; memset (LINKER,-1,SIZEOF (linker)); for (u=1;u<=un;u++) {memset (used,false,sizeof (used)); if (DFS (U)) res++; } return res; BOOL Color (int u) {if (col[u]==-1) col[u]=0; for (int i=head[u];i!=-1;i=edge[i].next) {intv=edge[i].to; if (col[v]==-1) {col[v]=col[u]^1; if (! Color (v)) return false; } if (Col[v]==col[u]) return false; } return true; int main () {int n,m; int u,v; while (scanf ("%d%d", &n,&m)!=eof) {init (); Un=vn=n; for (int i=1;i<=m;i++) {scanf ("%d%d", &u,&v); Addedge (U,V); Addedge (V,u); } bool Flag=true; memset (col,-1,sizeof (col)); for (int i=1;i<=n;i++) if (col[i]==-1) flag&=color (i); if (!flag) {printf ("no\n"); Continue } else printf ("%d\n", Hungary ()/2); } return 0;} *************************************************/#include<stdio.h>#include<string.h>#include<iostream>#include<vector>using namespacestd;Const intMAXN = About; Vector<int>G[MAXN];intUN;intLINKER[MAXN];BOOLUSED[MAXN];intCOL[MAXN];BOOLDfsintu) { for(intI=0; I<g[u].size (); i++){ intv=G[u][i]; if(!Used[v]) {Used[v]=true; if(linker[v]==-1||DFS (Linker[v])) {Linker[v]=u; return true; } } } return false;}intHungary () {intres=0; intu; MEMSET (linker,-1,sizeof(linker)); for(u=1; u<=un;u++) {memset (used,false,sizeof(used)); if(Dfs (U)) res++; } returnRes;}BOOLColor (intu) { if(col[u]==-1) col[u]=0; for(intI=0; I<g[u].size (); i++){ intv=G[u][i]; if(col[v]==-1) {Col[v]=col[u]^1; if(! Color (v))return false; } if(Col[v]==col[u])return false; } return true;}intMain () {intn,m; intu,v; while(SCANF ("%d%d", &n,&m)! =EOF) { for(intI=1; i<=n;i++) g[i].clear (); UN=N; for(intI=1; i<=m;i++) {scanf ("%d%d",&u,&v); G[u].push_back (v); G[v].push_back (U); } BOOLflag=true; memset (col,-1,sizeof(col)); for(intI=1; i<=n;i++) if(col[i]==-1) flag&=Color (i); if(!flag) {printf ("no\n"); Continue; } Elseprintf"%d\n", Hungary ()/2); } return 0;}
HDU 2444 the accomodation of Students (judging if it is a binary graph and then finding the maximum match)