HDU 6073 - Matching In Multiplication | 2017 Multi-University Training Contest 4
/*
HDU 6073 - Matching In Multiplication [ 图论 ] | 2017 Multi-University Training Contest 4
题意:
定义一张二分图,U中每个节点和V中两个节点连边
完美匹配的权值为该匹配所有边的权值相乘
求所有完美匹配的权值之和
分析:
可以发现有些V中的点只能连唯一的U中的点
按拓扑排序思路将这些全部处理掉后,剩下的点构成一个个环
每个环有两种连线方式,间隔取边权
*/
#include <bits/stdc++.h>
using namespace std;
#define LL long long
const int N = 600005;
const LL MOD = 998244353;
struct Edge {
int v; LL w;
};
vector<Edge> G[N];
int vis[N];
int cnt[N];
int t, n;
LL ans;
queue<int> que;
LL s[2];
void dfs(int x, int pre, int p)
{
if (vis[x]) return;
vis[x] = 2;
for (const auto & e : G[x])
{
if (vis[e.v] == 1 || e.v == pre) continue;
s[p] = s[p] * e.w % MOD;
dfs(e.v, x, p^1);
break;
}
}
void solve()
{
for (int i = n+1; i <= 2*n; i++)
if (G[i].size() == 1) que.push(i);
ans = 1;
while (!que.empty())
{
int y = que.front(); que.pop();
vis[y] = 1;
for (const auto& e: G[y])
{
if (!vis[e.v])
{
vis[e.v] = 1;
ans = ans * e.w % MOD;
for (const auto & ee: G[e.v])
{
if (vis[ee.v]) continue;
cnt[ee.v]--;
if (cnt[ee.v] == 1) que.push(ee.v);
}
}
}
}
for (int i = 1; i <= n; i++)
{
if (!vis[i])
{
s[0] = s[1] = 1;
dfs(i, i, 0);
ans = ans * (s[0] + s[1]) % MOD;
}
}
}
void init(int n)
{
for (int i = 0; i <= n; i++) G[i].clear();
while (!que.empty()) que.pop();
memset(vis, 0, sizeof(vis));
memset(cnt, 0, sizeof(cnt));
}
int main()
{
scanf("%d", &t);
while (t--)
{
scanf("%d", &n);
init(n<<1);
for (int i = 1; i <= n; i++)
{
int v; LL w;
scanf("%d%lld", &v, &w); v += n;
G[i].push_back(Edge{v, w});
G[v].push_back(Edge{i, w});
cnt[v]++;
scanf("%d%lld", &v, &w); v += n;
G[i].push_back(Edge{v, w});
G[v].push_back(Edge{i, w});
cnt[v]++;
}
solve();
printf("%lld\n", ans);
}
}
HDU 6073 - Matching In Multiplication | 2017 Multi-University Training Contest 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 ( ...
- HDU 6073 Matching In Multiplication(拓扑排序)
Matching In Multiplication Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 524288/524288 K ( ...
- HDU 6073 Matching In Multiplication dfs遍历环 + 拓扑
Matching In Multiplication Problem DescriptionIn the mathematical discipline of graph theory, a bipa ...
- 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 ...
- HDU 6073 Matching In Multiplication(拓扑排序+思维)
http://acm.hdu.edu.cn/showproblem.php?pid=6073 题意:有个二分图,左边和右边的顶点数相同,左边的顶点每个顶点度数为2.现在有个屌丝理解错了最佳完美匹配,它 ...
- HDU 6162 - Ch’s gift | 2017 ZJUT Multi-University Training 9
/* HDU 6162 - Ch’s gift [ LCA,线段树 ] | 2017 ZJUT Multi-University Training 9 题意: N节点的树,Q组询问 每次询问s,t两节 ...
- HDU 4951 Multiplication table(2014 Multi-University Training Contest 8)
思路 如果进制为p 那么当x<p时 (p-1)*(p-x)=(p-(x+1)) *p +x 因为x<p 所以没有进位 所以高位上的数字为 p-(x+1). 根 ...
- 2017 Wuhan University Programming Contest (Online Round) Lost in WHU 矩阵快速幂 一个无向图,求从1出发到达n最多经过T条边的方法数,边可以重复经过,到达n之后不可以再离开。
/** 题目:Lost in WHU 链接:https://oj.ejq.me/problem/26 题意:一个无向图,求从1出发到达n最多经过T条边的方法数,边可以重复经过,到达n之后不可以再离开. ...
- 2017 Wuhan University Programming Contest (Online Round) C. Divide by Six 分析+模拟
/** 题目:C. Divide by Six 链接:https://oj.ejq.me/problem/24 题意:给定一个数,这个数位数达到1e5,可能存在前导0.问为了使这个数是6的倍数,且没有 ...
随机推荐
- [百家号]7nm ARM 64核!华为Hi1620高性能CPU公开:3.0GHz
7nm ARM 64核!华为Hi1620高性能CPU公开:3.0GHz https://baijiahao.baidu.com/s?id=1617735663824201180&wfr=spi ...
- python 入门(基础)
1. python的常见数据类型(str , list ,dict,set) str (字符串的操作方法) astr = " Hello Workd " astr.strip() ...
- Jmeter博文索引~基础知识和实践操作汇总
所有Jmeter笔记的目录/索引 一,基础操作和常用操作 Jmeter入门(一)理论基础 Jmeter安装及配置(含JDK安装) Jmeter之设置线程组运行次数/时间 Jmeter之参数化(4种设置 ...
- MySql字段类型及字节
字段类型:TINYINT-----------------一个很小的整数.有符号的范围是-128到127,无符号的范围是0到255. SMALLINT--------------一个小整数.有符号的范 ...
- Rikka with Graph hdu 6090
题解:考虑贪心地一条一条边添加进去. 当 m \leq n-1m≤n−1 时,我们需要最小化距离为 nn 的点对数,所以肯定是连出一个大小为 m+1m+1 的联通块,剩下的点都是孤立点.在这个联通块中 ...
- windows 安装K8s 简易教程
1. 先安装 chocolatey https://chocolatey.org/install administrator 运行 命令: @"%SystemRoot%\System32\W ...
- aspectcore 简单解析
.netcore 下aspectcore 的使用 动态代理: static void Main(string[] args) { Console.WriteLine("Hello Worl ...
- Android应用市场App发布
来自知乎 Android应用市场App发布说到官方渠道,不得不说一些主要的大市场了,如:360.小米.应用宝.91.安卓.百度.豌豆荚.安智.现在我来一一说它们的一些简单特点. 1,360 (1)当天 ...
- SpringBoot整合Mybatis问题
IDEA对xml文件处理的方式不同 在Eclipse中到dao文件与mapper.xml文件只要在同一级目录下即可 在IDEA中mapper.xml要放在resources目录下 注:resource ...
- 微信小程序异步回调
场景如下:现有一个方法需要等待其他N个异步函数执行完毕后执行,callback麻烦的头大,翻了一波API原来小程序已经支持 async函数,那一切就好办了. 废话不多说,直接开始撸... 第一步:打开 ...