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. android 网络监测

    public class NetWorkStateReceiver extends BroadcastReceiver { @Override public void onReceive(Contex ...

  2. Sql Server 2016 Always On集群搭建

    第一步,配置好windows环境 第二步 (配置内容较多--需单写) 需要做windows集群 安装WSFC集群组件 直接在Windows服务器管理工具中,增加功能模块,集群故障转移模块 并增加节点 ...

  3. 011-spring cloud gateway-使用

    一.pom增加 <dependency> <groupId>org.springframework.boot</groupId> <artifactId> ...

  4. 12.预处理数据的方法总结(使用sklearn-preprocessing)

    https://blog.csdn.net/sinat_33761963/article/details/53433799

  5. 【Cocos2dx 3.3 Lua】触屏事件

    cocos2dx 3.x触屏时间分为单点触摸和多点触摸:     单点触摸:(即只有注册的Layer才能接收触摸事件)      多点触摸点单用法(多个Layer获取屏幕事件):           ...

  6. matplotlib--画图时保存图片空白的问题

    问题: 当使用如下代码保存使用 plt.savefig 保存生成的图片时,结果打开生成的图片却是一片空白. import matplotlib.pyplot as plt ""&q ...

  7. JSP—中文乱码

    中文乱码问题? --------------------------------------- 不乱码的条件: 1.JSP页面本身的编码 pageEncoding UTF-8 (把jsp页面翻译成ja ...

  8. 174. Dungeon Game(动态规划)

    The demons had captured the princess (P) and imprisoned her in the bottom-right corner of a dungeon. ...

  9. uva10167

    /* 暴力 过了 要使得两半的 樱桃数目相等 去试每一个斜率 还好他这里要的是 A.B 都为正整数 这样范围就锁定在200*100 个点范围内 */ #include <cstdio> # ...

  10. windows系统bat方式启动tomcat出现java.lang.OutOfmemoryError:PermGen Space 错误

    1.问题情景: 在部署项目时,将两个应用部署到同一个tomcat下,通过startup.bat启动服务时,控制台出现出现java.lang.OutOfmemoryError:PermGen Space ...