1. 程式人生 > 實用技巧 >2020牛客暑期多校訓練營(第一場)

2020牛客暑期多校訓練營(第一場)

I 題目描述

Bobo has a graph with n vertices and m edges where the i-th edge is between the verticesaiand bi. Find out whether is possible for him to choose some of the edges such that the i-th vertex is incident with exactly diedges.

輸入描述:

The input consists of several test cases terminated by end-of-file.
The first line of each test case contains two integers n and m.
The second line contains n integers d_1, d_2, \dots, d_nd1,d2,,dn.
The i-th of the following m lines contains two integers a_iai and b_ibi.

輸出描述:

For each test case, print "`Yes`" without quotes if it is possible. 
Otherwise, print "`No`" without quotes.

輸入

2 1
1 1
1 2
2 1
2 2
1 2
3 2
1 1 2
1 3
2 3

輸出

Yes
No
Yes
#include<bits/stdc++.h>
using namespace std;
const int inf = 1 << 30, N = 5e4 + 7, M = 3e5 + 7;
int head[N], ver[M], edge[M], nex[M], d[N], cur[N];
int n, m, s, t, tot, maxflow;
queue<int> q;
void init() {
    maxflow 
= 0; tot = 1; memset(head, 0, sizeof(head)); } void add(int x, int y, int z) { ver[++tot] = y, edge[tot] = z, nex[tot] = head[x], head[x] = tot; ver[++tot] = x, edge[tot] = 0, nex[tot] = head[y], head[y] = tot; } bool bfs() { memset(d, 0, sizeof(d)); while (q.size()) q.pop(); q.push(s); d[s] = 1; cur[s] = head[s]; while (q.size()) { int x = q.front(); q.pop(); for (int i = head[x]; i; i = nex[i]) { int y = ver[i]; if (edge[i] && !d[y]) { q.push(y); cur[y] = head[y]; d[y] = d[x] + 1; if (y == t) return true; } } } return false; } int dinic(int x, int flow) { if (x == t) return flow; int rest = flow, k, i; for (i = cur[x]; i && rest; i = nex[i]) { int y = ver[i]; if (edge[i] && d[y] == d[x] + 1) { k = dinic(y, min(rest, edge[i])); if (!k) d[y] = 0; edge[i] -= k; edge[i ^ 1] += k; rest -= k; } } cur[x] = i; return flow - rest; } int main() { while (scanf("%d%d", &n, &m) != EOF) { int p, sum = 0, x, y; s = 2 * n + 1; t = 2 * n + 2; init(); for (int i = 1; i <= n; i++) { scanf("%d", &p); add(s, i, p); add(i + n, t, p); sum += p; } for (int i = 1; i <= m; i++) { scanf("%d%d", &x, &y); add(x, y + n, 1); add(y, x + n, 1); } int flow; while (bfs()) { while ((flow = dinic(s, inf))) maxflow += flow; } if (maxflow == sum) printf("Yes\n"); else printf("No\n"); } return 0; }