100% by me coded in c+++
[hide]
PHP
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <deque>
#include <queue>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
using namespace std;
class CarrotBoxes
{
int n;
vector< vector<int> > adj;
public:
double theProbability(vector <string> info)
{
n = info.size();
adj.resize(n);
for(int i=0; i<n; i++){
adj[i].resize(n);
for(int j=0; j<info[i].length(); j++)
adj[i][j] = (info[i][j] == 'Y') ? 1 : 0;
}
FloydWarshall( );
vector<int> comp(n);
StronglyConnectedComponents(comp);
/* Encontra as fontes (grau de entrada = 0) */
vector<int> indeg(n);
for(int i=0; i<n; i++){
if(comp[i] != i) continue;
for(int j=0; j<n; j++)
indeg[j] += (adj[i][j] && j != i);
}
/* Conta qtas fontes alcancam cada vertice */
vector<int> reach(n);
for(int i=0; i<n; i++){
if(comp[i] != i || indeg[i] != 0) continue;
for(int j=0; j<n; j++)
if (i != j && adj[i][j]) reach[j]++;
}
/* Se houver uma fonte que so alcanca folha que
alguma outra folha alcanca, podemos ignora-la */
int open = 0;
int last = 0;
for(int i=0; i<n; i++){
if(comp[i] != i || indeg[i] != 0) continue;
int min_reach = n+1;
for(int j=0; j<n; j++)
if (i != j && adj[i][j])
min_reach = min(min_reach, reach[j]);
if (min_reach == 1 || last) // Nao podemos ignorar
open++;
else last = 1;
}
return(1.0 - open*(1.0/n));
}
void FloydWarshall( ){
for(int k=0; k<n; k++)
for(int i=0; i<n; i++)
for(int j=0; j<n; j++)
adj[i][j] = max(adj[i][j], adj[i][k] * adj[k][j]);
}
void StronglyConnectedComponents(vector<int> &comp){
comp.assign(n, -1);
for(int i=0; i<n; i++){
if(comp[i] != -1) continue;
for(int j=0; j<n; j++){
if(adj[i][j] && adj[j][i])
comp[j] = i;
}
}
}
// BEGIN CUT HERE
public:
void run_test(int Case) { if ((Case == -1) || (Case == 0)) test_case_0(); if ((Case == -1) || (Case == 1)) test_case_1(); if ((Case == -1) || (Case == 2)) test_case_2(); if ((Case == -1) || (Case == 3)) test_case_3(); if ((Case == -1) || (Case == 4)) test_case_4(); if ((Case == -1) || (Case == 5)) test_case_5(); }
private:
template <typename T> string print_array(const vector<T> &V) { ostringstream os; os << "{ "; for (typename vector<T>::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); }
void verify_case(int Case, const double &Expected, const double &Received) { cerr << "Test Case #" << Case << "..."; if (Expected <= Received + 1e-8 && Expected >= Received - 1e-8) cerr << "PASSED" << endl; else { cerr << "FAILED" << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } }
void test_case_0() { string Arr0[] = {"YYYYY",
"NYNNN",
"NNYNN",
"NNNYN",
"NNNNY"}
; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); double Arg1 = 0.8; verify_case(0, Arg1, theProbability(Arg0)); }
void test_case_1() { string Arr0[] = {"YNNNN",
"NYNNN",
"NNYNN",
"NNNYN",
"NNNNY"}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); double Arg1 = 0.2; verify_case(1, Arg1, theProbability(Arg0)); }
void test_case_2() { string Arr0[] = {"Y"}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); double Arg1 = 1.0; verify_case(2, Arg1, theProbability(Arg0)); }
void test_case_3() { string Arr0[] = {"YNNNN",
"YYNNN",
"YNYNN",
"NNNYY",
"NNNYY"}
; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); double Arg1 = 0.6; verify_case(3, Arg1, theProbability(Arg0)); }
void test_case_4() { string Arr0[] = {"YYYNNNYN",
"NYNNNNYN",
"NNYNNNNN",
"NYNYNNNN",
"YNNNYNNY",
"NNYNNYNN",
"NNNNYNYN",
"NNYNNNNY"}
; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); double Arg1 = 0.875; verify_case(4, Arg1, theProbability(Arg0)); }
void test_case_5() { string Arr0[] = {"YNNNNNNNNYNNNNNNNNNN",
"NYNNNNNNNNNNNNNNNNNN",
"NNYNNNNNNNYNNNNNYNNN",
"NNNYNYNNNNNNNNYNNNNN",
"NNNNYNNNNNNNNNYNNNNY",
"NNNNNYNNNNNNNNNNNNNY",
"NNNNYNYNYNNNNNNNNNNN",
"NNNNNNNYNNNYYNNNNNNN",
"NNNNNNNNYNNNNNNNNNNN",
"YNNNNNNNNYNNNNNYNNNN",
"NNNNNNNNNNYNNNNNNNNN",
"NYNNNNNNNNNYNNNNNNNN",
"NNNNNNNYNNNNYNNNNNNN",
"NNNNNNNNNNNNNYNNNYNN",
"NNNNNNNNNNNYNNYNNNYN",
"NYNNNNNNNNNNNNNYNNNN",
"NNYNNNNNNNNNNNNNYNNN",
"NNNNNNNNNNNNNYNYNYNN",
"NNNNNNNNYNYNNNNNNNYY",
"NNNYNNNNNNNNNNNNNNNY"}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); double Arg1 = 0.75; verify_case(5, Arg1, theProbability(Arg0)); }
// END CUT HERE
};
// BEGIN CUT HERE
int main()
{
CarrotBoxes ___test;
___test.run_test(-1);
}
// END CUT HERE
Alles anzeigen
[/hide]