/ Randle /

记录详情

Runtime Error


  
# 状态 耗时 内存占用
#1 Accepted 64ms 30.344 MiB
#2 Accepted 59ms 30.434 MiB
#3 Accepted 62ms 30.25 MiB
#4 Accepted 62ms 28.25 MiB
#5 Accepted 59ms 28.25 MiB
#6 Accepted 61ms 30.25 MiB
#7 Accepted 65ms 30.25 MiB
#8 Accepted 65ms 30.469 MiB
#9 Accepted 19ms 30.375 MiB
#10 Accepted 19ms 30.359 MiB
#11 Accepted 42ms 35.348 MiB
#12 Accepted 53ms 36.25 MiB
#13 Accepted 22ms 33.375 MiB
#14 Runtime Error 281ms 40.535 MiB
#15 Accepted 314ms 43.852 MiB
#16 Accepted 280ms 44.527 MiB
#17 Accepted 287ms 45.074 MiB
#18 Accepted 400ms 40.902 MiB
#19 Accepted 416ms 41.082 MiB
#20 Accepted 414ms 36.336 MiB

代码

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<queue>
using namespace std;
const int maxn = 1e6 + 1e4, maxm = 1e6 + 1e4;
int Index,pd[maxn],DFN[maxn],LOW[maxn];
int tot,color[maxn],sum[maxn],f[maxn];
int edge,fir[maxn],Next[maxm],to[maxm];
int sta[maxn],top; //手写栈 
int n,m,val[maxn],x[maxm],y[maxm],ans;
int read(){
    int x=0,f=1; char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1; ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-48; ch=getchar();}
    return f*x;    
}
void add(int x,int y){
    to[++edge]=y; Next[edge]=fir[x]; fir[x]=edge;
}
void tarjan(int x){  
    sta[++top]=x;
    pd[x]=1;
    DFN[x]=LOW[x]= ++Index;
    for(int i=fir[x];i;i=Next[i]){
        int v=to[i];
        if(!DFN[v]){
            tarjan(v);
            LOW[x]=min(LOW[x],LOW[v]);
        }
        else if(pd[v]){
            LOW[x]=min(LOW[x],DFN[v]);
        }
    }
    if(DFN[x]==LOW[x]){
        tot++;
        while(sta[top+1]!=x){
            color[sta[top]]=tot;
            sum[tot]+=val[sta[top]];
            pd[sta[top--]]=0;
        }
    }
}
void search(int x){   //在DAG上记忆化搜索
    if(f[x]) return ;
    f[x]=sum[x];
    int maxsum = 0;
    for(int i=fir[x];i;i=Next[i]){
        if(!f[to[i]]) search(to[i]);
        maxsum=max(maxsum,f[to[i]]);
    }
    f[x]+=maxsum;
}
int main(){
    n=read(); m=read();
    for(int i=1;i<=n;i++)val[i]=1;
    for(int i=1;i<=m;i++){
        x[i]=read();                                // 这里用数组保存好原边,便于后续重新建图;
        y[i]=read();
        add(x[i],y[i]);
    }
    for(int i=1;i<=n;i++) if(!DFN[i]) tarjan(i);
    memset(fir,0,sizeof(fir));                 //清空原图
    memset(Next,0,sizeof(Next));
    memset(to,0,sizeof(to));
    edge=0;
    for(int i=1;i<=m;i++){       //重新建图(枚举每一条原边,若不在同一强连通分量里,则连一条边(方向同原边))
        if(color[x[i]]!=color[y[i]])
            add(color[x[i]],color[y[i]]);
    }
    for(int i=1;i<=tot;i++){  
        if(!f[i]){
            search(i);
            ans=max(ans,f[i]);
        }
    }
    printf("%d",ans);
    return 0;
}

信息

递交者
类型
递交
题目
BOMB炸弹(CQ直辖市noip模拟赛联考) T1
题目数据
下载
语言
C++
递交时间
2017-11-04 19:35:12
评测时间
2017-11-04 19:35:12
评测机
分数
95
总耗时
3053ms
峰值内存
45.074 MiB