2017 多校4 Matching In Multiplication(二分图)
Matching In Multiplication
题解:
首先如果一个点的度数为1,那么它的匹配方案是固定的,继而我们可以去掉这一对点。通过拓扑我们可以不断去掉所有度数为1的点。
那么剩下的图中左右各有m个点,每个点度数都不小于2,且左边每个点度数都是2,而右侧总度数是2m,因此右侧只能是每个点度数都是2。这说明这个图每个连通块是个环,在环上间隔着取即可,一共两种方案。
时间复杂度O(n)。
比赛的时候已经想出做法了,然而实现的太慢了,时间不够了,最后看了题解才想到,哦,原来隔着取就好了,我还想着去求匹配,标记取边呢
讲道理,这个时间卡得真紧,我写的挫,用vector居然过不去,改成用数组建边,队列手写才过去
#include<bits/stdc++.h>
#define LL long long
#define P pair<int,int>
using namespace std;
const int mod = 998244353;
const int N = 1e6 + 10;
int read(){
int x = 0;
char c = getchar();
while(c < '0' || c > '9') c = getchar();
while(c >= '0' && c <= '9') x = x * 10 + c - 48, c = getchar();
return x;
}
struct Edge{
int v,w,nxt;
Edge(){};
}ee[N * 2];
int head[N],EN;
int n;
int d[N];
int vis[N];
int cnt;
int e[N];
int Q[N];
void add(int u,int v,int w){
ee[EN].v = v,ee[EN].w = w,ee[EN].nxt = head[u + n];
head[u + n] = EN++;
ee[EN].v = u + n,ee[EN].w = w,ee[EN].nxt = head[v];
head[v] = EN++;
}
void init(){
EN = 0;
for(int i = 1;i <= 2 * n;i++) {
vis[i] = d[i] = 0;
head[i] = -1;
}
}
void dfs(int u,int f){
vis[u] = 1;
bool flag = true;
int p;
for(int i = head[u];~i;i = ee[i].nxt){
if(ee[i].v != f) p = ee[i].w;
if(!vis[ee[i].v]){
e[cnt++] = ee[i].w;
flag = false;
dfs(ee[i].v,u);
}
}
if(flag){
e[cnt++] = p;
}
}
int main(){
int T;
T = read();
while(T--){
n = read();
int u,v,w,v1,w1,v2,w2;
init();
for(int i = 1;i <= n;i++){
v1 = read(),w1 = read(),v2 = read(),w2 = read();
d[v1]++,d[v2]++;
add(i,v1,w1);
add(i,v2,w2);
}
int h = 0,t = 0;
LL ans = 1;
for(int i = 1;i <= n;i++){
if(d[i] == 1) Q[t++] = i;
}
while(h < t){
u = Q[h++];
for(int i = head[u];~i;i = ee[i].nxt){
if(vis[ee[i].v]) continue;
ans = ans * ee[i].w % mod, v = ee[i].v;
break;
}
vis[u] = vis[v] = 1;
for(int i = head[v];~i;i = ee[i].nxt){
if(!vis[ee[i].v] && --d[ee[i].v] == 1) Q[t++] = ee[i].v;
}
}
for(int i = 1;i <= n;i++){
if(!vis[i] && d[i] == 2){
cnt = 0;
dfs(i,-1);
LL res1 = 1,res2 = 1;
for(int j = 0;j < cnt;j+=2){
res1 = res1 * e[j] % mod;
res2 = res2 * e[j+1] % mod;
}
ans = ans * (res1 + res2) % mod ;
}
}
printf("%lld\n",ans);
}
return 0;
}
2017 多校4 Matching In Multiplication(二分图)的更多相关文章
- HDU 6073 - Matching In Multiplication | 2017 Multi-University Training Contest 4
/* 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 ( ...
- hdu6073[dfs+删边] 2017多校4
题目中对二分图的定义十分特殊, 指的是 U,V两部分中,U的顶点度数必定为2,V中顶点无限制. 题目要求的是 对于所有匹配,该匹配的权值=该匹配中选中的边的边权的乘积,求所有匹配权值之和. 对于V中的 ...
- hdu6073 Matching In Multiplication 分析+拓扑序
Matching In Multiplication Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 524288/524288 K ( ...
- 2017 多校5 hdu 6093 Rikka with Number
2017 多校5 Rikka with Number(数学 + 数位dp) 题意: 统计\([L,R]\)内 有多少数字 满足在某个\(d(d>=2)\)进制下是\(d\)的全排列的 \(1 & ...
- 2017 多校5 Rikka with String
2017 多校5 Rikka with String(ac自动机+dp) 题意: Yuta has \(n\) \(01\) strings \(s_i\), and he wants to know ...
- 2017 多校4 Wavel Sequence
2017 多校4 Wavel Sequence 题意: Formally, he defines a sequence \(a_1,a_2,...,a_n\) as ''wavel'' if and ...
- 2017 多校4 Security Check
2017 多校4 Security Check 题意: 有\(A_i\)和\(B_i\)两个长度为\(n\)的队列过安检,当\(|A_i-B_j|>K\)的时候, \(A_i和B_j\)是可以同 ...
随机推荐
- 基于mybatis设计简单信息管理系统---jsp页面
1.在设计编辑界面的时候需要有一个下拉的列表页,想要他指定到指定的值: <select id="categoryId" name="categoryId" ...
- python核心编程2 第八章 练习
8–2. 循环. 编写一个程序, 让用户输入三个数字: (f)rom, (t)o, 和 (i)ncrement . 以 i为步长, 从 f 计数到 t , 包括 f 和 t . 例如, 如果输入的是 ...
- while循环,格式化输出,运算符
1.while循环 1.while 基本机构: while 条件: 循环体 执行流程: 当条件成立时为真,执行循环体. 再次判断条件是否成立,如果成立再次执行. 当判断条件结果为假时,跳出循环,本 ...
- python__高级 : @修饰器(装饰器)的理解
以下是第一次了解的时候写的东西,有的地方理解不正确,虽已改正但是太片面,请直接看下面第二次修改加上的内容. ---------------------------------------------- ...
- Python学习笔记:PEP8常用编程规范
PEP8编码规范是一种非常优秀的编码规范,也得到了Python程序员的普遍认可,如果实践中或者项目中没有统一的编码规范,建议尽量遵循PEP8编码规范,当然如果项目中已经有了自身的编码规范,应当优先遵循 ...
- A Country on Wheels【车轮上的国家】
A Country on Wheels As cultural symbols go, the American car is quite young. 作为文化象征的美国汽车还相当年轻. The ...
- Drazil and Tiles CodeForces - 516B (类拓扑)
Drazil created a following problem about putting 1 × 2 tiles into an n × m grid: "There is a gr ...
- HyperLedger Fabric ca 1.2 正式环境部署
生成一个根CA(RootCA),在根CA下3个中间CA(IntermediaCA). 1. 运行和配置RootCA服务#cd /opt/gopath/src/github.com/hyperledge ...
- Aizu:0009- Prime Number
Prime Number Time limit 1000 ms Memory limit 131072 kB Problem Description Write a program which rea ...
- 关于C、内存、栈的一些杂谈
c的程序要手动管理内存的,所有的数据(结构)都可以分为两种存储方式,连续存储,顾名思义申请一片连续的内存以供使用(数组.结构体.共用体.栈.队列):非连续存储,顾名思义断断续续的的存储,那有一点这有一 ...