流星雨问题,

为什么不能dfs搜索能去的点,过的代码都是cmp。。。。。。。
愁死我了
全部评论
#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过了。
点赞 回复 分享
发布于 2019-03-16 20:02
所以说这题目想过,就只能按错的来,因为容忍一颗可以倒着跑的流星的话,所有过的代码就都是错的了。只能说是数据真的是错得离谱
点赞 回复 分享
发布于 2022-02-17 15:15
题目数据有错,你的第一份代码之所以过,是因为if(a>b) swap(a,b);这个部分,数据中的错误是有部分流星出现的时间x大于消失的时间y,而这是不可能的,你第二份代码正是因为没有这个比较交换部分才过了
点赞 回复 分享
发布于 2022-02-17 15:13
#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; }
点赞 回复 分享
发布于 2019-03-16 17:51
dfs会超时的吧,数据太大了
点赞 回复 分享
发布于 2019-03-16 17:42

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务