题目链接
Bobo has a graph with n vertices and m edges where the i-th edge is between the vertices a i a_i ai and b i b_i bi. Find out whether is possible for him to choose some of the edges such that the i-th vertex is incident with exactly d i d_i di edges.
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 , … , d n d_1, d_2, \dots, d_n d1,d2,…,dn.
The i-th of the following m lines contains two integers a i a_i ai and b i b_i bi.
∗ 1 ≤ n ≤ 50 * 1 \leq n \leq 50 ∗1≤n≤50
∗ 1 ≤ m ≤ 100 * 1 \leq m \leq 100 ∗1≤m≤100
∗ 1 ≤ d i ≤ 2 * 1 \leq d_i \leq 2 ∗1≤di≤2
∗ 1 ≤ a i , b i ≤ n * 1 \leq a_i, b_i \leq n ∗1≤ai,bi≤n
∗ a i ≠ b i * a_i \neq b_i ∗ai=bi
∗ { a i , b i } ≠ { a j , b j } * \{a_i, b_i\} \neq \{a_j, b_j\} ∗{ai,bi}={aj,bj}
* The number of tests does not exceed 100.
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
这题就是重构图然后判断完全匹配,和 HDU 3551 一模一样,(⊙﹏⊙),考了原题都没发现,太菜了,我还一直在想怎么用 DFS 或者 BFS 甚至用离散数学的知识试图解出它,AC代码如下:
#include
using namespace std;
const int N=1e3+5;
deque<int>q;
bool G[N][N],g[N][N],inque[N],inblossom[N],vis[N];
int match[N],pre[N],base[N];
int n,m,t,u,v,d,id;
vector<int>V[N];
struct node{
int u,v;
}E[N];
int Find(int u,int v){
bool inpath[N]={false};
while(1){
u=base[u];
inpath[u]=true;
if(match[u]==-1)break;
u=pre[match[u]];
}
while(1){
v=base[v];
if(inpath[v])return v;
v=pre[match[v]];
}
}
void reset(int u,int anc){
while(u!=anc){
int v=match[u];
inblossom[base[u]]=1;
inblossom[base[v]]=1;
v=pre[v];
if(base[v]!=anc)pre[v]=match[u];
u=v;
}
}
void contract(int u,int v,int n){
int anc=Find(u,v);
memset(inblossom,0,sizeof(inblossom));
reset(u,anc);reset(v,anc);
if(base[u]!=anc)pre[u]=v;
if(base[v]!=anc)pre[v]=u;
for(int i=1;i<=n;i++)
if(inblossom[base[i]]){
base[i]=anc;
if(!inque[i]){
q.push_back(i);
inque[i]=1;
}
}
}
bool dfs(int s,int n){
for(int i=0;i<=n;i++)pre[i]=-1,inque[i]=0,base[i]=i;
q.clear();q.push_back(s);inque[s]=1;
while(!q.empty()){
int u=q.front();q.pop_front();
for(int v=1;v<=n;v++){
if(g[u][v]&&base[v]!=base[u]&&match[u]!=v){
if(v==s||(match[v]!=-1&&pre[match[v]]!=-1))contract(u,v,n);
else if(pre[v]==-1){
pre[v]=u;
if(match[v]!=-1)q.push_back(match[v]),inque[match[v]]=1;
else{
u=v;
while(u!=-1){
v=pre[u];
int w=match[v];
match[u]=v;
match[v]=u;
u=w;
}
return true;
}
}
}
}
}
return false;
}
void calculate()
{
int ans=0;
memset(match,-1,sizeof(match));
for(int i=1;i<=id-1;i++)
if(match[i]==-1&&dfs(i,id-1)) ans++;
if(ans*2==id-1) printf("Yes\n");
else printf("No\n");
}
void init(){
id=1;
memset(G,0,sizeof(G));
memset(g,0,sizeof(g));
for(int i=1;i<=n;i++) V[i].clear();
}
void build(){
for(int i=1;i<=m;i++){
for(auto k:V[E[i].u]) g[k][id]=g[id][k]=1;
id++;
for(auto k:V[E[i].v]) g[k][id]=g[id][k]=1;
g[id][id-1]=g[id-1][id]=1;
id++;
}
}
int main(){
while(~ scanf("%d%d",&n,&m)){
init();
for(int i=1;i<=n;i++){
scanf("%d",&d);
while(d--) V[i].push_back(id++);
}
for(int i=1;i<=m;i++){
scanf("%d%d",&u,&v);
G[u][v]=G[v][u]=1;
E[i].u=u,E[i].v=v;
}
build();
calculate();
}
}