http://acm.hdu.edu.cn/showproblem.php?pid=6073

题意:
有个二分图,左边和右边的顶点数相同,左边的顶点每个顶点度数为2。现在有个屌丝理解错了最佳完美匹配,它以为最佳完美匹配是边权的乘积了,现在要你计算所有这种最佳完美匹配的边权乘积和。保证至少存在一个完美匹配。

思路:

这道题目虽然打着二分图的幌子,但其实吧,根本就不是二分图,哎。

对于右边的点来说,如果它的度数为1,那么与它匹配的点肯定是确定的,所以我们先通过拓扑排序来计算出所有确定的匹配。去除这些点后,假设左边和右边各还剩下x个点,此时还有2x条边,此时右边顶点每个顶点的度数必为2。

此时其实就是连通图,对于左边的每一个顶点,它都有两种边可供选择,然而一旦确定了一条边之后,整个环就都确定下来了,所以对于一个环来说共有两种选择方法。

参考了大神的dfs方法,太强了!!!

 #include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<sstream>
#include<vector>
#include<stack>
#include<queue>
#include<cmath>
#include<map>
#include<set>
using namespace std;
typedef long long ll;
typedef pair<int,int> pll;
const int INF = 0x3f3f3f3f;
const int maxn=3e5+;
const int mod=; int n;
int tot, head[*maxn];
int del[*maxn], deg[maxn];
ll res[]; struct node
{
int v, w, next;
int mark;
}e[*maxn]; void addEdge(int u, int v, int w)
{
e[tot].v=v; e[tot].w=w; e[tot].next=head[u]; e[tot].mark=;
head[u]=tot++;
} void init()
{
tot=;
memset(head,-,sizeof(head));
memset(deg,,sizeof(deg));
memset(del,,sizeof(del));
} void dfs(int u, int flag)
{
del[u]=;
for(int i=head[u];i!=-;i=e[i].next)
{
if(e[i].mark) continue;
int v=e[i].v;
e[i].mark=e[i^].mark=;
res[flag]=(res[flag]*e[i].w)%mod;
dfs(v,flag^);
}
} int main()
{
//freopen("in.txt","r",stdin);
int T;
scanf("%d",&T);
while(T--)
{
init();
scanf("%d",&n);
for(int i=;i<=n;i++)
{
int v1,d1,v2,d2;
scanf("%d%d%d%d",&v1,&d1,&v2,&d2);
addEdge(i,v1+n,d1); addEdge(v1+n,i,d1);
addEdge(i,v2+n,d2); addEdge(v2+n,i,d2);
deg[v1]++; deg[v2]++;
} ll ans=;
queue<int> Q;
for(int i=;i<=n;i++)
if(deg[i]==) Q.push(i+n); while(!Q.empty())
{
int u=Q.front(); Q.pop();
del[u]=;
for(int i=head[u];i!=-;i=e[i].next) //左边
{
if(e[i].mark) continue;
int v=e[i].v;
del[v]=;
e[i].mark=e[i^].mark=;
ans=(ans*e[i].w)%mod; for(int j=head[v];j!=-;j=e[j].next) //右边
{
if(e[j].mark) continue;
int p=e[j].v;
deg[p-n]--;
e[j].mark=e[j^].mark=;
if(deg[p-n]==) Q.push(p);
}
}
} for(int i=;i<=n;i++)
{
if(!del[i])
{
res[]=res[]=;
dfs(i,);
ans=(ans*((res[]%mod+res[]%mod)%mod))%mod;
}
}
printf("%I64d\n",ans);
}
return ;
}

HDU 6073 Matching In Multiplication(拓扑排序+思维)的更多相关文章

  1. HDU 6073 - Matching In Multiplication | 2017 Multi-University Training Contest 4

    /* HDU 6073 - Matching In Multiplication [ 图论 ] | 2017 Multi-University Training Contest 4 题意: 定义一张二 ...

  2. HDU 6073 Matching In Multiplication(拓扑排序)

    Matching In Multiplication Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 524288/524288 K ( ...

  3. HDU 6073 Matching In Multiplication dfs遍历环 + 拓扑

    Matching In Multiplication Problem DescriptionIn the mathematical discipline of graph theory, a bipa ...

  4. HDU 6073 Matching In Multiplication —— 2017 Multi-University Training 4

    Matching In Multiplication Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 524288/524288 K ( ...

  5. 2017 ACM暑期多校联合训练 - Team 4 1007 HDU 6073 Matching In Multiplication (模拟)

    题目链接 Problem Description In the mathematical discipline of graph theory, a bipartite graph is a grap ...

  6. HDU.3342 Legal or Not (拓扑排序 TopSort)

    HDU.3342 Legal or Not (拓扑排序 TopSort) 题意分析 裸的拓扑排序 根据是否成环来判断是否合法 详解请移步 算法学习 拓扑排序(TopSort) 代码总览 #includ ...

  7. HDU.1285 确定比赛名次 (拓扑排序 TopSort)

    HDU.1285 确定比赛名次 (拓扑排序 TopSort) 题意分析 裸的拓扑排序 详解请移步 算法学习 拓扑排序(TopSort) 只不过这道的额外要求是,输出字典序最小的那组解.那么解决方案就是 ...

  8. HDU 4857 逃生 (反向拓扑排序 & 容器实现)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4857 逃生 Time Limit: 2000/1000 MS (Java/Others)    Mem ...

  9. ACM: HDU 1285 确定比赛名次 - 拓扑排序

     HDU 1285 确定比赛名次 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u De ...

随机推荐

  1. css内边距 边框

    /*1 元素的各边都有 10 像素的内边距 四个值上.右.下.左 两个上下,左右 三个值:上,左右,下*/ /*p {padding: 10%;}*/ h1 { padding-top: 10px; ...

  2. go for cryptocurrency

    https://blog.conformal.com/category/btcd/ https://github.com/btcsuite/btcd/tree/master/docs https:// ...

  3. (转)Mybatis insert后返回主键给实体对象(Mysql数据库)

    <insert id="insert" parameterType="com.zqgame.game.website.models.Team"> & ...

  4. FDR错误发现率-P值校正学习[转载]

    转自:https://baike.baidu.com/item/FDR/16312044?fr=aladdin  https://blog.csdn.net/taojiea1014/article/d ...

  5. PAT 1097 Deduplication on a Linked List[比较]

    1097 Deduplication on a Linked List(25 分) Given a singly linked list L with integer keys, you are su ...

  6. [LeetCode] 796. Rotate String_Easy **KMP

    We are given two strings, A and B. A shift on A consists of taking string A and moving the leftmost ...

  7. html07

    1.复习js的外部对象,DOM,BOMBOM window -location -Location对象 : href reload() -history -History :back() forwar ...

  8. EasyUI写的登录界面

    <!DOCTYPE html><html> <head>        <meta charset="utf-8" />       ...

  9. 面试官说:说一说CommonJs的实现原理

    其实刚看到这个题目的时候,我的内心是拒绝的,但是本着对科学的敬畏精神,我开始了 CommonJs 的探索之路. 来来来奉上我这几天的心血,拿走不客气.如有错误欢迎指正,共同进步. 提到CommonJs ...

  10. c语言的字符串拷贝函数的精简

    #include <stdio.h>#include <string.h>void str_cpy(char * to, char *from){    while ((*to ...