关注
#include<bits/stdc++.h>
#include<math.h>
#include<map>
#include<queue>
#include<stack>
#include<set>
#include<map>
#include<assert.h>
#include <algorithm>
#define ll long long
#include <time.h>
using namespace std;
//clock_t Begin,End;
//Begin = clock();printf("%dms\n",End-Begin);
int father[1000005];
struct ss{
int next;
int to;
}arr[1000005];
int tot;
void add(int a,int b){
arr[tot].next=father[a];
arr[tot].to=b;
father[a]=tot++;
}
int vis[1000005];
int num[1000005];
set<int>se;
int dfs(int t){
vis[t]=1;
int tt=-1;
for(int i=father[t];i!=-1;i=arr[i].next){
int to=arr[i].to;
if(vis[to]==0){
tt=max(tt,dfs(to));
}else{
tt=max(tt,num[to]);
}
}
if(tt==-1) return t;
else return num[t]=tt;
}
int max(int a,int b){
return a>b?a:b;
}
int main(){
int n;
scanf("%d",&n);
int a,b;
memset(father,-1,sizeof(father));
for(int i=0;i<n;i++){
scanf("%d %d",&a,&b);
se.insert(a);
if(a>b) swap(a,b);
add(a,b);
}
int sum=0;
for(set<int>::iterator ite=se.begin();ite!=se.end();ite++){
if(vis[*ite]==0){
dfs(*ite);
sum=max(sum,num[*ite]-*ite);
}
// printf("%d\n",num[*ite]);
}
printf("%d\n",sum);
return 0;
}
查看原帖
点赞 评论
相关推荐
牛客热帖
更多
正在热议
更多
# mt对你说过最有启发的一句话 #
4154次浏览 68人参与
# 考研失败就一定是坏事吗? #
160619次浏览 1141人参与
# 被上班搭子“传染”了哪些习惯 #
1629次浏览 50人参与
# 今年秋招你收到了多少封邮件? #
3748次浏览 78人参与
# 工作后,你落下了哪些病根 #
4034次浏览 101人参与
# 秋招特别不鸣谢 #
3098次浏览 46人参与
# 非技术2024笔面经 #
446535次浏览 4911人参与
# 选实习,你更看重哪方面? #
3171次浏览 59人参与
# 工作后明白的那些道理 #
36464次浏览 494人参与
# 什么是优秀的实习经历 #
1348次浏览 52人参与
# 巨人网络求职进展汇总 #
181534次浏览 1214人参与
# 摸鱼被leader发现了怎么办 #
77197次浏览 449人参与
# 工作中遇到的歹人 #
6347次浏览 117人参与
# 你见过最离谱的招聘要求是什么? #
246474次浏览 1697人参与
# 秋招感动瞬间 #
109720次浏览 497人参与
# 选完offer后,你后悔学机械吗? #
49297次浏览 270人参与
# 当发现同事想辞职 #
12388次浏览 39人参与
# 校招泡的最久的公司是哪家? #
46045次浏览 172人参与
# 分享一个让你热爱工作的瞬间 #
53362次浏览 467人参与
# 上班到公司第一件事做什么? #
115585次浏览 811人参与
海康威视公司福利 1409人发布