# [C (gcc)], <s>790 778 757 748 731 687</s> 639 bytes <!-- language-all: lang-c --> typedef struct F F;typedef struct{int c,p;F**m;}g;struct F{int d;g f;};g*a,S,y,q,*x=&y,*p=&q;A(g*g,F*s){g->c++[g->m=g->c-g->p?g->m:realloc(g->m,(g->p+=32)*20)]=s;}i,c[999],b,j,k,l,*s;*B(d){s=0;for(i=S.c;i--;)s=S.m[i]->d-d?s:S.m[i];!s?s=calloc(5,4),*s=d,A(&S,s):0;return s;}F*u,*v;E(){for(i=-1;b=++i<S.c;b&&A(x,S.m[i]))for(l=x->c;k=l--&&b;b&=!k)for(j=(u=x->m[l])->f.c;j--&&(k=u->f.m[j]->d-S.m[i]->d););x->c>p->c?a=x,x=p,p=a:0;x->c=0;}main(){for(;~scanf("%d %d",&a,&b)&&A(&(u=B(a))->f,v=B(b))|~A(&v->f,u););for(E();b<S.c;)c[b]>=b?c[b++]=0:E(S.m[b]=a,S.m[i]=S.m[b],a=S.m[i=b%2*c[b]++]);for(printf("%d,",i=p->c);i--;)printf(" %d",p->m[i]->d);} This is a slightly golfed version of the reference implementation. [Try it online!](https://tio.run/##VVLbjqJAEH33KxgTSXdTbESFVXtLM5OMP@Aj4YGLEhQcBsRoXOfXna5unc0@UH2qzumqU0Dq5ml6vx8v9SbbbK322HTp0VpZK/l/6VocjlYKtVwJUclbLp9KTWQyt7byJnMRwxou8AnijPYFRI32p3xluchhJVp@zd1F6jihOiok7KpQLymdN5u4LD9SRglQrB0cj7gYDXmErbwVkIaz2SyCBHawhxJEK8Uby/i1xaHcfjSswPWvVBauK3mrYBUWkbvI3GzZzk0mX9pli6mZ48OEqx6YwSuz19Dy@VA2m2PXHCw1bSU6ECf5zvjVtHY9maDjFH9oRmLbr@wMpivnpCjxrPaReyxd17YTJcGXvWZ2yDoiq7CMuLvYqvs70rA9dpRW4U77/HHMJZfUbFGrsIzxDGesocZYOaS6WvdWxcXh4U1@tWl82LL@ILMGWR/sGOyEk0NbDX5jMaepcFIw4fzvl6qfqNDRHGqglpSJ3ounYRItMFmq03EiHM7fGdlKIowf26LJITZvGJPBSNAtJTfd6kb9EdoN9KFA2oGbj/JktMuaXshj3dv97o1mgTX2gp4X@EMDqEKZQf7IUL89r6eef1pF@CNd1joCKidaHZ4f@D0KFpEG/dw0pB4znj5UT6D7jafGAM3TDBUmk556LG8aPJwZpAJZ0dJgPPsG "C (gcc) – Try It Online") [C (gcc)]: https://gcc.gnu.org/ Slightly golfed less typedef struct F F; // student group typedef struct{ int c, // count p; // capacity F**m; // members }g; // student struct F{ int d; // id g f; // friends }; g S,y,q,*x=&y,*p=&q; A(g*g,F*s){ g->c++[g->m=g->c-g->p?g->m:realloc(g->m,(g->p+=32)*20)]=s; } i,*a,c[999],b,j,k,l; *B(d){ s=0; for(i=S.c;i--;) s=S.m[i]->d-d?s:S.m[i]; !s? s=calloc(5,4), *s=d,A(&S,s) : 0; return s; } F*u,*v; E(){ for(i=-1;b=++i<S.c;b&&A(x,S.m[i])) for(l=x->c;k=l--&&b;b&=!k) for(j=(u=x->m[l])->f.c;j--&&(k=u->f.m[j]->d-S.m[i]->d);); x->c>p->c? a=x, x=p, p=a : 0; x->c=0; } main(){ for(;~scanf("%d %d",&a,&b)&&A(&(u=B(a))->f,v=B(b))|~A(&v->f,u);); for(E();b<S.c;) c[b]>=b? c[b++]=0 : E(S.m[b]=a,S.m[i]=S.m[b],a=S.m[i=b%2*c[b]++]); for(printf("%d,",i=p->c);i--;) printf(" %d",p->m[i]->d); }