#include <stdio.h>
#include <string.h>
struct employee {
int id;
char first_name[ 32 ] ;
char last_name[ 32 ] ;
int boss_id;
} ;
int find( int * parent, int x, struct employee e[ ] ) {
int temp= parent[ x] ;
if ( e[ temp] .id != e[ x] .id ) {
temp = find( parent, temp, e) ;
}
return temp;
}
void unite( int * parent, int x, int y, struct employee e[ ] ) {
int rootX = find( parent, x, e) ;
int rootY = find( parent, y, e) ;
if ( rootX != rootY) {
parent[ rootX] = rootY;
}
}
int check( int * parent, int x, int y, struct employee e[ ] ) {
int temp= parent[ x] ;
if ( e[ temp] .id != e[ x] .id ) {
if ( e[ y] .id == e[ x] .id ) {
return - 1 ;
}
temp = check( parent, temp, y, e) ;
}
if ( e[ y] .id == e[ x] .id ) {
return - 1 ;
}
return temp;
}
int main( ) {
int n, m;
struct employee e[ n] ;
int parent[ n] ;
// Read employee data
for ( int i = 0 ; i < n; i++ ) {
scanf ( "%d %s %s %d" , & e
[ i
] .
id , e
[ i
] .
first_name , e
[ i
] .
last_name , & e
[ i
] .
boss_id ) ; parent[ i] = i;
}
// Build hierarchy
for ( int i = 0 ; i < n; i++ ) {
for ( int j = 0 ; j < n; j++ ) {
if ( e[ j] .id == e[ i] .boss_id ) {
unite( parent, i, j, e) ;
}
}
}
// Process relationship queries
for ( int i = 0 ; i < m; i++ ) {
char al[ 32 ] , af[ 32 ] , bl[ 32 ] , bf[ 32 ] ;
int a = - 1 , b = - 1 ;
scanf ( "%s %s %s %s" , af
, al
, bf
, bl
) ;
// Find indices of queried employees
for ( int j = 0 ; j < n; j++ ) {
if ( strcmp ( al
, e
[ j
] .
last_name ) == 0 && strcmp ( af
, e
[ j
] .
first_name ) == 0 ) { a = j;
}
if ( strcmp ( bl
, e
[ j
] .
last_name ) == 0 && strcmp ( bf
, e
[ j
] .
first_name ) == 0 ) { b = j;
}
}
if ( a == - 1 || b == - 1 ) {
printf ( "Employee not found\n " ) ; continue ;
}
printf ( "%d\n " , check
( parent
, a
, b
, e
) ) ; if ( find( parent, a, e) == find( parent, b, e) ) {
if ( check( parent, a, b, e) ==- 1 ) {
} else if ( check( parent, b, a, e) ==- 1 ) {
} else {
}
} else {
}
}
return 0 ;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdHJpbmcuaD4KCnN0cnVjdCBlbXBsb3llZSB7CiAgICBpbnQgaWQ7CiAgICBjaGFyIGZpcnN0X25hbWVbMzJdOwogICAgY2hhciBsYXN0X25hbWVbMzJdOwogICAgaW50IGJvc3NfaWQ7Cn07CgppbnQgZmluZChpbnQgKnBhcmVudCwgaW50IHgsIHN0cnVjdCBlbXBsb3llZSBlW10pIHsKCWludCB0ZW1wPXBhcmVudFt4XTsKICAgIGlmIChlW3RlbXBdLmlkICE9IGVbeF0uaWQpIHsKICAgICAgIHRlbXAgPSBmaW5kKHBhcmVudCwgdGVtcCwgZSk7CiAgICB9CiAgICByZXR1cm4gdGVtcDsKfQoKdm9pZCB1bml0ZShpbnQgKnBhcmVudCwgaW50IHgsIGludCB5LCBzdHJ1Y3QgZW1wbG95ZWUgZVtdKSB7CiAgICBpbnQgcm9vdFggPSBmaW5kKHBhcmVudCwgeCwgZSk7CiAgICBpbnQgcm9vdFkgPSBmaW5kKHBhcmVudCwgeSwgZSk7CiAgICBpZiAocm9vdFggIT0gcm9vdFkpIHsKICAgICAgICBwYXJlbnRbcm9vdFhdID0gcm9vdFk7CiAgICB9Cn0KCmludCBjaGVjayhpbnQgKnBhcmVudCwgaW50IHgsIGludCB5LCBzdHJ1Y3QgZW1wbG95ZWUgZVtdKSB7CglpbnQgdGVtcD1wYXJlbnRbeF07CiAgICBpZiAoZVt0ZW1wXS5pZCAhPSBlW3hdLmlkKSB7CiAgICAJcHJpbnRmKCIlZCAlZFxuIix4LHkpOwogICAgICAgIGlmIChlW3ldLmlkID09IGVbeF0uaWQpIHsKICAgICAgICAgICAgcmV0dXJuIC0xOyAKICAgICAgICB9CiAgICAgICAgdGVtcCA9IGNoZWNrKHBhcmVudCwgdGV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Zm9yIChpbnQgaiA9IDA7IGogPCBuOyBqKyspIHsKICAgICAgICAgICAgaWYgKHN0cmNtcChhbCwgZVtqXS5sYXN0X25hbWUpID09IDAgJiYgc3RyY21wKGFmLCBlW2pdLmZpcnN0X25hbWUpID09IDApIHsKICAgICAgICAgICAgICAgIGEgPSBqOwogICAgICAgICAgICB9CiAgICAgICAgICAgIGlmIChzdHJjbXAoYmwsIGVbal0ubGFzdF9uYW1lKSA9PSAwICYmIHN0cmNtcChiZiwgZVtqXS5maXJzdF9uYW1lKSA9PSAwKSB7CiAgICAgICAgICAgICAgICBiID0gajsKICAgICAgICAgICAgfQogICAgICAgIH0KICAgICAgICAKICAgICAgICBpZiAoYSA9PSAtMSB8fCBiID09IC0xKSB7CiAgICAgICAgICAgIHByaW50ZigiRW1wbG95ZWUgbm90IGZvdW5kXG4iKTsKICAgICAgICAgICAgY29udGludWU7CiAgICAgICAgfQoJCXByaW50ZigiJWRcbiIsY2hlY2socGFyZW50LCBhLGIsIGUpKTsKICAgICAgICBpZiAoZmluZChwYXJlbnQsIGEsIGUpID09IGZpbmQocGFyZW50LCBiLCBlKSkgewogICAgICAgICAgICBpZiAoY2hlY2socGFyZW50LCBhLCBiLCBlKT09LTEpIHsKICAgICAgICAgICAgICAgIHByaW50Zigic3Vib3JkaW5hdGVcbiIpOwogICAgICAgICAgICB9IGVsc2UgaWYgKGNoZWNrKHBhcmVudCwgYiwgYSwgZSk9PS0xKSB7CiAgICAgICAgICAgICAgICBwcmludGYoInN1cGVydmlzb3JcbiIpOwogICAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICAgICAgcHJpbnRmKCJjb2xsZWFndWVcbiIpOwogICAgICAgICAgICB9CiAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgcHJpbnRmKCJ1bnJlbGF0ZWRcbiIpOwogICAgICAgIH0KICAgIH0KICAgIHJldHVybiAwOwp9