URAL 1198 Jobbery (强连通分量 Gabow)

#include <stdio.h>
#define MAX_MEMBERS 2000
int numOfMembers;
typedef struct To{
	int member;
	int nextToNum;
}To;
To ToArray[MAX_MEMBERS * MAX_MEMBERS + 1];
int ToNum;
int ToNumLastAdded[MAX_MEMBERS + 1];
int clock;
int pre[MAX_MEMBERS + 1];
int stack1[MAX_MEMBERS + 1];
int top1;
int stack2[MAX_MEMBERS + 1];
int top2;
int SCCNumArray[MAX_MEMBERS + 1];
int numOfSCCs;
int inDegree[MAX_MEMBERS + 1];

void getSCCsByGabow(int from){
	clock++;
	pre[from] = clock;
	top1++;
	stack1[top1] = from;
	top2++;
	stack2[top2] = from;
	int ToNum;
	for (ToNum = ToNumLastAdded[from]; ToNum != 0; ToNum = ToArray[ToNum].nextToNum){
		int to = ToArray[ToNum].member;
		if (pre[to] == 0)
			getSCCsByGabow(to);
		else if (SCCNumArray[to] == 0){
			int minPre = pre[to];
			while (pre[ stack2[top2] ] > minPre)
				top2--;
		}
	}
	if (stack2[top2] == from){
		top2--;
		numOfSCCs++;
		do 
			SCCNumArray[ stack1[top1] ] = numOfSCCs;
		while (stack1[top1--] != from);
	}
}

int main(){

	scanf("%d", &numOfMembers);
	int from, to;
	for (from = 1; from <= numOfMembers; from++)
		while (scanf("%d", &to) != EOF && to != 0){
			ToNum++;
			ToArray[ToNum].member = to;
			ToArray[ToNum].nextToNum = ToNumLastAdded[from];
			ToNumLastAdded[from] = ToNum;
		}
	
	for (from = 1; from <= numOfMembers; from++)
		if (pre[from] == 0)
			getSCCsByGabow(from);

	for (from = 1; from <= numOfMembers; from++){
		int ToNum;
		for (ToNum = ToNumLastAdded[from]; ToNum != 0; ToNum = ToArray[ToNum].nextToNum){
			int to = ToArray[ToNum].member;
			if (SCCNumArray[from] != SCCNumArray[to])
				inDegree[ SCCNumArray[to] ]++;
		}
	}

	int source = 0;  
    int SCCNumOfSource = 0;  
    int SCCNum;  
    for (SCCNum = 1; SCCNum <= numOfSCCs; SCCNum++)  
        if (inDegree[SCCNum] == 0){  
            if (++source > 1)  
                break;  
            SCCNumOfSource = SCCNum;  
              
        }  
  
	int member;
    if (source == 1)  
        for (member = 1; member <= numOfMembers; member++)  
            if (SCCNumArray[member] == SCCNumOfSource)  
                printf("%d ", member);  
    printf("0\n");  
  
	return 0;
}

你可能感兴趣的:(强连通分量,1198,ural,Gabow,Jobbery)