Tarjan 【整理】

Tarjan 【整理】

#include<bits/stdc++.h>
using namespace std;

class FastIO{
/* copyright (c) dgklr 2019. All rights reserved. */
	bool if_debug = 0;
	char st[70]; // stack
	int pl;
#ifdef linux
	#define putc(x) putchar_unlocked(x)
	#define getc() getchar_unlocked()
#else
	#define putc(x) putchar(x)
	#define getc() getchar()
#endif
#define endl '\n' // I don't have the authority to get this.
public:
	FastIO operator << (long long x){
		pl = 0;
		if (x == 0) putc('0');
		if (x < 0) putc('-');
		while (x != 0)
			st[++pl] = x % 10 + 48, x /= 10;
		while (pl != 0)
			putc(st[pl]), pl --;
		return (*this);
	}
	FastIO operator << (int x){
		pl = 0;
		if (x == 0) putc('0');
		if (x < 0) putc('-');
		while (x != 0)
			st[++pl] = x % 10 + 48, x /= 10;
		while (pl != 0)
			putc(st[pl]), pl --;
		return (*this);
	}
	FastIO operator << (char c){
		putc(c);
		return (*this);
	}
	FastIO operator << (string s){
		for (string::iterator it = s.begin(); it != s.end(); it ++)
			putc(*it);
	}
	FastIO operator << (char *c){
		int pl = 0;
		while (c[pl] != '\0') putc(c[pl++]);
	}
	template <typename T>
	FastIO operator << (vector <T> &a){
		putc('[');
		typedef typename vector <T> :: iterator Iter;
		if (a.begin() == a.end()) putc(']');
		else{
			(*this) << *a.begin();
			for (Iter it = ++a.begin(); it != a.end(); it ++)
				(*this) << ", " << *it;
			putc(']');
		}
	}
	template <typename T>
	FastIO operator << (set <T> &a){
		putc('[');
		typedef typename set <T> :: iterator Iter;
		if (a.begin() == a.end()) putc(']');
		else{
			(*this) << *a.begin();
			for (Iter it = ++a.begin(); it != a.end(); it ++)
				(*this) << ", " << *it;
			putc(']');
		}
	}
	template <typename S, typename T>
	FastIO operator << (map <S, T> &a){
		putc('[');
		typedef typename map <S, T> :: iterator Iter;
		if (a.begin() == a.end()) putc(']');
		else{
			(*this) << a.begin() -> first << ':' << a.begin() -> second;
			for (Iter it = ++a.begin(); it != a.end(); it ++)
			(*this) << ", " << it -> first << ':' << it -> second;
			putc(']');
		}
	}
	template <typename S, typename T>
	FastIO operator << (pair <S, T> &Pt){
		putc('<'); (*this) << Pt.first; putc(','); putc(' '); (*this) << Pt.second; putc('>');
	}
	FastIO operator >> (long long &x){
		x = 0;
		register char c;
		register int w = 1;
		while (c = getc(), c < '0' || c > '9') if (c == '-') w = -1;
		while (c >= '0' && c <= '9') x = x * 10 + c - 48, c = getc();
		return (*this);
	}
	FastIO operator >> (int &x){
		x = 0;
		register char c;
		register int w = 1;
		while (c = getc(), c < '0' || c > '9') if (c == '-') w = -1;
		while (c >= '0' && c <= '9') x = x * 10 + c - 48, c = getc();
		return (*this);
	}
	FastIO operator >> (char &c){
		c = getc();
		return (*this);
	}
}IO;

int dfn[101000];
int low[101000];
int n; int m;
vector <int> f[101000]; // edge
stack <int> stk; int pl = 0;
int vis[101000];
int ans[101000];
void dfs(int x){
	dfn[x] = low[x] = ++pl;
	stk.push(x);
	vis[x] = 1;
	for (auto i : f[x]){
		if (!dfn[i]){
			dfs(i);
			low[x] = min(low[x], low[i]);
		}
		else if (vis[i]){
			low[x] = min(low[x], dfn[i]);
		}
	}
	if (dfn[x] == low[x]){
		while (stk.top() != x){
			vis[stk.top()] = 0;
			ans[stk.top()] = x;
			stk.pop();
		}
		vis[x] = 0;
		ans[x] = x;
		stk.pop();
	}
}

int main(){
	IO >> n >> m;
	for (int i=1;i<=m;i++){
		int tp1, tp2;
		IO >> tp1 >> tp2;
		f[tp1].emplace_back(tp2);
	}
	for (int i=1;i<=n;i++)
	{
		if (dfn[i] == 0){
			dfs(i); // 缩点
		}
	}
	for (int i=1;i<=n;i++)
		cout << ans[i] << ' ';
}

posted @ 2019-11-09 20:21  dgklr  阅读(144)  评论(0编辑  收藏  举报