Bryce1010模板

http://codeforces.com/contest/1000/problem/E

题意:

给一个无向图,求图的最长直径。

思路:对无向图缩点以后,求图的最长直径

#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn=600010;
int From[maxn],Laxt[maxn],To[maxn<<2],Next[maxn<<2],cnt;
int low[maxn],dfn[maxn],times,q[maxn],head,scc_cnt,scc[maxn];
vector<int>G[maxn];
int dis[maxn],S,T,ans;
void add(int u,int v)
{
Next[++cnt]=Laxt[u]; From[cnt]=u;
Laxt[u]=cnt; To[cnt]=v;
}
void tarjan(int u,int fa)
{
dfn[u]=low[u]=++times;
q[++head]=u;
for(int i=Laxt[u];i;i=Next[i]){
if(To[i]==fa) continue;
if(!dfn[To[i]]) {
tarjan(To[i],u);
low[u]=min(low[u],low[To[i]]);
}
else low[u]=min(low[u],dfn[To[i]]);
}
if(low[u]==dfn[u]){
scc_cnt++;
while(true){
int x=q[head--];
scc[x]=scc_cnt;
if(x==u) break;
}
}
}
void dfs(int u,int f)
{
dis[u]=dis[f]+1;
for(int i=0;i<G[u].size();i++){
if(G[u][i]!=f) dfs(G[u][i],u);
}
}
int main()
{
int N,M,u,v,i,j;
scanf("%d%d",&N,&M);
for(i=1;i<=M;i++){
scanf("%d%d",&u,&v);
add(u,v); add(v,u);
}
tarjan(1,0);
for(i=1;i<=N;i++){
for(j=Laxt[i];j;j=Next[j]){
if(scc[i]!=scc[To[j]])
G[scc[i]].push_back(scc[To[j]]);
}
}
dfs(1,0);
for(i=1;i<=scc_cnt;i++) if(dis[i]>dis[S]) S=i;
dfs(S,0);
for(i=1;i<=scc_cnt;i++) ans=max(ans,dis[i]-1);
printf("%d\n",ans);
return 0;
}

Educational Codeforces Round 46 (Rated for Div. 2) E. We Need More Bosses的更多相关文章

  1. Educational Codeforces Round 46 (Rated for Div. 2) C. Covered Points Count

    Bryce1010模板 http://codeforces.com/problemset/problem/1000/C 题意:问你从[l,r]区间的被多少条线覆盖,列出所有答案. 思路:类似括号匹配的 ...

  2. Educational Codeforces Round 46 (Rated for Div. 2) B. Light It Up

    Bryce1010模板 http://codeforces.com/problemset/problem/1000/B 思路:先用两个数组sumon[]和sumoff[]将亮着的灯和灭的灯累计一下. ...

  3. Educational Codeforces Round 46 (Rated for Div. 2) A. Codehorses T-shirts

    Bryce1010模板 http://codeforces.com/problemset/problem/1000/A 题意: 问你将一种类型的衣服转换成另一种的最小次数. #include<b ...

  4. Educational Codeforces Round 46 (Rated for Div. 2)

    A - Codehorses T-shirts 思路:有相同抵消,没有相同的对答案+1 #include<bits/stdc++.h> #define LL long long #defi ...

  5. Educational Codeforces Round 46 (Rated for Div. 2) D. Yet Another Problem On a Subsequence

    这个题是dp, dp[i]代表以i开始的符合要求的字符串数 j是我们列举出的i之后一个字符串的开始地址,这里的C是组合数 dp[i] += C(j - i - 1, A[i]] )* dp[j]; # ...

  6. Educational Codeforces Round 46 (Rated for Div. 2) D

    dp[i]表示一定包含第I个点的好的子序列个数,那么最终答案就是求dp[0] + dp[1] + .... + dp[n-1] 最终的子序列被分成了很多块,因此很明显我们枚举第一块,第一块和剩下的再去 ...

  7. Educational Codeforces Round 72 (Rated for Div. 2)-D. Coloring Edges-拓扑排序

    Educational Codeforces Round 72 (Rated for Div. 2)-D. Coloring Edges-拓扑排序 [Problem Description] ​ 给你 ...

  8. Educational Codeforces Round 60 (Rated for Div. 2) - C. Magic Ship

    Problem   Educational Codeforces Round 60 (Rated for Div. 2) - C. Magic Ship Time Limit: 2000 mSec P ...

  9. Educational Codeforces Round 60 (Rated for Div. 2) - D. Magic Gems(动态规划+矩阵快速幂)

    Problem   Educational Codeforces Round 60 (Rated for Div. 2) - D. Magic Gems Time Limit: 3000 mSec P ...

随机推荐

  1. win10安装Anaconda+TensorFlow+配置PyCharm

    其实很简单,我这里也只是记录一下而已. 第一大坑:anaconda必须安装4.2以前的版本,不能安装4.3以后的 版本:满满的血泪史 因为我们需要安装自带的python必须是3.5,才可以调用Tens ...

  2. opencv IplImage各参数详细介绍以及如何从一个JPEG图像数据指针转换得到IplImage

    这篇文章里介绍得最清楚了.http://blog.chinaunix.net/uid-22682903-id-1771421.html 关于颜色空间  RGB颜色空间已经非常熟悉了.HSV颜色空间需要 ...

  3. @class && #import

    先前被问到@class和#import的区别,我很直白的说使用@class是对要引用的类进行一个声明,不让编译器报错,到后面要用的时候再引入相应的类,而#import则会引入类的所有实例变量和方法.接 ...

  4. ajax访问json文件缓存问题

    ajax访问json文件,json文件改动,访问的时候也不能及时看到改动后的内容. 这是因为浏览器缓存的原因. 在这时候就需要清除浏览器的缓存或者加上一个标记,让ajax访问文件的时候知道这是一个新的 ...

  5. IDHTTP用法详解 good

    一.IDHTTP的基本用法 IDHttp和WebBrowser一样,都可以实现抓取远端网页的功能,但是http方式更快.更节约资源,缺点是需要手动维护cook,连接等 IDHttp的创建,需要引入ID ...

  6. bootstrap中的less

    一.如何加入变量 引入你的 .less 样式文件的时候要设置 rel 属性值为 “stylesheet/less”: 参考网站:http://www.bootcss.com/p/lesscss/  1 ...

  7. Silverlight中使用MVVM(4)

    Silverlight中使用MVVM(1)--基础 Silverlight中使用MVVM(2)—提高 Silverlight中使用MVVM(3)—进阶 Silverlight中使用MVVM(4)—演练 ...

  8. 初学者对springMVC的认识

    首先是要一定说明的是,这倒是说明是什么?对吧 Spring MVC 是SpringFrameWork的后续产品,并且已经融入到Spring Web Flow中 同时Spring MVC 分离了控制器, ...

  9. python-----实现print不换行

    python中print输出是默认换行的,那如何我们不想换行,且需要用多个print函数输出时,就需要改变print默认换行的属性: 例: print('Hello') print('!') 输出结果 ...

  10. uoj103 apio2014 Palindromes

    题目链接:http://uoj.ac/problem/103 题解: 首先,我们可以用后缀自动机算出每个字符串的出现次数.然后我们可以用manacher找出所有不同的回文串(o(n)个),统计答案即可 ...