Cod sursa(job #2618971)

Utilizator RaduQQTCucuta Radu RaduQQT Data 26 mai 2020 17:34:45
Problema Sortare topologica Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.17 kb
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>

typedef struct listNode
{
	listNode* next;
	int element;
};

void pushQueue(listNode*& head, listNode*& tail, int element)
{

	listNode* node = (listNode*)malloc(sizeof(listNode));
	node->element = element;
	if (head==NULL)
	{
		head = node;
		tail = node;
		head->next = tail;
		return;
	}
	
	tail->next = node;
	tail = tail->next;
}
void popQueue(listNode*& head, listNode*& tail)
{
	if (head == tail)
	{
		free(head);
		head = NULL;
		return;
	}
	listNode* node = head;
	head = head->next;
	free(node);
}


bool visited[50001];

int main()
{
	int n,m;
	FILE* fin = fopen("sortaret.in", "r");
	FILE* fout = fopen("sortaret.out", "w");
	fscanf(fin, "%d%d", &n, &m);
	
	listNode* head = NULL, *tail = NULL;
	int a, b;
	for (int i = 0; i < m; i++)
	{
		fscanf(fin, "%d%d", &a, &b);
		if (!visited[a])
		{
			pushQueue(head, tail, a);
			visited[a] = 1;
		}
		if (!visited[b])
		{
			pushQueue(head, tail, b);
			visited[b] = 1;
		}
	}
	while (head != tail)
	{
		fprintf(fout,"%d ", head->element);
		head = head->next;
	}
	fprintf(fout,"%d", head->element);
}