COOHxNH2
来源:互联网 发布:android 高仿淘宝 编辑:程序博客网 时间:2024/04/25 15:07
题目描述
随便搞搞
显然如果有奇环我们就GG。
如果没有奇环我们是一定能对原图黑白染色的。
于是枚举一个点,从其出发bfs建分层图。
那么同一层都可以缩在一起,最长链是最大深度。
然后就做完了。
#include<cstdio>#include<algorithm>#define fo(i,a,b) for(i=a;i<=b;i++)using namespace std;const int maxn=1000+10,maxm=10000+10;int belong[maxn],mx[maxn],co[maxn],d[maxn],dl[maxn],h[maxn],go[maxm*2],next[maxm*2];int i,j,k,l,t,n,m,tot,top,ans;bool czy,bz[maxn];void add(int x,int y){ go[++tot]=y; next[tot]=h[x]; h[x]=tot;}void dfs(int x,int y){ belong[x]=top; co[x]=y; int t=h[x]; while (t){ if (co[go[t]]==y){ czy=1; return; } else if (co[go[t]]==-1) dfs(go[t],y^1); if (czy) return; t=next[t]; }}void bfs(int x){ int i,k=0,l=1,t,now; fo(i,1,n) bz[i]=0; d[x]=0; dl[1]=x; bz[x]=1; while (k<l){ k++; now=dl[k]; t=h[now]; while (t){ if (!bz[go[t]]){ d[go[t]]=d[now]+1; dl[++l]=go[t]; bz[go[t]]=1; } t=next[t]; } }}int main(){ scanf("%d%d",&n,&m); fo(i,1,n) co[i]=-1; fo(i,1,m){ scanf("%d%d",&j,&k); add(j,k); add(k,j); } fo(i,1,n) if (co[i]==-1){ top++; dfs(i,0); } if (czy) printf("-1\n"); else{ fo(i,1,n){ bfs(i); fo(j,1,n) if (belong[i]==belong[j]) mx[belong[i]]=max(mx[belong[i]],d[j]); } fo(i,1,top) ans+=mx[i]; printf("%d\n",ans); }}
0 0