题目 | Red and Blue Graph
AtCoder Beginner Contest 262
E - Red and Blue Graph
https://atcoder.jp/contests/abc262/tasks/abc262_e
Time Limit: 2 sec / Memory Limit: 1024 MB
Score :
Problem Statement
You are given a simple undirected graph with
There are
- There are exactly
vertices painted red. - There is an even number of edges connecting vertices painted in different colors.
Constraints
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
Output
Print the answer.
Sample Input 1
4 4 2
1 2
1 3
2 3
3 4
Sample Output 1
2
The following two ways satisfy the conditions.
- Paint Vertices
and red and Vertices and blue. - Paint Vertices
and red and Vertices and blue.
In either of the ways above, the
Sample Input 2
10 10 3
1 2
2 4
1 5
3 6
3 9
4 10
7 8
9 10
5 9
3 4
Sample Output 2
64
我的笔记
需要满足的要求是,有
我们可以思考红点的度数,所有红点度数之和
我们需要满足连接不同颜色点的边数为偶数,即
我们首先读入边的情况,计算出每个点的度数,并计算出奇度、偶度顶点的个数,我们只需要这两个数据。
由于我们要求
另外,由于组合公式
代码
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int MOD = 998244353;
const int MAXN = 2e5 + 10;
int fact[MAXN], invf[MAXN];
int fast_pow(int a, int b)
{
a %= MOD;
int ans = 1;
while (b)
{
if (b % 2 == 1)
ans = ans * a % MOD;
a = a * a % MOD;
b >>= 1;
}
return ans;
}
void fact_init()
{
fact[0] = 1;
for (int i = 1; i <= MAXN; i++)
fact[i] = fact[i - 1] * i % MOD;
for (int i = 0; i <= MAXN; i++)
invf[i] = fast_pow(fact[i], MOD - 2);
}
int comb(int n, int m)
{
return ((fact[n] * invf[m]) % MOD * invf[n - m]) % MOD;
}
signed main()
{
fact_init();
int N, M, K;
cin >> N >> M >> K;
vector<int> d(N);
for (int i = 0; i < M; i++)
{
int U, V;
cin >> U >> V;
d[U - 1]++;
d[V - 1]++;
}
int odd = 0;
for (auto x : d)
if (x % 2)
odd++;
int even = N - odd;
long long ans = 0;
for (int selected_odd = 0; selected_odd <= K; selected_odd += 2)
{
int selected_even = K - selected_odd;
if (selected_odd <= odd && selected_even <= even)
{
ans += comb(odd, selected_odd) * comb(even, selected_even);
ans %= MOD;
}
}
cout << ans << endl;
return 0;
}
本文采用 CC BY-SA 4.0 许可,本文 Markdown 源码:Haotian-BiJi