Educational Codeforces Round 46 (Rated for Div. 2) E. We Need More Bosses
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的更多相关文章
- Educational Codeforces Round 46 (Rated for Div. 2) C. Covered Points Count
Bryce1010模板 http://codeforces.com/problemset/problem/1000/C 题意:问你从[l,r]区间的被多少条线覆盖,列出所有答案. 思路:类似括号匹配的 ...
- Educational Codeforces Round 46 (Rated for Div. 2) B. Light It Up
Bryce1010模板 http://codeforces.com/problemset/problem/1000/B 思路:先用两个数组sumon[]和sumoff[]将亮着的灯和灭的灯累计一下. ...
- Educational Codeforces Round 46 (Rated for Div. 2) A. Codehorses T-shirts
Bryce1010模板 http://codeforces.com/problemset/problem/1000/A 题意: 问你将一种类型的衣服转换成另一种的最小次数. #include<b ...
- Educational Codeforces Round 46 (Rated for Div. 2)
A - Codehorses T-shirts 思路:有相同抵消,没有相同的对答案+1 #include<bits/stdc++.h> #define LL long long #defi ...
- 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]; # ...
- Educational Codeforces Round 46 (Rated for Div. 2) D
dp[i]表示一定包含第I个点的好的子序列个数,那么最终答案就是求dp[0] + dp[1] + .... + dp[n-1] 最终的子序列被分成了很多块,因此很明显我们枚举第一块,第一块和剩下的再去 ...
- Educational Codeforces Round 72 (Rated for Div. 2)-D. Coloring Edges-拓扑排序
Educational Codeforces Round 72 (Rated for Div. 2)-D. Coloring Edges-拓扑排序 [Problem Description] 给你 ...
- 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 ...
- 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 ...
随机推荐
- log Configuration
Log4j – Configuring Log4j 2 - Apache Log4j 2 https://logging.apache.org/log4j/2.x/manual/configurati ...
- this that 时间戳转日期 小程序 列表 与 加载
var gd = getApp().globalData; var imgUrlApp = gd.imgUrlApp; var localImgPath = gd.localImgPath; var ...
- 把x指针指向的4个字节次序颠倒过来
举例:x指向的内存地址,其字节内容从低到高依次分别为c1,c2,c3,c4(Delphi读取一个integer的时候,结果是c4c3c2c1,其排列规则是"高高低低"),那么结果是 ...
- Android应用之——最新版本号SDK V2.4实现QQ第三方登录
为什么要写这篇博客呢?由于.我在做这个第三方登录的时候,找了非常多资料,发现要么就是过时了.要么就是说的非常不清楚.非常罗嗦.并且非常多都是一些小demo,不是什么实例.甚至连腾讯官方的文档都有这个问 ...
- gson如何转化json数组
String.JsonObject.JavaBean 互相转换 User user = new Gson().fromJson(jsonObject, User.class); User user = ...
- HDU3081 Marriage Match II —— 传递闭包 + 二分图最大匹配 or 传递闭包 + 二分 + 最大流
题目链接:https://vjudge.net/problem/HDU-3081 Marriage Match II Time Limit: 2000/1000 MS (Java/Others) ...
- 广大暑假训练1 E题 Paid Roads(poj 3411) 解题报告
题目链接:http://poj.org/problem?id=3411 题目意思:N个city 由 m 条路连接,对于一条路(假设连接Cityia和 Cityb),如果从Citya 去 Cityb的途 ...
- set STL 简单说说
set 这个容器,可以排序,以及去掉重复的东西 #include<bits/stdc++.h> using namespace std; int main() { string s; se ...
- Opencv实现简易播放器
实现了在MFC中显示图片,再要显示一个视频就是轻而易举的事了,本篇介绍使用Opencv制作一个简易的播放器,实现打开文件.暂停.继续播放.再次播放和总\当前帧数显示功能. 首先还是先看一下界面效果: ...
- Superprime Rib
链接 分析:满足题目条件的必然是1,2,3,5,7,9这几个数字的组合,DFS按位进行即可,边组合边判断是否合法. /* PROB:sprime ID:wanghan LANG:C++ */ #inc ...