CodeForces - 1093D:Beautiful Graph(二分图判定+方案数)
题意:给定无向图,让你给点加权(1,2,3),使得每条边是两端点点权和维奇数。
思路:一个连通块是个二分图,判定二分图可以dfs,并查集,2-sat染色。 这里用的并查集(还可以带权并查集优化一下,或者干脆用dfs)。
计数的时候每个连通块单独考虑,我们从连通块的第一个点开始dfs,如果是该填奇数点,那么当前方案数*=2;分第一个点奇偶两种情况即可。
(多组输入一定注意初始化,这次CF多组输入好坑啊。。。
#include<bits/stdc++.h>
#define ll long long
#define rep(i,a,b) for(int i=a;i<=b;i++)
using namespace std;
const int maxn=;
const int Mod=;
int Laxt[maxn],Next[maxn],To[maxn],cnt,num1,num2,vis[maxn],tot,ans;
void add(int u,int v){
Next[++cnt]=Laxt[u]; Laxt[u]=cnt; To[cnt]=v;
}
void dfs(int u,int f,int x,int y)
{
tot++; vis[u]=;
if(x==) num1=num1*%Mod;
if(y==) num2=num2*%Mod;
for(int i=Laxt[u];i;i=Next[i]){
if(To[i]!=f&&!vis[To[i]]) dfs(To[i],u,-x,-y);
} }
int fa[maxn];
int find(int x){
if(fa[x]==x) return x;
return fa[x]=find(fa[x]);
}
int main()
{
int T,N,M,u,v;
scanf("%d",&T);
while(T--){
scanf("%d%d",&N,&M);
rep(i,,N) Laxt[i]=; cnt=;
rep(i,,N+N) fa[i]=i,vis[i]=;
bool F=true;
rep(i,,M){
scanf("%d%d",&u,&v);
add(u,v); add(v,u);
if(find(u)==find(v)) F=false;
fa[find(u)]=find(v+N);
fa[find(v)]=find(u+N);
}
if(!F) puts("");
else {
ans=;
rep(i,,N) {
if(!vis[i]){
num1=; num2=; tot=;
dfs(i,,,);
if(tot==) ans=3LL*ans%Mod;
else ans=1LL*ans*(num1+num2)%Mod;
}
}
printf("%d\n",ans);
}
}
return ;
}
CodeForces - 1093D:Beautiful Graph(二分图判定+方案数)的更多相关文章
- Codeforces 1093D Beautiful Graph(二分图染色+计数)
题目链接:Beautiful Graph 题意:给定一张无向无权图,每个顶点可以赋值1,2,3,现要求相邻节点一奇一偶,求符合要求的图的个数. 题解:由于一奇一偶,需二分图判定,染色.判定失败,直接输 ...
- Codeforces 1093D. Beautiful Graph【二分图染色】+【组合数】
<题目链接> 题目大意: 给你一个无向图(该无向图无自环,且无重边),现在要你给这个无向图的点加权,所加权值可以是1,2,3.给这些点加权之后,要使得任意边的两个端点权值之和为奇数,问总共 ...
- D - Beautiful Graph CodeForces - 1093D (二分图染色+方案数)
D - Beautiful Graph CodeForces - 1093D You are given an undirected unweighted graph consisting of nn ...
- Educational Codeforces Round 56 (Rated for Div. 2) D. Beautiful Graph (二分图染色)
题意:有\(n\)个点,\(m\)条边的无向图,可以给每个点赋点权\({1,2,3}\),使得每个点连的奇偶不同,问有多少种方案,答案对\(998244353\)取模. 题解:要使得每个点所连的奇偶不 ...
- Educational Codeforces Round 56 (Rated for Div. 2) D. Beautiful Graph 【规律 && DFS】
传送门:http://codeforces.com/contest/1093/problem/D D. Beautiful Graph time limit per test 2 seconds me ...
- Codeforces 509F Progress Monitoring:区间dp【根据遍历顺序求树的方案数】
题目链接:http://codeforces.com/problemset/problem/509/F 题意: 告诉你遍历一棵树的方法,以及遍历节点的顺序a[i],长度为n. 问你这棵树有多少种可能的 ...
- Codeforces 461B. Appleman and Tree[树形DP 方案数]
B. Appleman and Tree time limit per test 2 seconds memory limit per test 256 megabytes input standar ...
- Codeforces 57C (1-n递增方案数,组合数取模,lucas)
这个题相当于求从1-n的递增方案数,为C(2*n-1,n); 取模要用lucas定理,附上代码: #include<bits/stdc++.h> using namespace std; ...
- Educational Codeforces Round 56 D - Beautiful Graph
题目大意: 在给定的一个图中(可能不连通) 给每个点赋值1.2.3 使得一条边上的两个端点点权相加为奇数 求方案数 一条满足条件的路径上的点权必为一奇一偶交替 偶数只有2 奇数有1.3 若位于1.3 ...
随机推荐
- JavaScript 基础知识入门
js3种弹出框 alert消息对话框 var mychar=I love JavaScript;alert(mychar); confirm消息对话框 返回值:bool var mymessage ...
- mint18
ubuntu16.04用了一段时间,果然遇到祖传内部错误.然后虚拟机遇到2次重启后卡死在黑屏闪光标位置.但是用系统盘准备重装,执行到分区这步放弃,重启,居然有能启动. 作为开发机,实在有点胆战心惊,虽 ...
- How to implement connection pool in spark streaming
在spark streaming的文档里,有这么一段: def sendPartition(iter): # ConnectionPool is a static, lazily initialize ...
- 20170706wdVBA保存图片到本地API
Private Type GUID Data1 As Long Data2 As Integer Data3 As Integer Data4(0 To 7) As Byte End Type Pri ...
- 我的Java学习笔记 -开发环境搭建
开始学习Java~ 一.Java简介 Java编程语言是一种简单.面向对象.分布式.解释型.健壮安全.与系统无关.可移植.高性能.多线程和动态的语言. Java分为三个体系: JavaSE(J2SE) ...
- bzoj 1267 Kth Number I (点分治,堆)
超级钢琴的树上版本, 类似做法即可, 只不过区间转为dfs序了, 用点分求一下, 复杂度$O(nlog^2n)$ #include <iostream> #include <algo ...
- 让CLOVER默认引导WINDOWS
解决问题帖子: http://www.insanelymac.com/forum/topic/296000-force-clover-to-always-choose-win-81-efi-as-de ...
- After reading a picture than out a picture
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletExcepti ...
- 使用API失效供应商地址Demo(转)
原文地址 使用API失效供应商地址Demo DECLARE lv_return_status ) := NULL; ln_msg_count NUMBER; lv_errmsg ); lt_vend ...
- shell里的/dev/null 2>&1详解
shell中可能经常能看到: >/dev/null 2>&1 命令意思是:标准输出stdout 和标准错误输出stderr 也重定向到空设备文件,即不显示输出信息 分解这个组合:“ ...