FZU 1202

http://acm.fzu.edu.cn/problem.php?pid=1202

二分图最大匹配,问哪些边是必要的,O(n^3)的方法

删边的时候把连接关系也要删掉,如果在此基础上无法找到增广路,加入答案,恢复连接关系,如果能找到,连接关系不用恢复(因为要n对匹配,这组匹配新的了剩下的要留给别的组)

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std ;
int T,M[105][105],n,linkx[105],linky[105],vis[105],a[105],b[105] ;
int find(int s)
{
for(int i=1 ;i<=n ;i++)
{
if(M[s][i])
{
if(vis[i]==T)continue ;
vis[i]=T ;
if(!linky[i] || find(linky[i]))
{
linky[i]=s ;
linkx[s]=i ;
return 1 ;
}
}
}
return 0 ;
}
int max_match()
{
int ans=0 ;
memset(linkx,0,sizeof(linkx)) ;
memset(linky,0,sizeof(linky)) ;
memset(vis,0,sizeof(vis)) ;
for(int i=1 ;i<=n ;i++)
{
T=i ;
ans+=find(i);
}
return ans;
}
struct node
{
int first,second ;
}ans[105] ;
int cmp(node aa,node bb)
{
if(aa.first==bb.first)return aa.second<bb.second ;
return aa.first<bb.first ;
}
int main()
{
while(~scanf("%d",&n))
{
for(int i=0 ;i<105 ;i++)
for(int j=0 ;j<105 ;j++)
M[i][j]=1 ;
while(1)
{
int xx,yy ;
scanf("%d%d",&xx,&yy) ;
if(!xx && !yy)break ;
M[xx][yy]=0 ;
}
int res=max_match() ;
if(res!=n)
{
puts("none") ;
putchar(‘\n‘) ;
continue ;
}
int st=0 ;
memset(vis,0,sizeof(vis)) ;
for(int i=1 ;i<=n ;i++)
{
T=i ;
int temp=linkx[i] ;
M[i][temp]=0 ;
linkx[i]=0 ;linky[temp]=0 ;
if(!find(i))
{
ans[st].first=i ;
ans[st++].second=temp ;
linkx[i]=temp ;
linky[temp]=i ;
}
M[i][temp]=1 ;
}
if(st)
{
sort(ans,ans+st,cmp) ;
for(int i=0 ;i<st ;i++)
printf("%d %d\n",ans[i].first,ans[i].second) ;
}
else puts("none") ;
putchar(‘\n‘) ;
}
return 0 ;
}

时间: 2024-08-10 21:14:30

FZU 1202的相关文章

FZU 2150 Fire Game(点火游戏)

p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: Calibri; font-size: 10.5000pt } h2 { margin-top: 5.0000pt; margin-bottom: 5.0000pt; text-align: left; font-family: 宋体; font-weight: bold; font-size: 18.0000pt } h3 {

FZU 1096 QS Network

QS Network Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged on FZU. Original ID: 1096 64-bit integer IO format: %I64d      Java class name: Main In the planet w-503 of galaxy cgb, there is a kind of intelligent creature named QS.

51NOD 1202 子序列个数 DP

http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1202&judgeId=225600 这题看起来挺复杂,但是真正的dp还是挺好理解的.唯独是想不到的,应该把样例模拟一遍. 比如1.2.4.2 考虑第一个,只有"1"这一个子序列 考虑前两个,有:"1", "12", "2" 前三个,有:"1", "12"

FZU 1759 欧拉函数 降幂公式

Description Given A,B,C, You should quickly calculate the result of A^B mod C. (1<=A,C<=1000000000,1<=B<=10^1000000). Input There are multiply testcases. Each testcase, there is one line contains three integers A, B and C, separated by a singl

1202 子序列个数

1202 子序列个数 基准时间限制:1 秒 空间限制:131072 KB 子序列的定义:对于一个序列a=a[1],a[2],......a[n].则非空序列a'=a[p1],a[p2]......a[pm]为a的一个子序列,其中1<=p1<p2<.....<pm<=n. 例如4,14,2,3和14,1,2,3都为4,13,14,1,2,3的子序列.对于给出序列a,有些子序列可能是相同的,这里只算做1个,请输出a的不同子序列的数量.由于答案比较大,输出Mod 10^9 + 7的

ACM: FZU 2150 Fire Game - DFS+BFS+枝剪 或者 纯BFS+枝剪

FZU 2150 Fire Game Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Description Fat brother and Maze are playing a kind of special (hentai) game on an N*M board (N rows, M columns). At the beginning, each grid of this boar

FZU 2112 并查集、欧拉通路

原题:http://acm.fzu.edu.cn/problem.php?pid=2112 首先是,票上没有提到的点是不需要去的. 然后我们先考虑这个图有几个联通分量,我们可以用一个并查集来维护,假设有n个联通分量,我们就需要n-1条边把他们连起来. 最后对于每个联通分量来说,我们要使它能一次走完,就是要求他是否满足欧拉通路,也就是这个联通分量中至多有2个度为奇数的点,每多出2个度为奇数的点,就多需要一条边(因为单个连通分量的所有点的度数之和为偶数,所以不可能存在奇数个奇数度数的点). 1 #i

POJ 1202 Family 概率,DP,高精 难度:2

http://poj.org/problem?id=1202 难度集中在输出格式上,因为输出格式所以是高精度 递推式: 血缘肯定只有从双亲传到儿子的,所以,设f,m为双亲,son为儿子,p[i][j]为i和j之间的血缘关系,p[j][i]=p[i][j] 则: p[son][f]=p[son][m]=0.5+0.5*p[f][m] 对于兄弟和其他不是双亲的节点j,则有 p[son][j]=0.5*(p[f][j]+p[m][j]) 另外:http://swerc.up.pt/2002/Data/

FZU Problem 2102 Solve equation (数学啊 )

题目链接:http://acm.fzu.edu.cn/problem.php?pid=2102 Problem Description You are given two positive integers A and B in Base C. For the equation: A=k*B+d We know there always existing many non-negative pairs (k, d) that satisfy the equation above. Now in