UVALive 7662 Perfect Matchings

7662 Perfect Matchings

Given a graph G = (V, E), a matching in G is a set of pairwise non-adjacent edges; that is, no two edges
share a common vertex. A perfect matching is a matching which matches all vertices of the graph. That
is, every vertex of the graph is incident to exactly one edge of the matching. Finally, a bipartite graph
G, is a graph whose vertices can be divided into two disjoint sets U and V such that every edge connects
a vertex in U to one in V . In this task, please determine whether the number of perfect matchings of
a bipartite graph is odd or even. Note that counting the number of perfect matchings on a bipartite
graph has been proven to be #P − complete. Thus, it would be better to avoid actually counting the
number of perfect matchings.
Technical Specification
• The number of bipartite graphs is n, n ≤ 20.
• Maximum number of vertices in a bipartite graph is v, v ≤ 20 and v is an even number.
• Maximum number of edges in a bipartite graph is e, e ≤ 46.
Input
The first line consists of a number n, which is the number of bipartite graphs to follow. The first line
of each bipartite graph G contains two integers v and e, separated by a blank space. The v vertices are
naturally numbered from 1, … , v. One partition of the bipartite graph G contains vertices 1, … , v/2,
while the other partition contains vertices v/2 + 1, … , v. The next e lines each contains two integers
vi and vj , separated by a blank space, indicating an edge going from vertex vi to vertex vj , where vi
and vj belong to distinct partitions in the bipartite graph G.
Output
Please output ‘0’ if there is an odd number of perfect matchings on the input graph, and output ‘1’
otherwise.

Sample Input
2
10 5
1 6
2 7
3 8
4 9
5 10
10 7
1 6
1 7
2 6
2 7
3 8
4 9
5 10

Sample Output
0
1

题意:给一个二分图(u,v)(u和v数量相同),问从u一个一个往v映射,最多有多少种不同的映射(每个u,v一定有且只有一条边)

思路:暴力搜索即可。
因为给的是二分图,我们可以从 u 的第一个点开始dfs,若找到了与之对应的 v 点,则往下搜 u 的第二个点……当搜完所有点时,ans++

#include<bits/stdc++.h>
using namespace std;
int t,v,e,a,b;
int mp[21][21];
int vis[21];
int ans;

void dfs(int u)
{
    if(u==v/2+1)
    {
        ans++;
        return;
    }
    int flag=0;

    for(int i=v/2+1;i<=v;i++)
    {
        if(!vis[i]&&mp[u][i])
        {
            vis[i]=1;
            dfs(u+1);
            vis[i]=0;//记得 dfs 之后把vis修改回来
            flag=1;
        }
    }
    if(!flag)return;
}

int main()
{
    cin>>t;
    while(t--)
    {
        memset(mp,0,sizeof(mp));
        scanf("%d%d",&v,&e);
        for(int i=1;i<=e;i++)
        {
            scanf("%d%d",&a,&b);
            mp[a][b]=mp[b][a]=1;
        }
        ans = 0;
        memset(vis,0,sizeof(vis));
        dfs(1);
        cout<<(ans%2==0)<<endl;
    }
    return 0;
}

转载请注明出处^ ^

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值