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的倍数,且没有 ...
随机推荐
- 原生js实现图片的3d效果
<!doctype html><html lang="en"><head><meta charset="UTF-8"& ...
- Flask Bug记录之JinJa2.exceptions.UndefinedError: 'sqlite3.Row object' has no attribute 'get'
源码 py文件定义db的工厂函数如下 def get_db(): if "db" not in g: g.db = sqlite3.connect( current_app.con ...
- SpringBoot以WAR包部署遇到的坑---集合贴
⒈忽略tomcat的context-path 方式一: 停止tomcat服务,删除tomcat安装目录的webapps目录下的ROOT目录,将打成的WAR包重命名为ROOT.war,重启tomcat服 ...
- [DEBUG] spring boot在eclipse中用maven打包成jar访问templates报500错误
更新:打war包的话只要把html文件放在resources/templates下即可,根本不需要放外面. 配置application.yml和templates放外面这种做法,打war包确实不行. ...
- python学习-9 pycharm的安装
1.python 开发IDE : pycharm.eclipse等等 安装: 百度搜索pycharm 就可以了,去官网下载专业版.(百度有各种破解方法) #不要 ...
- C库函数:scanf、fscanf、printf、fprintf、sprintf、 snprintf
1. scanf 函数原型 int scanf(const char *format, ...); 功能:从标准输入 stdin 读取格式化输入. 2.fscanf 函数原型 int fscanf( ...
- Jboss未授权访问部署木马 利用exp
查看系统名称 java -jar jboss_exploit_fat.jar -i http://www.any.com:8080/invoker/JMXInvokerServlet get jbos ...
- SSISDB7:当前正在运行的Package及其Executable
PM问:“Vic,现在ETL Job跑到哪一个Package了,正在执行哪个Task?”,第一次遇到这个问题时,一下就懵逼了,只能硬着头皮说:“我看看”. 在做项目开发时,这个问题很常见,但是,被很多 ...
- 操作xml文件
http://www.cnblogs.com/ 一.xml文件体系如下: <?xml version="1.0" encoding="utf-8" ?&g ...
- 【Java并发】基础
一.概述 1.1 线程与进程区别 1.2 多线程引发的性能问题 二.多线程创建方式 2.1 第一种-继承Thread类 2.2 第二种-实现Runnable接口 2.3 第三种-实现Callable接 ...