/*
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. hadoop(二MapReduce)

    hadoop(二MapReduce) 介绍 MapReduce:其实就是把数据分开处理后再将数据合在一起. Map负责“分”,即把复杂的任务分解为若干个“简单的任务”来并行处理.可以进行拆分的前提是这 ...

  2. window下 php7+apache2搭建

    一.下载php7和apache2.4 首先下载php7的windows压缩包,到这里下载http://windows.php.net/download/ Php7  VC14 x86 Thread S ...

  3. Neo4j

    Neo4j是一个高性能的,NOSQL图形数据库,它将结构化数据存储在网络上而不是表中.它是一个嵌入式的.基于磁盘的.具备完全的事务特性的Java持久化引擎,但是它将结构化数据存储在网络(从数学角度叫做 ...

  4. Fiddler 抓包工具详解

    Fiddler是一个蛮好用的抓包工具,可以将网络传输发送与接受的数据包进行截获.重发.编辑.转存等操作.也可以用来检测网络安全.反正好处多多,举之不尽呀!当年学习的时候也蛮费劲,一些蛮实用隐藏的小功能 ...

  5. table html

    <html><head><title>demo-110101</title><style type="text/css"> ...

  6. Python占位符使用总结

    格式化对象为字符串:%s myName=input('Enter your name:') userAge=input('Enter your age:') userHight=input('Ente ...

  7. springboot 集成 dubbo(一)简介

    一.简介 1,springboot 是 一款快速开发的框架,减少了开发人员对配置文件的操作.采用一些注解来取代xml配置文件. 注解包含预先封装的注解和开发人员自定义注解.同时使用Maven.Grad ...

  8. mycat sql timeout 问题解决

    发现程序中有个批量update语句需要update 16000多条数据导致超时 2019-11-06 10:35:28.312 pool-9-thread-24 ERROR com.hp.nova.c ...

  9. vue学习(8)-过渡transition&动画animate

      进入之前                                                    离开之后 v-enter---v-enter-to            v-lea ...

  10. S5PV210 点亮Led

    GPC1CON, R/W, Address = 0xE020_0080 GPC1DAT, R/W, Address = 0xE020_0084 举例 #define GPC1CON *((volati ...