2019-03-16 17:32
北京交通大学 C++ 妈妈说如果昵称太长是...:#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[2000005];
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[2000005];
int num[2000005];
int se[1000005];
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));
int r=0;
for(int i=0;i<n;i++){
scanf("%d %d",&a,&b);
se[++r]=a;
add(a,b);
}
int sum=0;
for(int i=1;i<=r;i++){
if(vis[se[i]]==0){
dfs(se[i]);
sum=max(sum,num[se[i]]-se[i]);
}
// printf("%d\n",num[*ite]);
}
printf("%d\n",sum);
return 0;
} 出题人给的范围不够实际的数要比1000000大,要把数组开大, 数据过多导致set爆掉 dfs过了。
0 点赞 评论 收藏
分享
创作者周榜
更多
关注他的用户也关注了: