当前位置:网站首页>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;
}
边栏推荐
- 【OPENVX】对象基本使用之vx_convolution
- Greed 45. Jumping game II
- 简单、好用的性能测试工具推荐
- pip-script. py‘ is not present Verifying transaction: failed
- Several ways of connecting upper computer and MES
- [openvx] VX for basic use of objects_ distribution
- Leetcode58. 最后一个单词的长度
- Msgan is used for pattern search of multiple image synthesis to generate confrontation Network -- to solve the problem of pattern collapse
- Interview essential skills: SQL query special training!
- Practical scripts of mangopapa (contents)
猜你喜欢

CH340 RTS DTR引脚编程驱动OLED

【原型与原型链】初识原型与原型链~

Advanced Mathematics (Seventh Edition) Tongji University exercises 3-4 personal solutions (first 8 questions)

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

Monotonic stack - 739. Daily temperature

Common interface testing tools

Iterator function operation of iterator learning

The latest version of pagoda installs the zip extension, and PHP -m does not display the processing method

Fourier series

95后阿里P7晒出工资单:真的是狠狠扎心了...
随机推荐
After 95, Alibaba P7 published the payroll: it's really heartbreaking
Leetcode 0141. circular linked list - three solutions
conda虚拟环境总结与解读
Data mining-02
LeetCode 0141. 环形链表 - 三种方法解决
Do you regret doing automated testing?
Notes on writing questions in sword finger offer
Advanced Mathematics (Seventh Edition) Tongji University exercises 3-4 personal solutions (first 8 questions)
VBA reads the create document of SQL in batches to create tables
Crowdfunding platform system based on JSP & Servlet
构建“产业大脑”,以“数字化”提升园区运营管理及服务能力!
面试必备杀技:SQL查询专项训练!
Vertical align align the elements in the row are vertically centered
LabVIEW loads and uses custom symbols in tree control projects
Dynamic programming - 474. One and zero
递归和非递归分别实现求第n个斐波那契数
What is interface testing and its testing process
[wrong question]
Selenium--WEB自动化测试工具
超好用的 PC 端长截图工具