草鉴定
这个题调了一天。。
读完题目之后我们不难想出这个题是个
然后问题就落在怎么处理我们走这一次逆向边上。
仔细看题目要求,题目要求我们必须从一号点出发,最后回到一号点。所以我想到了建一个逆向的图,虚拟出
最后我们只要从
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
using namespace std;
const int maxn = 2e5 + 5;
inline int read(){
char ch = getchar();
int f = 1 , x = 0;
while(ch > '9' || ch < '0'){if(ch == '-')f = -1;ch = getchar();}
while(ch >= '0' && ch <= '9'){x = (x << 1) + (x << 3) + ch - '0';ch = getchar();}
return x * f;
}
int n,m,x,y;
int head[maxn],tot,head2[maxn],tot2;
int ans;
struct Edge{
int from,to,next;
}edge[maxn << 1],edge2[maxn << 1];
void add(int u,int v){
edge[++tot].from = u;
edge[tot].to = v;
edge[tot].next = head[u];
head[u] = tot;
}
void add_edge(int u,int v){
edge2[++tot2].from = u;
edge2[tot2].to = v;
edge2[tot2].next = head2[u];
head2[u] = tot2;
}
int dfn[maxn],low[maxn],ind;
int belong[maxn],num[maxn],cnt,stack[maxn],top;
bool ins[maxn];
void tarjan(int x){
low[x] = dfn[x] = ++ind;
ins[x] = true;
stack[++top] = x;
for(int i=head[x];i;i=edge[i].next){
int v = edge[i].to;
if(ins[v]) low[x] = min(low[x] , dfn[v]);
if(!dfn[v]) {
tarjan(v);
low[x] = min(low[x] , low[v]);
}
}
int k = 0;
if(dfn[x] == low[x]){
cnt++;
do{
k = stack[top];
num[cnt]++;
top--;
ins[k] = false;
belong[k] = cnt;
} while(k != x);
}
}
int dis[maxn];
bool vis[maxn];
void spfa(int s){
queue<int> q;
q.push(s);
dis[s] = 0;
vis[s] = true;
while(!q.empty()){
int cur = q.front();
q.pop(); vis[cur] = false;
for(int i=head2[cur];i;i=edge2[i].next){
int v = edge2[i].to;
if(dis[v] < dis[cur] + num[cur]){
dis[v] = dis[cur] + num[cur];
if(vis[v] == 0){
q.push(v);
vis[v] = true;
}
}
}
}
}
int main(){
n = read(); m = read();
for(int i=1;i<=m;i++){
x = read(); y =read();
add(x , y);
}
for(int i=1;i<=n;i++)
if(!dfn[i]) tarjan(i);
for(int i=1;i<=n;i++)
num[i + cnt] = num[i];
for(int i=1;i<=m;i++)
if(belong[edge[i].from] != belong[edge[i].to]){
add_edge(belong[edge[i].from] , belong[edge[i].to]);
add_edge(belong[edge[i].to] , belong[edge[i].from] + cnt);
add_edge(belong[edge[i].from] + cnt , belong[edge[i].to] + cnt);
}
spfa(belong[1]);
printf("%d\n",dis[belong[1] + cnt]);
return 0;
}