Pagini recente » Cod sursa (job #2795486) | Cod sursa (job #384398) | Cod sursa (job #1834362) | Cod sursa (job #852653) | Cod sursa (job #2618971)
#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);
}