Merge two rows using awk or python

Hi,
Suppose I have a space delimited file like this:

Serial# 1970 1971 1972 1973 1974
193532 21 2 X X X
200201 20 30 X X 40
200201 X X 13 15 X
393666 66 3 X X 5
393666 77 X X X X

First, I want to check the serial#, if any two lines have the same serial#,(in this case line 2+3, and line 4+5 qualify), then merge these two lines by replacing X with the value of the other line.

Also, when there is a conflict, in this case line 4+5 have the first column as 66,77(rather than having X in either line or in both lines), then do not merge even though they have the same serial#, but flag both lines with FLAGGED on the CONFLICT_FLAG column.

The result would be:

Serial# 1970 1971 1972 1973 1974 CONFLICT_FLAG
193532 21 2 X X X
200201 20 30 13 15 40
393666 66 3 X X 5 FLAGGED
393666 77 X X X X FLAGGED

Is it possible to do this in either python or awk? Thank you.

below perl should help you some.

open FH,"<a";
while(<FH>){
 my @tmp = split;
 my @t1=@tmp;
 if (not exists $hash{$tmp[0]}){
   $hash{$tmp[0]}=[@tmp[1..5]];
 }
 else{
   $hash{$tmp[0]}=[@{$hash{$tmp[0]}},@tmp[1..5]];
 }
}
foreach my $key ( keys %hash ){
 my $flag=0;
 my @tmp = @{$hash{$key}};
 if($#tmp < 6){
   print $key," ";
   print join " ", @{$hash{$key}};
   print "\n";
 }
 else{
   my @t = @tmp;
   map { s/X/0/ } @t;
   for(my $i=0;$i<=4;$i++){
     if($t[$i]!=0 && $t[$i+5]!=0){
	$flag=1;	
     }
   }
   if($flag==1){
	print $key," ";
	print join " ", @tmp[0..4];
	print " FLAGGED\n";
	print $key," ";
	print join " ", @tmp[5..9];
	print " FLAGGED\n";
   }
   else{
	print $key," ";
	for(my $i=0;$i<=4;$i++){
	  print $t[$i]+$t[$i+5]," ";
	}
        print "\n";
   }
 }
}

thanks a lot, unfortunately, perl is not installed on site, nor do i have permission to.
is it possible to do it in python or awk?