#include<bits/stdc++.h>
#define ite vector<Edge>::iterator
#define ith list<int>::iterator
using namespace std;
const int INF=INT_MAX;
int n,m,s,t,u,v,w,highest,now_height,tmp,x;
struct Edge
{
int next,targ,wght;
Edge(int n,int t,int w): next(n),targ(t),wght(w) {}
};
queue<int>q;
list<int>h[100005];
vector<int>l,ph,gap,bs[100005];
vector<Edge>head[100005];
vector<list<int>::iterator>it;
void add(int u,int v,int w)
{
head[u].push_back(Edge(head[v].size(),v,w));
head[v].push_back(Edge(head[u].size()-1,u,0));
}
void relabel()
{
gap.assign(n+5,0);
ph.assign(n+5,n);
ph[t]=0;
q.push(t);
while(!q.empty())
{
tmp=q.front();
q.pop();
for(ite i=head[tmp].begin();i!=head[tmp].end();i++)
if(ph[i->targ]==n && head[i->targ][i->next].wght)
{
ph[i->targ]=ph[tmp]+1;
gap[ph[i->targ]]++;
q.push(i->targ);
}
}
for(int i=s;i<=t;i++)
{
bs[i].clear();
h[i].clear();
}
for(int i=s;i<=t;i++)
if(ph[i]<n)
{
it[i]=h[ph[i]].insert(h[ph[i]].begin(),i);
if(l[i])
bs[ph[i]].push_back(i);
}
highest=now_height=ph[tmp];
}
void push(int u,Edge &e)
{
int v=e.targ,df=min(l[u],e.wght);
l[u]-=df;
l[v]+=df;
e.wght-=df;
head[v][e.next].wght+=df;
if(l[v] && l[v]==df)
bs[ph[v]].push_back(v);
}
void push(int u)
{
int new_height=n,u_height=ph[u];
for(ite i=head[u].begin();i!=head[u].end();i++)
if(i->wght)
{
if(ph[i->targ]==ph[u]-1)
{
push(u,*i);
if(!l[u])
return;
}
else
new_height=min(new_height,ph[i->targ]+1);
}
if(gap[u_height]==1)
{
for(int i=u_height;i<=highest;i++)
for(ith p=h[i].begin();p!=h[i].end();p++)
{
gap[ph[*p]]--;
ph[*p]=n;
}
highest=u_height-1;
}
else
{
gap[u_height]--;
it[u]=h[u_height].erase(it[u]);
ph[u]=new_height;
if(ph[u]==n)
return;
gap[ph[u]]++;
it[u]=h[ph[u]].insert(h[ph[u]].begin(),u);
highest=max(highest,now_height=new_height);
bs[ph[u]].push_back(u);
}
}
int HLPP()
{
now_height=highest=0;
ph.assign(n+5,0);
ph[s]=n;
it.resize(n+5);
for(int i=s;i<=t;i++)
if(i!=s)
it[i]=h[ph[i]].insert(h[ph[i]].begin(),i);
gap.assign(n+5,0);
gap[0]=n-1;
l.assign(n+5,0);
l[s]=INF;
l[t]=-INF;
for(ite i=head[s].begin();i!=head[s].end();i++)
push(s,*i);
relabel();
for(int i;now_height;)
{
if(bs[now_height].empty())
now_height--;
else
{
i=bs[now_height].back();
bs[now_height].pop_back();
push(i);
}
}
return l[t]+INF;
}
int main()
{
// cin>>n>>m>>s>>t;
// for(int i=1;i<=m;i++)
// {
// cin>>u>>v>>w;
// add(u,v,w);
// }
// cout<<HLPP();
cin>>n>>m;
while(n && m)
{
s=0;
t=n+1;
for(int i=s;i<=t;i++)
head[i].clear();
for(int i=1;i<=n;i++)
{
cin>>x;
if(x)
add(i,t,1);
else
add(s,i,1);
}
for(int i=1;i<=m;i++)
{
cin>>u>>v;
add(u,v,1);
add(v,u,1);
}
n+=2;
cout<<HLPP()<<endl;
cin>>n>>m;
}
}
从善意的投票复制来的
只改了多组数据输入
但是还是wa了
没搞懂错哪里了