68pts 求助
查看原帖
68pts 求助
759710
LuoFeng_Nanami楼主2023/10/19 22:37

题解 Tarjan 好抽象,看不懂,再看一遍,更抽象了。
不知道这种 Tarjan 为什么会错。
不知道是不是 Tarjan 的错。

#include<bits/stdc++.h>
#define ll long long
#define rll register ll
#define F(i,a,b) for(rll i=a;i<=b;i++)
#define Fdn(i,a,b) for(rll i=a;i>=b;i--)
#define int ll
#define itn int
#define Sort sort(a + 1,a + n + 1)
#define SortCmp sort(a + 1,a + n + 1,cmp)
#define R(n) n = read()
#define Bk break
#define Pc putchar

using namespace std;
namespace IO{
	char ibuf[(1<<20)+1],*iS,*iT;
	#if ONLINE_JUDGE
	#define gh() (iS==iT?iT=(iS=ibuf)+fread(ibuf,1,(1<<20)+1,stdin),(iS==iT?EOF:*iS++):*iS++)
 	#else
	#define gh() getchar()
	#endif
	#define reg register
	inline ll read(){
		reg char ch=gh();
		reg ll x=0;
		reg char t=0;
		while(ch<'0'||ch>'9')   t|=ch=='-',ch=gh();
		while(ch>='0'&&ch<='9') x=x*10+(ch^48),ch=gh();
		return t?-x:x;
	}
	inline void writ(ll x)
	{
		if(x < 0){
			putchar('-');
			x = -x;
		}
		if(x > 9) 
			writ(x / 10);
		putchar(x % 10 + '0');
	}
	inline void write(ll x,char c)
	{
		writ(x);
		putchar(c);
	}
}
using IO::read;
using IO::writ;
using IO::write;
#define umap unordered_map
#define uset unordered_set
#define ld long double
#define pii pair<int,int>
#define pll pair<long long,long long>
#define pb push_back
const ll ll_INF=9223372036854775807;
const int INF = 0x7fffffff;
namespace mySTL{
	inline ll max(ll a,ll b){return a>b?a:b;}
	inline ll min(ll a,ll b){return a<b?a:b;}
	inline ld min(ld a,ld b){return a<b?a:b;}
	inline ld max(ld a,ld b){return a>b?a:b;}
	inline void swap(ll &a,ll &b){a^=b,b^=a,a^=b;}
	inline ll pw(ll a,ll b,ll p){if(b==0)return 1;
	if(b==1)return a%p;
	ll mid=pw(a,b/2,p)%p;
	if(b&1)return mid*mid%p*a%p;else{return mid*mid%p;}}
	inline ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
	inline ll lcm(ll a,ll b){return a*b/gcd(a,b);}
}
using namespace mySTL;

const int inf = 0x3f3f3f3f,mod = 1e9 + 7; 
const int maxn = 5e5 + 7, maxm = 2e6 + 7;

struct edge{
	int to, nxt;
}G[maxm << 1];

int head[maxn];
int tot = 1;
int n, m;

inline void add(int u, int v){ G[++tot].to = v, G[tot].nxt = head[u], head[u] = tot; }

int dfn[maxn], low[maxn];
bool cut[maxn];
int __dfn;
int root;
int stk[maxn];
int tp;
int cnt;
int dcc[maxn];
int deg[maxn];
int sum[maxn];

inline void Tarjan(int u){
	dfn[u] = low[u] = ++__dfn;
	stk[++tp] = u;
	int fl = 0; 
	if(u == root && head[u] == 0){
		dcc[u] = ++cnt;
		return;
	}
	for(int i = head[u]; i; i = G[i].nxt){
		int v = G[i].to;
		if(!dfn[v]){
			Tarjan(v);
			low[u] = min(low[u], low[v]);
			if(low[v] >= dfn[u]){
				fl++;
				if(u != root || fl > 1)
					cut[u] = 1;
				++cnt;
				while(stk[tp + 1] != v) 
					dcc[stk[tp--]] = cnt;
				dcc[u] = cnt;
			}
			
		} else
			low[u] = min(low[u], dfn[v]);
	}
	return;
}

bool vis[maxn];

inline void getsum(int u){
	sum[dcc[u]]++;
	for(int i = head[u]; i; i = G[i].nxt){
		int v = G[i].to;
		if(vis[v]) continue;
		vis[v] = 1;
		getsum(v);
	}
	return;
}

signed main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	
	R(n), R(m);
	F(i, 1, m){
		int u = read(), v = read();
		add(u, v);
	}
	F(i, 1, n)
		if(!dfn[i])
			root = i, Tarjan(i);
	F(i, 1, n)
		if(!vis[i])
			vis[i] = 1, getsum(i);
	F(u, 1, n)
		for(int i = head[u]; i; i = G[i].nxt){
			int v = G[i].to;
			if(dcc[u] != dcc[v])
				deg[dcc[u]]++;
		}
	int fl = 0;
	F(i, 1, cnt){
		if(deg[i] == 0){
			if(fl){
				write(0, '\n');
				return 0;
			}
			fl = i;
		}
	}
	write(sum[fl], '\n');
	
	return 0;
}
2023/10/19 22:37
加载中...