当前位置:网站首页>7/27(板子)染色法判定二分图+求组合数(递推公式)
7/27(板子)染色法判定二分图+求组合数(递推公式)
2022-07-28 03:55:00 【咸蛋_dd】
1.染色法判定二分图
给定一个 nn 个点 mm 条边的无向图,图中可能存在重边和自环。
请你判断这个图是否是二分图。
输入格式
第一行包含两个整数 nn 和 mm。
接下来 mm 行,每行包含两个整数 uu 和 vv,表示点 uu 和点 vv 之间存在一条边。
输出格式
如果给定图是二分图,则输出
Yes,否则输出No。数据范围
1≤n,m≤1051≤n,m≤105
输入样例:
4 4 1 3 1 4 2 3 2 4输出样例:
Yes#include <bits/stdc++.h>
using namespace std;
const int N=200010;
int n,m;
int color[N];
int he[N],e[N],ne[N],idx;
void add(int a,int b)
{
e[idx]=b;
ne[idx]=he[a];
he[a]=idx++;
}
bool dfs(int u,int c)
{
color[u]=c;
for(int i=he[u];i!=-1;i=ne[i])
{
int j=e[i];
if(!color[j])
{
if(!dfs(j,3-c))
return false;
}
else if(color[j]==c)
return false;
}
return true;
}
int main()
{
//咸蛋_dd salty egg
scanf("%d %d",&n,&m);
memset(he,-1,sizeof(he));
while(m--)
{
int a,b;
cin>>a>>b;
add(a,b);
add(b,a);
}
bool flag=true;
for(int i=1;i<=n;i++)
{
if(!color[i])
{
if(!dfs(i,1))
{
flag=false;
break;
}
}
}
if(flag)
puts("Yes");
else
puts("No");
return 0;
}
2.求组合数i(注意数据范围)
给定 nn 组询问,每组询问给定两个整数 a,ba,b,请你输出 Cbamod(109+7)Cabmod(109+7) 的值。
输入格式
第一行包含整数 nn。
接下来 nn 行,每行包含一组 aa 和 bb。
输出格式
共 nn 行,每行输出一个询问的解。
数据范围
1≤n≤100001≤n≤10000,
1≤b≤a≤20001≤b≤a≤2000输入样例:
3 3 1 5 3 2 2输出样例:
3
10
1思路:根据组合数的递推公式提前预处理就ok了
#include <bits/stdc++.h>
using namespace std;
const int N=2010,mod=1e9+7;
int c[N][N];
void init()
{
for(int i=0;i<N;i++)
for(int j=0;j<=i;j++)
if(!j)c[i][j]=1;
else c[i][j]=(c[i-1][j]+c[i-1][j-1])%mod;
}
int main()
{
int n;
init();
scanf("%d",&n);
while(n--)
{
int a,b;
scanf("%d %d",&a,&b);
printf("%d\n",c[a][b]);
}
return 0;
}
边栏推荐
- 95后阿里P7晒出工资单:真的是狠狠扎心了...
- Several ways of connecting upper computer and MES
- SAP UI5 FileUploader 控件深入介绍 - 为什么需要一个隐藏的 iframe 试读版
- [leetcode] 34. Find the first and last positions of elements in the sorted array
- Implementation of online rental system based on SSM
- [P4] check the differences between the two historical versions of the library file
- Convert py file to exe executable file
- Weekly recommended short video: how to correctly understand the word "lean"?
- CH340 RTS DTR引脚编程驱动OLED
- My creation anniversary
猜你喜欢

MySQL是怎么保证高可用的

Collection | 0 basic open source data visualization platform flyfish large screen development guide

21天,胖哥亲自带你玩转OAuth2

CH340 RTS DTR引脚编程驱动OLED

一个仿win10蓝屏的404页面源码

Tungsten Fabric SDN — BGP as a Service

LeetCode 0141. 环形链表 - 三种方法解决

It's amazing. This article completely explains the service interface testing

CV2. Threshold(), CV2. Findcontours(), CV2. Findcontours image contour processing

【图像分类】2021-MLP-Mixer NIPS
随机推荐
Lightpicture - exquisite drawing bed system
It's amazing. This article completely explains the service interface testing
Prefix-Tuning: Optimizing Continuous Prompts for Generation
搬家通知!
8000 word explanation of OBSA principle and application practice
Greed 122. The best time to buy and sell stocks II
【LeetCode】34、在排序数组中查找元素的第一个和最后一个位置
Monotonic stack - 739. Daily temperature
CH340 RTS DTR引脚编程驱动OLED
Crowdfunding platform system based on JSP & Servlet
Which stock exchange has the lowest commission? Is it safe to open an account on your mobile phone
pip-script. py‘ is not present Verifying transaction: failed
security异常处理机制
Read Plato farm's eplato and the reason for its high premium
Dynamic programming - 416. Segmentation and subsets
[openvx] VX for basic use of objects_ distribution
Msgan is used for pattern search of multiple image synthesis to generate confrontation Network -- to solve the problem of pattern collapse
Dynamic planning - 62. Different paths
My creation anniversary
【P4】解决本地文件修改与库文件间的冲突问题