/*
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的更多相关文章

  1. 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 ( ...

  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. 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 ...

  5. HDU 6073 Matching In Multiplication(拓扑排序+思维)

    http://acm.hdu.edu.cn/showproblem.php?pid=6073 题意:有个二分图,左边和右边的顶点数相同,左边的顶点每个顶点度数为2.现在有个屌丝理解错了最佳完美匹配,它 ...

  6. 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两节 ...

  7. 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). 根 ...

  8. 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之后不可以再离开. ...

  9. 2017 Wuhan University Programming Contest (Online Round) C. Divide by Six 分析+模拟

    /** 题目:C. Divide by Six 链接:https://oj.ejq.me/problem/24 题意:给定一个数,这个数位数达到1e5,可能存在前导0.问为了使这个数是6的倍数,且没有 ...

随机推荐

  1. 改变checkbox默认样式

    input[type='checkbox']{ width: 5rem; height: 5rem; -webkit-appearance: none; /*清除复选框默认样式*/ backgroun ...

  2. lua 模块与包(五)

    一.模块的介绍 模块类似于1个封装库,从Lua 5.1 开始,Lua加入了标准的模块管理机制,可以把一些公用的代码放在一个文件里,以API接口的形式在其他地方调用,用利于代码的重用和降低代码的耦合度. ...

  3. javaweb中关于转发与重定向的写法

    转发: RequestDispatcher rd = request.getRequestDispatcher("/WEB-INF/main.jsp"); rd.forward(r ...

  4. JavaScript(js)笔记

    js注释 JavaScript注释与Java注释相同 // 单行注释 /* 多行注释 */ js五大基本类型:   number(数值型).string(字符串性).boolean(布尔型).unde ...

  5. golang数据类型

    整数类型   Golang各整数类型分:有符号和无符号,int uint 的大小和系统有关. Golang查看一个变量的数据类型: package main import "fmt" ...

  6. 基于LINUX下的进程管理问题

    什么是进程? 程序(program)放置在存储媒体中(如硬盘,光盘,软盘,磁带等)为实体文件的形态存在 进程:程序被触发后,执行者的权限与属性,程序的程序码与所需数据等都会被载入内存中,操作系统并给予 ...

  7. 【KMP】OKR-Periods of Words

    [KMP]OKR-Periods of Words 题目描述 串是有限个小写字符的序列,特别的,一个空序列也可以是一个串.一个串P是串A的前缀,当且仅当存在串B,使得A=PB.如果P≠A并且P不是一个 ...

  8. spring-boot-plus CORS跨域处理

    CORS跨域处理 CORS:Cross-Origin Resource Sharing CORS是一种允许当前域(domain)的资源(比如html/js/web service)被其他域(domai ...

  9. sql 行数据找出最大的及所有数据最大的

    SELECT @charges=ISNULL(MAX(a.maxcharge), 0.00) FROM( SELECT (SELECT MAX(maxcharge) FROM(VALUES(ilong ...

  10. mvc 登陆界面+后台代码

    上代码 前端+js(懒得分文件了) @{ ViewBag.Title = "MVC权限系统架构学习-登录"; Layout = "/Views/Shared/_LoadJ ...