#include <iostream>
#include <vector>
#include <cstring>
using namespace std;
long long n;
vector<long long> g[100005];
long long a[100005];
long long dr[100005];
bool pos[100005];
void dfs(long long x, long long dr1)
{
try
{
pos[x]=1;
long long k;
dr[x]=dr1;
for (k=0; k<g[x].size(); k++) if (pos[g[x][k]]==0)
{
dfs(g[x][k],dr1*(g[x].size()-1));
a[x]+=a[g[x][k]];
}
}
catch(exception &e)
{
cout<<"dfs1"<<endl;
}
}
long long gcd(long long a, long long b)
{
try
{
if (b==0) return a;
else return gcd(b,a%b);
}
catch(exception &e)
{
cout<<"jovan se otkazuva od programiranje"<<endl;
}
}
long long lcm(long long a, long long b)
{
try
{
return (a*b)/gcd(a,b);
}
catch(exception &e)
{
cout<<"jovan se otkazuva od programiranje"<<endl;
}
}
int main()
{
try
{
long long k,i,j,rez;
cin>>n;
for (k=1; k<=n; k++) cin>>a[k];
try
{
for (k=0; k<n-1; k++)
{
cin>>i>>j;
g[i].push_back(j);
g[j].push_back(i);
}
}
catch(exception& e)
{
cout<<'a'<<endl;
}
try
{
g[1].push_back(1);
memset(pos,0,sizeof(pos));
}
catch(exception& e)
{
cout<<"mem"<<endl;
}
try
{
dfs(1,1);
}
catch(exception& e)
{
cout<<"dfs"<<endl;
}
rez=99999999999999999L;
try
{
for (k=1; k<=n; k++)
{
rez=min(rez,a[k]*dr[k]);
}
}
catch(exception& e)
{
cout<<"min"<<endl;
}
long long l=1;
try
{
for (k=2; k<=n; k++) l=lcm(l,dr[k]);
}
catch(exception& e)
{
cout<<'c'<<endl;
}
//for (k=1;k<=n;k++) cout<<dr[k]<<endl;
try
{
rez=rez-(rez%l);
cout<<a[1]-rez<<endl;
}
catch(exception& e)
{
cout<<"tuka nema"<<endl;
}
return 0;
}
catch(exception& e)
{
cout<<"tuka ako nema "<<endl;
}
}
My whole code is in try-catch blocks, and still I get a runtime error. I can't figure out why... Can someone help me?









You know you can post links to submissions like this: http://codeforces.me/contest/348/submission/4614129 or like this 4614129?
Also you can read this topic on StackOverflow:http://stackoverflow.com/questions/1239938/c-accesses-an-array-out-of-bounds-gives-no-error-why.
If there is an undefined behaivour in your code, it can generate non-zero exit code without throwing an exception. For example:
This code will get runtime error without exception.
It looks like L can be equal to 0, so you divide by zero and that's it, Runtime Error. C++ is not Java, it will not generate an exception. It will say to process DIVIDE BY ZERO and processor will answer GO TO HELL and everything will be very very bad.
http://stackoverflow.com/questions/4747934/c-catch-a-divide-by-zero-error
I fixed the problem and the task was accepted by the judge. :D Thanks for clearing this up for me, and I'm sorry for posting the whole code here. :D