关注
#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;
}
查看原帖
点赞 评论
相关推荐
牛客热帖
更多
正在热议
更多
# 帮我看看,领导说这话什么意思? #
15724次浏览 87人参与
# 牛友的志愿填报指南 #
33562次浏览 180人参与
# 快手技术岗信息交流阵地 #
133次浏览 0人参与
# 你的mentor是什么样的人? #
12447次浏览 97人参与
# 毕业租房也有小确幸 #
140262次浏览 4491人参与
# 怎么给家人解释你的工作? #
7613次浏览 57人参与
# 得物app工作体验 #
27167次浏览 61人参与
# 租房前辈的忠告 #
259160次浏览 7114人参与
# 国企还是互联网,你怎么选? #
167627次浏览 1191人参与
# 求职中的尴尬瞬间 #
1672次浏览 25人参与
# 小红书求职进展汇总 #
120494次浏览 952人参与
# 薪资爆料 #
199957次浏览 1512人参与
# 校招泡的最久的公司是哪家? #
10248次浏览 66人参与
# 求职低谷期你是怎么度过的 #
10197次浏览 199人参与
# 26届秋招公司红黑榜 #
24258次浏览 88人参与
# 从哪些方向判断这个offer值不值得去? #
12887次浏览 158人参与
# 度小满求职进展汇总 #
11911次浏览 64人参与
# 你觉得mentor喜欢什么样的实习生 #
14841次浏览 392人参与
# 牛客树洞,我想对你说 #
4299次浏览 63人参与
# 还记得你第一次面试吗? #
340030次浏览 3876人参与
# 机械人的秋招小目标 #
22599次浏览 217人参与