Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

无语了《《《help

Posted by china_ll at 2011-03-24 19:15:15 on Problem 2762 and last updated at 2011-03-24 19:17:03
#include<stdio.h>
#include<string.h>

#define CLR(x) memset(x, 0, sizeof(x))
#define MIN(a, b) a < b ? a : b

#define V 1005

void init();
void tarjan();
int top_sort();
void creat_newg();
void solve();

int t, n, m, id, cnt, top;
short int scc[V], s[V], ins[V], dfn[V], low[V];
short int in[V];
short int g[V][V];
short int newg[V][V];

int main()
{
	freopen("Poj 2762", "r", stdin);
	scanf("%d", &t);
	while(t--){
		init();
		solve();
	}

	return 0;
}

void init()
{
	CLR(dfn);
	CLR(ins);
	CLR(in);
	CLR(g);
	CLR(newg);
	id = cnt = top = 0;
}

void tarjan(int src)
{
	int i, poj;

	dfn[src] = low[src] = ++id;
	s[++top] = src, ++ins[src];
	for(i=1; i<=n; ++i){
		if(g[src][i]){
			if(!dfn[i]){
				tarjan(i);
				low[src] = MIN(low[src], low[i]);
			}else if(ins[i])
				low[src] = MIN(low[src], dfn[i]);
		}
	}
	if(low[src] == dfn[src]){
		++cnt;
		do{
			poj = s[top--];
			scc[poj] = cnt;
			ins[poj] = 0;
		}while(poj != src);
	}
}

int top_sort() /* 对新图拓朴*/
{
	int i, j, k;

	for(i=1, top=0; i<=cnt; ++i)
		if(!in[i]) s[++top] = i;
	if(!top) return 1; /* 强联通*/

	for(i=1; i<=cnt; ++i){
		if(top == 1){/* 存在一条连,則每次应只有一个入度零点*/
			j = s[top--];
			for(k=1; k<=cnt; ++k)
				if(newg[j][k]){
					if(!(--in[k])) 
						s[++top] = k;
				}
		}else break;

	}

	return i > cnt ? 1 : 0;
}

void creat_newg()
{
	int i, j;

	CLR(in);
	for(i=1; i<=n; ++i){ /* 枚举入口*/
		for(j=1; j<=n; ++j){
			if(g[j][i] && scc[j] != scc[i]){
				if(!newg[scc[j]][scc[i]]){
					newg[scc[j]][scc[i]] = 1;
					++in[scc[i]];
				}
			}
		}
	}
}

void solve()
{
	int i, x, y;

	scanf("%d%d", &n, &m);
	for(i=1; i<=m; ++i){
		scanf("%d%d", &x, &y);
		g[x][y] = 1;
	}
	tarjan(1);
	creat_newg();
	puts(top_sort() ? "Yes" : "No");
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator