比赛 cmath生日赛 评测结果 AAAAAAAAAAAAAAAA
题目名称 膜拜 最终得分 100
用户昵称 Sky_miner 运行时间 0.282 s
代码语言 C++ 内存使用 12.79 MiB
提交时间 2017-06-13 20:40:54
显示代码纯文本
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long ll;
inline void read(int &x){
    x=0;char ch;bool flag = false;
    while(ch=getchar(),ch<'!');if(ch == '-') ch=getchar(),flag = true;
    while(x=10*x+ch-'0',ch=getchar(),ch>'!');if(flag) x=-x;
}
#define rg register int
#define rep(i,a,b) for(rg i=(a);i<=(b);++i)
#define per(i,a,b) for(rg i=(a);i>=(b);--i)
const int maxn = 100010;
struct Edge{
    int to,next;
}G[maxn];
int head[maxn],cnt;
void add(int u,int v){
    //printf("ade %d %d\n",u,v);
    G[++cnt].to = v;
    G[cnt].next = head[u];
    head[u] = cnt;
}
int dfn[maxn],low[maxn],dfs_clock;
int sta[maxn],top,belong[maxn];
int scc_cnt,siz[maxn];
#define v G[i].to
void dfs(int u){
    dfn[u] = low[u] = ++ dfs_clock;
    sta[++top] = u;
    for(rg i = head[u];i;i=G[i].next){
	if(!dfn[v]){
	    dfs(v);
	    low[u] = min(low[u],low[v]);
	}else if(!belong[v]) low[u] = min(low[u],dfn[v]);
    }
    //printf("low[%d] = %d,dfn[%d] = %d\n",u,low[u],u,dfn[u]);
    if(dfn[u] == low[u]){
	++ scc_cnt;
	while(1){
	    int x = sta[top--];
	    belong[x] = scc_cnt;
	    siz[scc_cnt] ++ ;
	    if(x == u) break;
	}
    }
}
#undef v
struct Topu{
    struct Edge{
	int to,next;
    }G[maxn];
    int head[maxn],cnt,deg[maxn];
    void add(int u,int v){
	G[++cnt].to = v;
	G[cnt].next = head[u];
	head[u] = cnt;
	++ deg[v];
    }
#define v G[i].to
    int q[maxn],l,r,f[maxn];
    void bfs(){
	memset(f,-0x3f,sizeof f);
	f[belong[1]] = siz[belong[1]];l = 0;r = -1;
	rep(i,1,scc_cnt){
	    if(deg[i] == 0) q[++r] = i;
	}
	while(l <= r){
	    int u = q[l++];
	    for(rg i = head[u];i;i=G[i].next){
		f[v] = max(f[v],f[u] + siz[v]);
		if(-- deg[v] == 0) q[++r] = v;
	    }
	}return ;
    }
#undef v
}a,b;
struct Node{
    int u,v;
}e[maxn];
int main(){
    freopen("OrzOrz.in","r",stdin);
    freopen("OrzOrz.out","w",stdout);
    int n,m;read(n);read(m);
    int u,v;
    rep(i,1,m){
	read(u);read(v);
	e[i].u = u;e[i].v = v;
	add(u,v);
    }
    rep(i,1,n) if(!dfn[i]) dfs(i);
    rep(i,1,m){
	if(belong[e[i].u] == belong[e[i].v]) continue;
	a.add(belong[e[i].u],belong[e[i].v]);
	b.add(belong[e[i].v],belong[e[i].u]);
    }a.bfs();b.bfs();
    int ans = siz[belong[1]] << 1;
    rep(i,1,m){
	if(belong[e[i].u] == belong[e[i].v]) continue;
	ans = max(ans,a.f[belong[e[i].v]] + b.f[belong[e[i].u]]);
    }
    printf("%d\n",ans - siz[belong[1]]);
    return 0;
}