Language: C

/*

????????? ??¨?´?????????? ???????????????
A ???????????????????????§???????????????A ??????????????????????????¨?´???????????±???????????????????
?????????????????¨????????????A???????????? 1 ?????? N ??????????????????????????? N ??????????????¨???
1 ?????? M ??????????????????????????? M ?¨?????????¨?´?????????????????????¨?´????????????°??¶??????
????????????????????¨?´??????????????????????????????????A ?????????????????§?????????????????¨????????¨
?´?????????§??????????±???§???????????????????????¨???????????????????????£????????????

????????????????????????????????????????????????????¬??????????????????§??????????????? K ???????¨????
?????\????????????

??? ?????? a ??¨?????? b ???????????¨?´???????????±????????????????
??? ?????? c ?????¨?´???? x ???????±????????????????

???????????????????¨????????????°??????????????????????????£?????????????????????????????????????????????
???????????????????????????????????????????????????????????????????????¨????????§???????????????????????¢
????????¨????????????

??????
K ???????¨????????????????????????¨???????????????????????¨????????§???????????????????????????????±????
???????????°????????????????????????

??\???
??\????????\????????¢?????§??????????????????

1 a b

?????????

2 c x

???????????°?????????1????????¨??????
	??????a(1???a???N)??¨??????b(1???b???N)???????????¨?´???????????±??????????????????¨??????
	??????????????????a???b??§?????????
???????????°?????????2????????¨??????
	??????c(1???c???N)?????¨?´????x(1???x???M)???????±??????????????????¨????????????

????????¶???
??\?????????????????????????????????????§?????¶?????????????????????????

??????
?????????????????¨????????§??????????????????????????????????????????????????????????????????????????´???
??? 0 ???????????????????????????

	http://judge.u-aizu.ac.jp/onlinejudge/review.jsp?rid=2409100#1

*/

#include <stdio.h>

#define MAX_N 100001

	int par[MAX_N];
	int size[MAX_N];
	int id[MAX_N];
	int flg = 0;

/*
 * ????????????????????????
 */
void init( void )
{
	int i;
	for( i = 0 ; i < MAX_N ; i++ ) {
		par[ i ] = i;
		size[ i ] = 1;
		id[ i ] = -1;
	}
}
 
/*
 * ?????????????????????????´¢
 */
int find(
	int x )
{
	int ret;

	if ( par[ x ] == x ) {
		ret = x;
	}
	else {
		par[ x ] = find( par[ x ] );
		ret = par[ x ];
	}

	return ret;
}
 
/*
 * ????????????????????????
 */
void unite(
	int a,
	int b )
{
  a = find( a );
  b = find( b );

  if ( a == b ) return;
  if ( size[ a ] > size[ b ]) swap( a, b );

  par[ a]=b;
 
  if(id[a]!=-1&&id[b]!=-1&&id[a]!=id[b])flg=true;
   
  if(id[a]!=-1){
    id[b]=id[a];
  }
}
 
int N,M,K;
int main(){
  init();
   
  scanf("%d %d %d",&N,&M,&K);
  for(int k=1;k<=K;k++){
    int type,x,y;
    scanf("%d %d %d",&type,&x,&y);
    if(type==1){
      unite(x,y);
    }else if(type==2){
      x=find(x);
      if(id[x]!=-1&&id[x]!=y)flg=true;
      id[x]=y;
    }
    if(flg){
      cout<<k<<endl;
      break;
    }
  }
  if(!flg){
    cout<<0<<endl;
  }
  return 0;
}

Compiler output:
main.c: In function 'unite':
main.c:102:31: error: implicit declaration of function 'swap' [-Wimplicit-function-declaration]
  102 |   if ( size[ a ] > size[ b ]) swap( a, b );
      |                               ^~~~
main.c:106:45: error: 'true' undeclared (first use in this function)
  106 |   if(id[a]!=-1&&id[b]!=-1&&id[a]!=id[b])flg=true;
      |                                             ^~~~
main.c:51:1: note: 'true' is defined in header '<stdbool.h>'; this is probably fixable by adding '#include <stdbool.h>'
   50 | #include <stdio.h>
  +++ |+#include <stdbool.h>
   51 | 
main.c:106:45: note: each undeclared identifier is reported only once for each function it appears in
  106 |   if(id[a]!=-1&&id[b]!=-1&&id[a]!=id[b])flg=true;
      |                                             ^~~~
main.c: In function 'main':
main.c:125:34: error: 'true' undeclared (first use in this function)
  125 |       if(id[x]!=-1&&id[x]!=y)flg=true;
      |                                  ^~~~
main.c:125:34: note: 'true' is defined in header '<stdbool.h>'; this is probably fixable by adding '#include <stdbool.h>'
main.c:129:7: error: 'cout' undeclared (first use in this function)
  129 |       cout<<k<<endl;
      |       ^~~~
main.c:129:16: error: 'endl' undeclared (first use in this function)
  129 |       cout<<k<<endl;
      |                ^~~~