hdu 4687 Boke and Tsukkomi(一般图匹配)
来源:互联网 发布:乐视网络电视怎么下载 编辑:程序博客网 时间:2024/05/19 19:58
Boke and Tsukkomi
Time Limit: 3000/3000 MS (Java/Others) Memory Limit: 102400/102400 K (Java/Others)Total Submission(s): 152 Accepted Submission(s): 51
Problem Description
A new season of Touhou M-1 Grand Prix is approaching. Girls in Gensokyo cannot wait for participating it. Before the registration, they have to decide which combination they are going to compete as. Every girl in Gensokyo is both a boke (funny girl) and a tsukkomi (straight girl). Every candidate combination is made up of two girls, a boke and a tsukkomi. A girl may belong to zero or more candidate combinations, but one can only register as a member of one formal combination. The host of Touhou M-1 Grand Prix hopes that as many formal combinations as possible can participate in this year. Under these constraints, some candidate combinations are actually redundant as it\'s impossible to register it as a formal one as long as the number of formal combinations has to be maximized. So they want to figure out these redundant combinations and stop considering about them.
Input
There are multiple test cases. Process to the End of File.
The first line of each test case contains two integers: 1 ≤ N ≤ 40 and 1 ≤ M ≤ 123, where N is the number of girls in Gensokyo, and M is the number of candidate combinations. The following M lines are M candidate combinations, one by each line. Each combination is represented by two integers, the index of the boke girl 1 ≤ Bi ≤ N and the index of the tsukkomi girl 1 ≤ Ti ≤ N, where Bi != Ti.
The first line of each test case contains two integers: 1 ≤ N ≤ 40 and 1 ≤ M ≤ 123, where N is the number of girls in Gensokyo, and M is the number of candidate combinations. The following M lines are M candidate combinations, one by each line. Each combination is represented by two integers, the index of the boke girl 1 ≤ Bi ≤ N and the index of the tsukkomi girl 1 ≤ Ti ≤ N, where Bi != Ti.
Output
For each test case, output the number of redundant combinations in the first line. Then output the space-separated indexes of the redundant combinations in ascending order in the second line.
Sample Input
4 41 32 32 43 16 61 23 23 45 25 45 6
Sample Output
1232 4 5思路:一般图匹配,用带花树匹配算法。AC代码:#include <iostream>#include <cstdio>#include <cstring>#include <string>#include <algorithm>#include <queue>#include <vector>#include <cmath>#include <map>#include <cstdlib>#define L(rt) (rt<<1)#define R(rt) (rt<<1|1)#define ll long longusing namespace std;const int MAXN=50;deque<int> Q;//g[i][j]存放关系图:i,j是否有边,match[i]存放i所匹配的点bool g[MAXN][MAXN],inque[MAXN],inblossom[MAXN],vis[150];int match[MAXN],pre[MAXN],base[MAXN];int n,m,mmg;vector<int>res;struct node{ int u,v;}com[150];//找公共祖先int findancestor(int u,int v){ bool inpath[MAXN]={false}; while(1){ u=base[u]; inpath[u]=true; if(match[u]==-1)break; u=pre[match[u]]; } while(1){ v=base[v]; if(inpath[v])return v; v=pre[match[v]]; }}//压缩花void reset(int u,int anc){ while(u!=anc){ int v=match[u]; inblossom[base[u]]=1; inblossom[base[v]]=1; v=pre[v]; if(base[v]!=anc)pre[v]=match[u]; u=v; }}void contract(int u,int v,int n){ int anc=findancestor(u,v); memset(inblossom,0,sizeof(inblossom)); reset(u,anc);reset(v,anc); if(base[u]!=anc)pre[u]=v; if(base[v]!=anc)pre[v]=u; for(int i=1;i<=n;i++) if(inblossom[base[i]]){ base[i]=anc; if(!inque[i]){ Q.push_back(i); inque[i]=1; } }}bool dfs(int S,int n){ for(int i=0;i<=n;i++)pre[i]=-1,inque[i]=0,base[i]=i; Q.clear();Q.push_back(S);inque[S]=1; while(!Q.empty()){ int u=Q.front();Q.pop_front(); for(int v=1;v<=n;v++){ if(g[u][v]&&base[v]!=base[u]&&match[u]!=v){ if(v==S||(match[v]!=-1&&pre[match[v]]!=-1))contract(u,v,n); else if(pre[v]==-1){ pre[v]=u; if(match[v]!=-1)Q.push_back(match[v]),inque[match[v]]=1; else{ u=v; while(u!=-1){ v=pre[u]; int w=match[v]; match[u]=v; match[v]=u; u=w; } return true; } } } } } return false;}int MMG(){ int ans=0; memset(match,-1,sizeof(match)); for(int i=1;i<=n;i++) if(match[i]==-1&&dfs(i,n)) ans++; return ans;}int main(){ int a,b; while(~scanf("%d%d",&n,&m)) { memset(g,false,sizeof(g)); for(int i=1;i<=m;i++) { scanf("%d%d",&a,&b); g[a][b]=g[b][a]=true; com[i].u=a; com[i].v=b; } mmg=MMG(); res.clear(); for(int i=1;i<=m;i++) { a=com[i].u; b=com[i].v; memset(g,false,sizeof(g)); for(int j=1;j<=m;j++) if(j!=i) { int x=com[j].u,y=com[j].v; if(x==a||x==b||y==a||y==b) continue; g[x][y]=g[y][x]=true; } int t=MMG(); if(t!=mmg-1) res.push_back(i); } printf("%d\n",res.size()); if(res.size()) printf("%d",res[0]); for(int i=1;i<res.size();i++) printf(" %d",res[i]); printf("\n"); } return 0;}
- hdu 4687 Boke and Tsukkomi(一般图匹配)
- HDU 4687 Boke and Tsukkomi (一般图匹配带花树)
- HDU 4687 Boke and Tsukkomi(一般图匹配|带花树)
- HDU 4687 Boke and Tsukkomi(一般图匹配+枚举)
- [HDU 4687]Boke and Tsukkomi[一般图匹配][带花树]
- HDU 4687 Boke and Tsukkomi 一般图匹配
- hdu - 4687 Boke and Tsukkomi(一般图带花树匹配)
- hdu 4687 Boke and Tsukkomi(一般图匹配 带花树算法)
- HDU4687-Boke and Tsukkomi(一般图匹配带花树)
- hdu 4687 Boke and Tsukkomi
- HDOJ 4687 Boke and Tsukkomi 一般图最大匹配带花树+暴力
- Boke and Tsukkomi HDU
- HDU 4687 Boke and Tsukkomi【带花树】
- Hdu4687 Boke and Tsukkomi
- hdu 4687 带花树求一般图最大匹配(模板)
- uva753(一般图匹配)
- HDU 3551 Hard Problem 一般图的最大匹配(带花树)
- HDU 1054Strategic Game(一般图匹配之最小点覆盖)
- How to Have Two Versions of the Same App on Your Device
- Value must be an existing directory配置tomcat问题?
- TortoiseGit配合msysGit在Git@OSC代码托管的傻瓜教程
- Xcode 5 Finally Makes Interface Builder a Viable Option for Teams
- 26个Jquery使用小技巧
- hdu 4687 Boke and Tsukkomi(一般图匹配)
- VVDocumenter-Xcode
- 好书推荐-Spring in Action
- C/C++语言中闭包的探究及比较
- 基于手机音频通信应用
- getchar从字符串逐个读取字符
- Ry’s Objective-C Tutorial
- 一次架构师面试题,你会怎么回答
- Open Source Objective-C Code With Github and CocoaPods