All pastes #40530 Raw Edit

Miscellany

public text v1 · immutable
#40530 ·published 2006-02-07 12:33 UTC
rendered paste body
#include <stdio.h>
#include <string.h>

struct Entry{
	char name[20];
	int ndef;
	char term[20][20];
} dict[1000];

int adj[1000][1000];
int flag[1000];
int n;

void findAdj ()
{
	int i,j,k;

	for (i=0; i<n; i++)
		for(j=0; j<dict[i].ndef; j++)
			for(k=0; k<n; k++)
				if (strcmp (dict[i].term[j], dict[k].name) == 0)
					adj[i][k] = 1;
}	

int isCycle(int i)
{
	if (flag[i] == 0){
		flag[i] = 1;
		for (int j=0; j<n; j++)
			if (adj[i][j] == 1 ){
				if(isCycle(j)){
					flag[i] = 0;
					return 1;
				}
			}
		flag[i] = 0;
		return 0;
	}else 
		return 1;
}


int main()
{
	int i;
	scanf ("%d", &n);

	for (i=0; i<n; i++){
		scanf ("%s %d", dict[i].name, &(dict[i].ndef));
		for(int k=0; k<i; k++)
			if(strcmp(dict[i].name,dict[k].name)==0){
				printf("NOT CORRECT\n");
				return 0;
			}
		for (int j=0; j<dict[i].ndef; j++)
			scanf ("%s", dict[i].term[j]);
	}

	findAdj();

	for(i=0; i<n; i++){
		if(isCycle(i)){
			printf("NOT CORRECT\n", i);
			return 0;
		}
	}
	printf ("CORRECT\n");
	return 0;
}