[二分图 图的直径] Codeforces 542E VK Cup 2015 - Round 3 E.Playing on Graph
来源:互联网 发布:linux脚本启动程序 编辑:程序博客网 时间:2024/06/05 22:33
存在奇环就一定无解。
对于一个二分图,最长链为这个图的直径。
将所有连通块的直径相加
#include<cstdio>#include<cstdlib>#include<algorithm>#include<cstring>using namespace std;inline char nc(){ static char buf[100000],*p1=buf,*p2=buf; if (p1==p2) { p2=(p1=buf)+fread(buf,1,100000,stdin); if (p1==p2) return EOF; } return *p1++;}inline void read(int &x){ char c=nc(),b=1; for (;!(c>='0' && c<='9');c=nc()) if (c=='-') b=-1; for (x=0;c>='0' && c<='9';x=x*10+c-'0',c=nc()); x*=b;}const int N=1003;const int M=100005;struct edge{ int u,v,next;}G[M<<1];int head[N],inum;inline void add(int u,int v,int p){ G[p].u=u; G[p].v=v; G[p].next=head[u]; head[u]=p;}int clr[N],dis[N];int belong[N],cnt;#define V G[p].vint tag=0;inline void dfs(int u){ belong[u]=cnt; for (int p=head[u];p;p=G[p].next) if (!belong[V]) clr[V]=clr[u]^1,dfs(V); else if (clr[V]==clr[u])tag=1;}int n,m;int Q[N],l,r;inline void bfs(int S){ memset(dis,-1,sizeof(dis)); l=r=-1; dis[S]=0; Q[++r]=S; while (l<r){ int u=Q[++l]; for (int p=head[u];p;p=G[p].next) if (dis[V]==-1)dis[V]=dis[u]+1,Q[++r]=V; }}int ans[N];int main(){ int iu,iv; freopen("t.in","r",stdin); freopen("t.out","w",stdout); read(n); read(m); for (int i=1;i<=m;i++) read(iu),read(iv),add(iu,iv,++inum),add(iv,iu,++inum); for (int i=1;i<=n;i++) if (!belong[i]) ++cnt,dfs(i); if (tag) return printf("-1\n"),0; for (int i=1;i<=n;i++){ bfs(i); for (int j=1;j<=n;j++) ans[belong[i]]=max(ans[belong[i]],dis[j]); } int Ans=0; for (int i=1;i<=cnt;i++) Ans+=ans[i]; printf("%d\n",Ans);}
0 0
- [二分图 图的直径] Codeforces 542E VK Cup 2015 - Round 3 E.Playing on Graph
- Codeforces VK Cup 2015 - Round 1 (Div.1 A~E)
- [线段树 杂题] Codeforces 806E VK Cup 2017 Round 3 E. Blog Post Rating
- Codeforces Round #423 (Div. 1, rated, based on VK Cup Finals) E. Rusty String(fft)
- Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals) E Cards Sorting
- 【瞎搞】 VK Cup 2015 - Round 2 E. Correcting Mistakes
- Codeforces Round #423 (Div. 2, rated, based on VK Cup Finals) E. DNA Evolution 分组+树状数组
- VK Cup 2012 Qualification Round 1 E
- [线段树] [二分图] Codeforces VK Cup 2015
- Codeforces Round #351 (VK Cup 2016 Round 3, Div. 1 Edition) C E (斜率优化. 概率)
- Codeforces Round #348 (VK Cup 2016 Round 2) E F (2-sat. 待补)
- Codeforces Round #412 (rated, Div. 2, base on VK Cup 2017 Round 3) C. Success Rate(数学,二分)
- [2-SAT] Codeforces 668E #348 (VK Cup 2016 Round 2, Div. 1 Edition) E. Little Artem and 2-SAT
- 【dp】VK Cup 2012 Qualification Round 1 ——E
- VK Cup 2012 Qualification Round 1 ( E Phone Talks)
- VK Cup 2012 Qualification Round 2 E 无语凝噎
- Codeforces Round #351 (VK Cup 2016 Round 3, Div. 2 Edition) E. Levels and Regions(斜率优化dp) ★ ★ ★
- Codeforces Round #348 (VK Cup 2016 Round 2, Div. 2 Edition) E. Little Artem and Time Machine 树状数组
- window下装ubuntu虚拟机
- 【DP】 摆花
- http协议
- 线上应用故障排查之二:高内存占用
- 切面条问题
- [二分图 图的直径] Codeforces 542E VK Cup 2015 - Round 3 E.Playing on Graph
- 10.1引用数据类型的转换
- #!/usr/bin/env python与#!/usr/bin/python
- 自定view实践(一) 自定义loadingView
- android 观察者模式
- Android开发之蓝牙(Bluetooth)---源码目录
- php + jquery 利用 smtp 实现发送邮件功能
- expdp / impdp 用法详解
- php中删除数组中的某一个unset和array_splice的区别