1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
/*
 * main.c
 *
 *  Created on: 2 paź 2015
 *      Author: knoppix
 */

#include <stdio.h>
#include <stdlib.h>

//#define DEBUG_1
//#define DEBUG_2
//#define DEBUG_3
//#define DEBUG_4
//#define DEBUG_5
//#define DEBUG_6
//#define DEBUG_7
//#define DEBUG_8
//#define DEBUG_9
//#define DEBUG_10

#define MAXNODES 500000

enum marktype {unvisited, visited};
enum spantree	{outoftree, intree};

struct nodelist {
	long node;
	enum spantree spantree;
	enum marktype visit;
	struct nodelist *next;
};

struct graph {
	enum marktype mark;
	long postorder;	//czas przetworzenia wierzchołka
	long nodenr; 	//nr węzła
	long strconnected;	//nr składowej silnie spójnej
	struct nodelist * successors;
};
struct graph graph[MAXNODES+1];	//Lista sąsiedztwa dla skrzyżowań (numeracja od 1)
struct graph graph_trans[MAXNODES+1];	//graf przetransponowany ()
long postorder[MAXNODES+1];	//indeks-postorder, wartość-nr wierzcholka

struct krawedz {
		struct nodelist *left;
//		struct nodelist *right;
		long nodea;
		long nodeb;
	};
struct krawedz odrzucone[MAXNODES];
long ilodrz;

long n,m; //liczba wierzchołków, liczba krawędzi

long k;	//pomocnicza dla forest

//----- prototypy -------------
void append(struct graph g[], long from, long to);
void dfsForest(struct graph g[]);
//int testAcyclic(struct graph g[], long n); //indeksowanie od zera!
void transpone(struct graph g[], struct graph gt[], long order[]);
void dfs(long u, struct graph g[], long tree);
void dfsForestTrans(struct graph g[], long order[]);
void removeList(struct graph g[]);
//void sortgraph(long gi[]);
void removeEdges(struct graph g[]);
long buildSpanTree(struct graph g[]);
int DFSfindCycle(struct graph g[],long b, long w, long stack[], long *ilstack);

//----- main ------------------
int main()
{
	long i, a, b;

	scanf("%ld %ld\n", &n, &m);
	for(i=0; i<m; i++)
	{
		scanf("%ld %ld\n", &a, &b);
		append(graph, a,b);
//		graph[a].nodenr = a;
	}

#ifdef DEBUG_1
	struct nodelist *tmpptr;
	for(i=1; i<=n; i++)
	{
		printf("%ld(%d,%ld): ",i,graph[i].mark,graph[i].postorder);
		tmpptr=graph[i].successors;
		while(tmpptr)
		{
			printf("%ld ", tmpptr->node);
			tmpptr = tmpptr->next;
		}
		printf("\n");
	}
#endif

	dfsForest(graph);

#ifdef DEBUG_2
{	struct nodelist *tmpptr;
	for(i=1; i<=n; i++)
	{
		printf("%ld(%d,%ld,%ld): ",i,graph[i].mark,graph[i].postorder,graph[i].strconnected);
		tmpptr=graph[i].successors;
		while(tmpptr)
		{
			printf("%ld ", tmpptr->node);
			tmpptr = tmpptr->next;
		}
		printf("\n");
	}
	printf("\n");
}
#endif
#ifdef DEBUG_3
	printf("%d\n", testAcyclic(graph));
#endif

	transpone(graph, graph_trans, postorder);

#ifdef DEBUG_4
{	struct nodelist *tmpptr;
	for(i=1; i<=n; i++)
	{
		printf("%ld(%d,%ld,%ld): ",i,graph_trans[i].mark,graph_trans[i].postorder,graph_trans[i].nodenr);
		tmpptr=graph_trans[i].successors;
		while(tmpptr)
		{
			printf("%ld ", tmpptr->node);
			tmpptr = tmpptr->next;
		}
		printf("\n");
	}
	printf("\n");
}
#endif

#ifdef DEBUG_5
	for(i=1; i<=n; i++)
	{
		printf("%ld\n",postorder[i]);
	}

#endif

	dfsForestTrans(graph_trans, postorder);

#ifdef DEBUG_6
{	struct nodelist *tmpptr;
	for(i=1; i<=n; i++)
	{
		printf("%ld(%d,%ld,%ld): ",i,graph_trans[i].mark,graph_trans[i].postorder,graph_trans[i].strconnected);
		tmpptr=graph_trans[i].successors;
		while(tmpptr)
		{
			printf("%ld ", tmpptr->node);
			tmpptr = tmpptr->next;
		}
		printf("\n");
	}
	printf("\n");
}
#endif

	//przkopiowanie nr silnych skladowych do graph, inicjajca graph
	for(i=1; i<=n; i++)
	{
		graph[i].strconnected = graph_trans[i].strconnected;
		graph[i].mark = unvisited;
	}

	//usuniecie list, tablica graph_trans jest pusta i pozostaje nieuzywana i w zasadzie mozna ja usunac
	removeList(graph_trans);

	//usuniecie krawedzi miedzy silnymi skladowymi
	removeEdges(graph);

#ifdef DEBUG_8
{	struct nodelist *tmpptr;
	for(i=1; i<=n; i++)
	{
		printf("i:%ld(mark:%d,postorder:%ld,strconn:%ld,nodenr:%ld): ",i,graph[i].mark,graph[i].postorder,graph[i].strconnected,graph[i].nodenr);
		tmpptr=graph[i].successors;
		while(tmpptr)
		{
			printf("%ld ", tmpptr->node);
			tmpptr = tmpptr->next;
		}
		printf("\n");
	}
	printf("\n");
}
#endif

	//budowanie drzewa rozpinającego, wszystkie krawędzie mają tę samą wagę
	//więc po prostu przejedziemy graf DFS zaznaczajac krawedzie zamykajace cykl
	//jako nie wchodzace do drzewa
	ilodrz = buildSpanTree(graph);

#ifdef DEBUG_9
{	struct nodelist *tmpptr;
	for(i=1; i<=n; i++)
	{
		printf("i:%ld(mark:%d,postorder:%ld,strconn:%ld,nodenr:%ld): ",i,graph[i].mark,graph[i].postorder,graph[i].strconnected,graph[i].nodenr);
		tmpptr=graph[i].successors;
		while(tmpptr)
		{
			printf("%ld:%d ", tmpptr->node, tmpptr->spantree);
			tmpptr = tmpptr->next;
		}
		printf("\n");
	}
	printf("\n");
}
#endif

	if(!ilodrz)
	{
		printf("NIE\n");
		return 0;
	}

#ifdef DEBUG_10
	for(i=0; i<ilodrz; i++)
		printf("%ld:%ld %ld\n", i, odrzucone[i].nodea, odrzucone[i].nodeb); //chwilowo włączany tę krawędx
#endif

	long stack[n];
	long j, ilstackelem = 0;
	long ilcykli = 0;
	for(i=1; i<=n; i++) //inicjujemy licznik przejsc dla nodow
		postorder[i]=0;
	for(i=0; i<ilodrz; i++)
	{
		odrzucone[i].left->spantree = intree; //chwilowo włączany tę krawędx

		for(j=1; j<=n; j++) //inicjujemy wszystkie nody jako nieodwiedzone
		{
			graph[j].mark = unvisited;
		}

		if( DFSfindCycle(graph,odrzucone[i].nodea,odrzucone[i].nodea,stack,&ilstackelem) ) //znaleziono cykl
		{
			ilcykli++;

//			printf("\n%ld: ",odrzucone[i].nodea);
			while(ilstackelem)
			{
				postorder[stack[--ilstackelem]]++;
				//printf("%ld ",stack[--ilstackelem]);
			}
		}

		odrzucone[i].left->spantree = outoftree; //
	}

//	printf("%ld:\n",ilcykli);
//	for(i=1; i<=n; i++) //inicjujemy licznik przejsc dla nodow
//			printf("%ld:%ld\n",i, postorder[i]);

	long licznik = 0;
	for(i=1; i<=n; i++)
		if(postorder[i]== ilcykli)
			licznik++;

	//printf("%ld:%ld\n",licznik,ilcykli);
	if(!licznik)
	{
		printf("NIE\n");
		return 0;
	}

	printf("%ld\n",licznik);
	for(i=1; i<=n; i++)
	{
			if(postorder[i]== ilcykli)
				printf("%ld ",i);
	};



	return 0;
}

//********************************************

int DFSfindCycle(struct graph g[],long b, long w, long stack[], long *ilstack)
{
	struct nodelist *p;
	long v;

	g[w].mark = visited;
	stack[(*ilstack)++]=w;
	p = g[w].successors;
	while(p!=NULL)
	{
		if(p->spantree == intree)
		{
		v = p->node;
		if( v == b )
			return 1;
		if( 	(g[v].mark == unvisited) &&
				DFSfindCycle(g,b, v, stack, ilstack)
				)
			return 1;
		}
		p = p->next;
	}
	(*ilstack)--;
	return 0;

}

//-----------------
void makeSet(long u,long zb[])
{
	zb[u] = 0;
}

long findSet(long x, long zb[])
{
    if(!zb[x])
       return x;

    return findSet(zb[x],zb);
}

void unionSets(long a, long b, long zb[])
{
    long xRoot = findSet(a,zb);
    long yRoot = findSet(b,zb);

    if( xRoot != yRoot )
        zb[xRoot] = yRoot;
}

//struct nodelist *findEdge(long u,struct nodelist *head )
//{
//	while(head)
//	{
//		if(head->node == u)
//			return head;
//		head = head->next;
//	}
//	return (struct nodelist *)0;
//}

long buildSpanTree(struct graph g[])
{
	long u,v;
	long zbior[n+1];
	struct krawedz t[m];
	long i = 0, j, ilodrz = 0;
	struct nodelist *p;

	for(u=1; u<=n; u++)
	{
		makeSet(u,zbior);
		p = g[u].successors;
		while(p!=NULL)
		{
			v = p->node; //nr drugiej strony krawędzi
//			if(p->visit == unvisited)
//			{
				t[i].left = p;
//				p->visit = visited;
//				t[i].right =0;
//				t[i].right = findEdge(u,g[v].successors);
//				printf("%p\n",t[i].right);
//				if(t[i].right)
//				{
//					printf("bla: %d\n",t[i].right->visit);
//					(t[i].right)->visit = visited;
//				}
				t[i].nodea = u;
				t[i].nodeb = v;
				i++;
//			}

			p = p->next;
		}
	}

//printf("i:%ld, m:%ld\n",i, m);
	for(j=0; j<i; j++) //kolejne krawedzie
	{
		if( findSet(t[j].nodea,zbior) == findSet(t[j].nodeb,zbior ) ) //naleza do tej samej parafii - cykl
		{
			odrzucone[ilodrz++] = t[j]; //zachowaj na liscie odrzuconych krawedzi
		}
		else
		{
			unionSets(t[j].nodea,t[j].nodeb,zbior);
			t[j].left ->spantree = intree;
//			if(t[j].right)
//				t[j].right ->spantree = intree;
		}
	}

	return ilodrz;



}

//----------------------------------------
//usuwa krawedzie miedzy silnymi skladowymi
void removeEdges(struct graph g[])
{
	struct nodelist * p, **q, *tmp;
	long i, node;

	for(i=1; i<=n; i++)
	{
		p = g[i].successors;
		q = &g[i].successors;
//		printf("succesor: %p\n",g[i].successors);
		while(p!=NULL)	//jedziemy po liscie sąsiedztwa
		{
			node = p->node;
			tmp = p->next;
			if(g[i].strconnected != g[node].strconnected)
			{
	//			printf("przed: %p %p\n",g[i].successors, p->next);
				*q = (p->next);
		//		printf("po: %p\n",g[i].successors);
				free(p);
				//printf("bla\n");
			}
			else
			{
				q=&(p->next);
			}
			p = tmp;
		}
	}
}

//int comp( const void *a, const void *b )
//{
//	struct graph * at = (struct graph *)a, *bt = (struct graph *)b;
//	if( (at->strconnected < bt->strconnected) ||
//			((at->strconnected == bt->strconnected) && (at->nodenr<bt->nodenr)) )
//		return -1;
//	else if( (at->strconnected > bt->strconnected) ||
//			((at->strconnected == bt->strconnected) && (at->nodenr > bt->nodenr)) )
//		return 1;
//	else
//		return 0;
//}

//int comp( const void *a, const void *b )
//{
//	long * at = (long *)a, *bt = (long *)b;
//
//	if( (graph_trans[*at].strconnected < graph_trans[*bt].strconnected) ||
//			((graph_trans[*at].strconnected == graph_trans[*bt].strconnected) && (graph_trans[*at].nodenr < graph_trans[*bt].nodenr)) )
//		return -1;
//	else if( (graph_trans[*at].strconnected > graph_trans[*bt].strconnected) ||
//			((graph_trans[*at].strconnected == graph_trans[*bt].strconnected) && (graph_trans[*at].nodenr > graph_trans[*bt].nodenr)) )
//		return 1;
//	else
//		return 0;
//}
//
//void sortgraph(long gi[])
//{
//	qsort( &gi[1], n, sizeof(long), comp);
//}

//--------------------------
void removeList(struct graph g[])
{
	struct nodelist * p;
	long i;

	for(i=1; i<=n; i++)
	{
		p = g[i].successors;
		while(p!=NULL)	//jedziemy po liscie sąsiedztwa
		{
			g[i].successors = p->next;
			free(p);
			p = g[i].successors;
		}
	}
}

//-------------------
void dfsForestTrans(struct graph g[], long order[])
{
	long u,v;

	k=0;

//	for(u=1; u<=n; u++)
//	{
//		g[u].mark = unvisited;
//	}
//
	for(u=n; u>=1; u--)	//graf przetransponowany wg malejącej postorder
	{
		v = order[u];	//nr wezla
		if(g[v].mark == unvisited)
		{
			dfs(v,g,u);
		}
	}


}

//-----------------
void transpone(struct graph g[], struct graph gt[], long order[])
{
	long i,v;
	struct nodelist *p;

	for(i=1; i<=n; i++)
	{
		order[ g[i].postorder ] = i;

		gt[i].nodenr = i;
		p = g[i].successors;
		while(p!=NULL)	//jedziemy po liscie sąsiedztwa
		{
			v = p->node;
			append(gt,v,i);
			p = p->next;
		}
	}
}

//-----------------------------
/*
 * A.V.Aho, J.D.Ullman:Wykłady z informatyki
 */

//int testAcyclic(struct graph g[], long n) //uwaga! indeksowanie od zera!
//{
//	long u, v;
//	struct nodelist *p;
//	//int retcode=1;
//
//	//dfsForest(g);
//	for(u=0; u<n; u++)
//	{
//		p = g[u].successors;
//		while(p!=NULL)
//		{
//			v = p->node;
//			if(g[u].postorder <= g[postorder[v]].postorder)
//			{
//				return 0;
//				//retcode = 0;
//#ifdef DEBUG_3
//				printf("krwst:%ld %ld\n",u,v);
//#endif
//			}
//			p=p->next;
//		}
//	}
//	return 1;
//	//return retcode;
//}
//



void dfs(long u, struct graph g[], long tree)
{
	struct nodelist *p;
	long v;

	g[u].mark = visited;
	g[u].strconnected = tree;
	p = g[u].successors;
	while(p!=NULL)
	{
		v = p->node;
		if(g[v].mark == unvisited)
			dfs(v,g, tree);
		p = p->next;
	}
	k++;
	g[u].postorder = k;
}

void dfsForest(struct graph g[])
{
	long u;

	k=0;

//	for(u=1; u<=n; u++)
//	{
//		g[u].mark = unvisited;
//	}
//
	for(u=1; u<=n; u++)
	{
		if(g[u].mark == unvisited)
			dfs(u,g,u);
	}


}


//-------------------------------
void append(struct graph g[], long from, long to)
{
	struct nodelist *tmp;

	tmp = malloc(sizeof(struct nodelist));
	tmp->node = to;
	tmp->spantree = outoftree;
	tmp->visit = unvisited;
	tmp->next = g[from].successors;
	g[from].successors = tmp;
//	g[from].mark = unvisited;
}//---------------------------------