zoukankan      html  css  js  c++  java
  • 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 '
    ' // 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] != '') 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] << ' ';
    }
    
    
  • 相关阅读:
    文档浏览类的网站该如何设计?
    如何将动态生成Word文件
    Word 2007 文档结构图混乱
    Spring 配置 Spring JPA 发生错误的解决方法
    今天开始着手原来Office系统的重构
    Jetty入门
    推荐一个C语言学习教程
    博客园的第一天
    我与solr(一)--solr的配置与安装
    XML解析工具类
  • 原文地址:https://www.cnblogs.com/dgklr/p/11827609.html
Copyright © 2011-2022 走看看