pku2186Popular Cows(Tarjan)

Xredman posted @ Apr 12, 2010 12:21:51 AM in Algorithm with tags 强连通分量 pku Tarjan , 1200 阅读

6730628 Xredman 2186 Accepted 1268K 485MS C++ 2055B 2010-04-12 11:09:49

//6730628 Xredman 2186 Accepted 1268K 485MS C++ 2055B 2010-04-12 11:09:49 
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <stack>
using namespace std;

const int MAX_SIZE = 10002;

int n, m;
vector<int> G[MAX_SIZE];
int UFSet[MAX_SIZE];
bool instack[MAX_SIZE];
int bcnt, dindex;
int dfn[MAX_SIZE], low[MAX_SIZE];
stack<int> Stack;
bool out[MAX_SIZE];

void init()
{
	int i;
	for(i = 1; i <= n; i++)
	{
		G[i].clear();
		UFSet[i] = i;//并查集判强连通集合
		low[i] = i;
	}
	while(!Stack.empty())
		Stack.pop();
	memset(instack, false, sizeof(instack));
}

void tarjan(int x)
{
	int j;
	dfn[x] = low[x] = ++dindex;
	instack[x] = true;
	Stack.push(x);
	for(vector<int>::iterator iter = G[x].begin(); iter != G[x].end(); iter++)
	{
		j = *iter;
		if(!dfn[j])
		{
			tarjan(j);
			if(low[j] < low[x])
				low[x] = low[j];
		}
		else if(instack[j] && dfn[j] < low[x])
			low[x] = dfn[j];
	}

	if(dfn[x] == low[x])
	{
		bcnt++;
		do
		{
			j = Stack.top();
			Stack.pop();
			instack[j] = false;
			UFSet[j] = bcnt;
		} while(x != j);
	}
}

void solve()
{
	int i;
	memset(dfn, 0, sizeof(dfn));
	for(i = 1; i <= n; i++)
		if(!dfn[i])
			tarjan(i);
}


int main()
{
	int a, b;
	int i;
	int ans, flag;
	while(cin>>n>>m)
	{
		init();
		for(i = 0; i < m; i++)
		{
			scanf("%d%d", &a, &b);
			G[a].push_back(b);
		}
		solve();
	/*	for(i = 1; i <= n; i++)
			cout<<UFSet[i]<<" ";*/
		///////////////////////////////////////
		memset(out, 0, sizeof(out));
		for(i = 1; i <= n; i++)
			for(vector<int>::iterator iter = G[i].begin(); iter != G[i].end(); iter++)
				if(UFSet[i] != UFSet[*iter])
				{
					out[UFSet[i]] = true;
				}
		ans = 0; flag = -1;
		for(i = 1; i <= n; i++)
			if(!out[UFSet[i]])
			{//无出边
				if(flag == -1)
				{
					flag = UFSet[i];
					ans++;
				}else if(flag != UFSet[i])
				{
					flag = -1;
					break;
				}
				else
					ans++;
			}
		if(flag == -1)
			cout<<"0"<<endl;
		else
			cout<<ans<<endl;
	}
	return 0;
}

 

 






mi tv samsung no se 说:
Jul 30, 2023 05:10:46 AM

Es posible que el Wi-Fi no funcione en su televisor Samsung pero esté funcionando en otros dispositivos, o puede estar conectado al punto de acceso de su teléfono y acceder a Internet de esa manera, mi tv samsung no se conecta a wifi pero se niega a conectarse a Internet a través de su enrutador. Esta guía simple lo ayudará a volver a conectar su televisor Samsung en línea si no se conecta a Internet pero otros dispositivos pueden hacerlo o si Wi-Fi no funciona en absoluto.

Punjab 7th Class Sy 说:
Aug 02, 2023 11:55:04 PM

PSEB 7th Class Exam Date Sheet 2024 Available at Official Website, Every Year This Elementary School Final Exam Conducted Month of April, so Students Download Punjabi Class new Syllabus 2024 Regular Redding and Fallow for Final Exam Better Performance,Students we are Providing here the Latest Updated PSEB Class Syllabus 2024, Punjab Board Students who entered 7th Class This year Punjab 7th Class Syllabus 2024 must go Through This Latest Syllabus to Understand the course Structure for the Upcoming Part of the Current Academic Session.one of the most Important Tools that help in knowing the Course Description.


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter