#!/usr/bin/perl -w

use strict;
#use Getopt::Std;
use Class::Struct;

my $line = ""; 
my $number_line = 0;
my $number_atoms = 0;
my $number_bonds = 0;
my $atom_section = 0;
my $bond_section = 0;
my $i = 0;
#my $chrg = 0;
my $atom;
#my @charges;
my @molecule;
#our $opt_c;

my $H_id = 0;
my $O_id = -1;
my $C1_id = 0;
my $temp_id = 0;
#my $H_charge = 0;
#my $O_charge = 0;

struct Atom =>
    {
    	id => '$',
    	number => '$',
        symbol  => '$',
        bonds => '@',
        bond_ids => '@',
        bond_orders => '@',
    };

# relative atomic weight
our %atoms = ("H" => 1.0, "C" => 12.0, "N" => 14.0, "O" => 16.0, "F" => 19.0, "Cl" => 35.5, "Br" => 79.9, "I" => 126.9);

#getopts("c:"); 

#if($opt_c){
#	if(-e $opt_c){
#		open(CHARGES,$opt_c);
#		while($line=<CHARGES>){
#			if($line =~ /^\s*([0-9]+)\s+([a-zA-Z]{1,2})\s+([+-]?[0-9]+[.]?[0-9]*)\s*$/){
#				$charges[$i] = $3;				
#				$i++;	
#			}
#		}
#		close CHARGES;
#	}
#	$chrg = 1;	
#}

while($line=<>){
	$number_line++;
	
	if(( $number_line == 4 ) and ( $line =~ /^\s*([0-9]+)\s+([0-9]+)/ )){
		$number_atoms = $1;
		$number_bonds = $2;
		$atom_section = 1;
	}
	
	elsif($atom_section){
		if( $number_line == (5+$number_atoms) ){ 
			$atom_section = 0; 
			$bond_section = 1;
		}
		elsif( $line =~ /^\s*([-+]?[0-9]+[.]?[0-9]*)\s+([-+]?[0-9]+[.]?[0-9]*)\s+([-+]?[0-9]+[.]?[0-9]*)\s+([a-zA-Z]{1,2})/ ) {

			$molecule[$number_line-5] = new Atom(	id		=> $number_line-5,
										number => $number_line-4, 
										symbol	=> $4,
										bonds	=> [-1,-1,-1,-1],
										bond_ids	=> [-1,-1,-1,-1],
										bond_orders	=> [-1,-1,-1,-1],
										);
		}
	}
	
	if($bond_section){
		if( $number_line == (5+$number_atoms+$number_bonds) ){
			$bond_section = 0;
		}
		elsif( $line =~ /^\s*([0-9]+)\s+([0-9]+)\s+([0-9]+)/ ) {

			if($molecule[$1-1]->bonds(0)==-1){
				$molecule[$1-1]->bonds(0,$2);
				$molecule[$1-1]->bond_ids(0,$2-1);
				$molecule[$1-1]->bond_orders(0,$3);
			}
			elsif($molecule[$1-1]->bonds(1)==-1){
				$molecule[$1-1]->bonds(1,$2);
				$molecule[$1-1]->bond_ids(1,$2-1);
				$molecule[$1-1]->bond_orders(1,$3);
			}
			elsif($molecule[$1-1]->bonds(2)==-1){
				$molecule[$1-1]->bonds(2,$2);
				$molecule[$1-1]->bond_ids(2,$2-1);
				$molecule[$1-1]->bond_orders(2,$3);
			}
			else{
				$molecule[$1-1]->bonds(3,$2);
				$molecule[$1-1]->bond_ids(3,$2-1);
				$molecule[$1-1]->bond_orders(3,$3);
			}
			
			if($molecule[$2-1]->bonds(0)==-1){
				$molecule[$2-1]->bonds(0,$1);
				$molecule[$2-1]->bond_ids(0,$1-1);
				$molecule[$1-1]->bond_orders(0,$3);
			}
			elsif($molecule[$2-1]->bonds(1)==-1){
				$molecule[$2-1]->bonds(1,$1);
				$molecule[$2-1]->bond_ids(1,$1-1);
				$molecule[$1-1]->bond_orders(1,$3);
			}
			elsif($molecule[$2-1]->bonds(2)==-1){
				$molecule[$2-1]->bonds(2,$1);
				$molecule[$2-1]->bond_ids(2,$1-1);
				$molecule[$1-1]->bond_orders(2,$3);
			}
			else{
				$molecule[$2-1]->bonds(3,$1);
				$molecule[$2-1]->bond_ids(3,$1-1);
				$molecule[$1-1]->bond_orders(3,$3);
			}
			
#			if( $3 eq 1 ){
				# print $atoms[$1-1]." ".$atoms[$2-1]."\n";
#				if( $molecule[$1-1]->symbol eq "H" and $molecule[$2-1]->symbol eq "O" ){$O_id=$2;$H_id=$1;}
#				if( $molecule[$2-1]->symbol eq "H" and $molecule[$1-1]->symbol eq "O" ){$O_id=$1;$H_id=$2;}
#			}
		}
	}
}

#if($chrg){
#	$charges[$O_id] ? $O_charge = $charges[$O_id] : $O_charge = "-";
#	$charges[$H_id] ? $H_charge = $charges[$H_id] : $H_charge = "-";
#	
#	print $O_id.";".$O_charge.";".$H_id.";".$H_charge."\n";
#}
#else {
	
## HLEDANI H a O v OH skupine fenolu (podminka - C1 vazby pouze na dalsi 2 uhliky!!)	

for($i=0; $i<$number_atoms; $i++){
	if($molecule[$i]->symbol eq "O" and 
		$molecule[$i]->bonds(0)!=-1 and 
		$molecule[$i]->bonds(1)!=-1 and
		$molecule[$i]->bonds(2)==-1 and 
		$molecule[$i]->bonds(3)==-1){	
		
		if($molecule[$molecule[$i]->bond_ids(0)]->symbol eq "H" or 
			$molecule[$molecule[$i]->bond_ids(1)]->symbol eq "H"){
			
			
			if($molecule[$molecule[$i]->bond_ids(0)]->symbol eq "C"){$temp_id=$molecule[$i]->bond_ids(0)}
			elsif($molecule[$molecule[$i]->bond_ids(1)]->symbol eq "C"){$temp_id=$molecule[$i]->bond_ids(1)}
			
			
			if($molecule[$temp_id]->bonds(0)!=-1 and 
				($molecule[$molecule[$temp_id]->bonds(0)-1]->symbol eq "C" or
				$molecule[$molecule[$temp_id]->bonds(0)-1]->symbol eq "O") and
				$molecule[$temp_id]->bonds(1)!=-1 and
				($molecule[$molecule[$temp_id]->bonds(1)-1]->symbol eq "C" or
				$molecule[$molecule[$temp_id]->bonds(1)-1]->symbol eq "O") and
				$molecule[$temp_id]->bonds(2)!=-1 and 
				($molecule[$molecule[$temp_id]->bonds(2)-1]->symbol eq "C" or
				$molecule[$molecule[$temp_id]->bonds(2)-1]->symbol eq "O") and
				$molecule[$temp_id]->bonds(3)==-1){
				
				if($molecule[$molecule[$i]->bonds(0)-1]->symbol eq "H"){$H_id=$molecule[$i]->bonds(0)}
				elsif($molecule[$molecule[$i]->bonds(1)-1]->symbol eq "H"){$H_id=$molecule[$i]->bonds(1)};
				
				if($O_id!=-1){print "Error!";}
				
				$O_id=$i+1;
				$C1_id = $temp_id+1;				
			}
			
			
		}
	}	
}

#for($i=0; $i<4; $i++){
#	
#	my $id = 0;
#	
#	$id = $molecule[$O_id-1]->bonds($i);
#	
#	if($id != $H_id and $id != -1){
#		$C1_id = $id;	
#	}
#		
#}
	
print $H_id.";".$O_id.";".$C1_id."\n";
