WC模拟(1.4) T2 轰炸

轰炸

题目背景:

1.4 WC模拟T2

分析:tarjan缩点求最长路

 

表示搞不清楚自己明明都知道了有向图的最小次数是top序的层数,竟然加上强连通分量就不会做了·····显然对于一个强连通中的点,是一定要分开删除的,那么我们把每一个强连通的点权定义为当前强连通的点数,那么只要top排序,然后DP最长链就是答案了······

 

Source:

 

/*
	created by scarlyw
*/
#include <cstdio>
#include <string>
#include <algorithm>
#include <cstring>
#include <iostream>
#include <cmath>
#include <cctype>
#include <vector>
#include <set>
#include <queue>
#include <ctime>
#include <bitset>

inline char read() {
	static const int IN_LEN = 1024 * 1024;
	static char buf[IN_LEN], *s, *t;
	if (s == t) {
		t = (s = buf) + fread(buf, 1, IN_LEN, stdin);
		if (s == t) return -1;
	}
	return *s++;
}

///*
template<class T>
inline void R(T &x) {
	static char c;
	static bool iosig;
	for (c = read(), iosig = false; !isdigit(c); c = read()) {
		if (c == -1) return ;
		if (c == '-') iosig = true;	
	}
	for (x = 0; isdigit(c); c = read()) 
		x = ((x << 2) + x << 1) + (c ^ '0');
	if (iosig) x = -x;
}
//*/

const int OUT_LEN = 1024 * 1024;
char obuf[OUT_LEN], *oh = obuf;
inline void write_char(char c) {
	if (oh == obuf + OUT_LEN) fwrite(obuf, 1, OUT_LEN, stdout), oh = obuf;
	*oh++ = c;
}

template<class T>
inline void W(T x) {
	static int buf[30], cnt;
	if (x == 0) write_char('0');
	else {
		if (x < 0) write_char('-'), x = -x;
		for (cnt = 0; x; x /= 10) buf[++cnt] = x % 10 + 48;
		while (cnt) write_char(buf[cnt--]);
	}
}

inline void flush() {
	fwrite(obuf, 1, oh - obuf, stdout);
}

/*
template<class T>
inline void R(T &x) {
	static char c;
	static bool iosig;
	for (c = getchar(), iosig = false; !isdigit(c); c = getchar())
		if (c == '-') iosig = true;	
	for (x = 0; isdigit(c); c = getchar()) 
		x = ((x << 2) + x << 1) + (c ^ '0');
	if (iosig) x = -x;
}
//*/

const int MAXN = 1000000 + 10;

std::vector<int> edge[MAXN], new_edge[MAXN];
std::vector<int> s, top;
bool vis[MAXN];
int n, m, x, y, o, cnt, ind, ans;
int size[MAXN], num[MAXN], low[MAXN], scc[MAXN], dep[MAXN];

inline void add_edge(int x, int y) {
	edge[x].push_back(y);
}

inline void tarjan(int cur) {
	num[cur] = low[cur] = ++ind, vis[cur] = true, s.push_back(cur);
	for (int p = 0; p < edge[cur].size(); ++p) {
		int v = edge[cur][p];
		if (num[v] == 0) tarjan(v), low[cur] = std::min(low[cur], low[v]);
		else if (vis[v]) low[cur] = std::min(low[cur], num[v]);
	}
	if (low[cur] == num[cur]) {
		++cnt;
		do {
			o = s.back(), scc[o] = cnt, size[cnt]++;
			vis[o] = false, s.pop_back();
		} while (o != cur);
	}
}

inline void dfs(int cur) {
	vis[cur] = true;
	for (int p = 0; p < new_edge[cur].size(); ++p) {
		int v = new_edge[cur][p];
		if (!vis[v]) dfs(v);
	}
	top.push_back(cur);
}

inline void read_in() {
	R(n), R(m);
	for (int i = 1; i <= m; ++i) R(x), R(y), add_edge(x, y);
}

inline void solve() {
	for (int i = 1; i <= n; ++i) if (num[i] == 0) tarjan(i);
	for (int cur = 1; cur <= n; ++cur)
		for (int p = 0; p < edge[cur].size(); ++p) {
			int v = edge[cur][p];
			if (scc[v] != scc[cur]) new_edge[scc[cur]].push_back(scc[v]);
		}
	for (int i = 1; i <= cnt; ++i) if (!vis[i]) dfs(i);
	for (int i = 0; i < top.size(); ++i) {
		int cur = top[i];
		dep[cur] = size[cur];
		for (int p = 0; p < new_edge[cur].size(); ++p) {
			int v = new_edge[cur][p];
			dep[cur] = std::max(dep[cur], dep[v] + size[cur]);
		}
		ans = std::max(ans, dep[cur]);
	}
	std::cout << ans;
}

int main() {
	freopen("bomb.in", "r", stdin);
	freopen("bomb.out", "w", stdout);
	read_in();
	solve();
	return 0;
}

阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/scar_lyw/article/details/79034427
文章标签: 图论 tarjan NOIP
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭