Skip to content

Commit 89a2669

Browse files
committed
srm 184 div 2 1000
1 parent 572150d commit 89a2669

File tree

2 files changed

+82
-0
lines changed

2 files changed

+82
-0
lines changed

TeamBuilder.cpp

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
#include <iostream>
2+
#include <sstream>
3+
#include <vector>
4+
#include <algorithm>
5+
#include <set>
6+
#include <map>
7+
#include <queue>
8+
#include <cstring>
9+
#include <climits>
10+
#include <cstdio>
11+
using namespace std;
12+
typedef pair<int,int> pi;
13+
typedef vector<int> vi;
14+
typedef vector<vi> vvi;
15+
typedef vector<string> vs;
16+
typedef vector<vs> vvs;
17+
18+
class TeamBuilder{
19+
public:
20+
vector <int> specialLocations(vector <string> paths) {
21+
22+
int n = paths.size();
23+
int D[55][55][55];
24+
for(int i = 1; i <= n; i++)
25+
for(int j = 1; j <= n; j++){
26+
D[i][j][0] = paths[i-1][j-1] - '0';
27+
if(i == j) D[i][j][0] = 1;
28+
}
29+
30+
for(int k = 1; k <= n; k++)
31+
for(int i = 1; i <= n; i++)
32+
for(int j = 1; j <= n; j++)
33+
D[i][j][k] = ( D[i][j][k-1] | (D[i][k][k-1] & D[k][j][k-1]) );
34+
35+
vector<int> to(55, 0), from(55, 0);
36+
for(int i = 1; i <= n; i++)
37+
for(int j = 1; j <= n; j++)
38+
if(D[i][j][n] == 1){
39+
to[j]++;
40+
from[i]++;
41+
}
42+
43+
int t = 0, f = 0;
44+
for(int i = 1; i <= n; i++){
45+
if(to[i] == n) t++;
46+
if(from[i] == n) f++;
47+
}
48+
49+
vector<int> res;
50+
res.push_back(f);
51+
res.push_back(t);
52+
return res;
53+
}
54+
55+
// BEGIN CUT HERE
56+
public:
57+
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(); }
58+
private:
59+
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(); }
60+
void verify_case(int Case, const vector <int> &Expected, const vector <int> &Received) { cerr << "Test Case #" << Case << "..."; if (Expected == Received) cerr << "PASSED" << endl; else { cerr << "FAILED" << endl; cerr << "\tExpected: " << print_array(Expected) << endl; cerr << "\tReceived: " << print_array(Received) << endl; } }
61+
void test_case_0() { string Arr0[] = {"00000000001000", "00000000000000", "10000010001001", "00000000000000", "00000000011000", "00000000000000",
62+
"00000000010000", "00100100000000", "10000000000000", "11000110000000", "01000000000000", "01000001000000", "00000000000000", "10000000000000"};
63+
vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arr1[] = { 0, 0 }; vector <int> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); verify_case(0, Arg1, specialLocations(Arg0)); }
64+
void test_case_1() { string Arr0[] = {"0010","1000","1100","1000"}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arr1[] = { 1, 3 }; vector <int> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); verify_case(1, Arg1, specialLocations(Arg0)); }
65+
void test_case_2() { string Arr0[] = {"01000","00100","00010","00001","10000"}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arr1[] = { 5, 5 }; vector <int> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); verify_case(2, Arg1, specialLocations(Arg0)); }
66+
void test_case_3() { string Arr0[] = {"0110000","1000100","0000001","0010000","0110000","1000010","0001000"}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arr1[] = { 1, 3 }; vector <int> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); verify_case(3, Arg1, specialLocations(Arg0)); }
67+
68+
// END CUT HERE
69+
70+
};
71+
72+
// BEGIN CUT HERE
73+
int main(){
74+
75+
TeamBuilder ___test;
76+
___test.run_test(-1);
77+
}
78+
// END CUT HERE

TeamBuilder.html

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<html><body bgcolor="#000000" text="#ffffff"><table><tr><td colspan="2"><h3>Problem Statement</h3></td></tr><tr><td>&#160;&#160;&#160;&#160;</td><td>You are arranging a weird game for a team building exercise. In this game there are certain locations that people can stand at, and from each location there are paths that lead to other locations, but there are not necessarily paths that lead directly back. You have everything set up, but you need to know two important numbers. There might be some locations from which every other location can be reached. There might also be locations that can be reached from every other location. You need to know how many of each of these there are.<br></br>
2+
Create a class TeamBuilder with a method specialLocations that takes a vector &lt;string&gt; <b>paths</b> that describes the way the locations have been connected, and returns a vector &lt;int&gt; with exactly two elements, the first one is the number of locations that can reach all other locations, and the second one is the number of locations that are reachable by all other locations. Each element of <b>paths</b> will be a string containing as many characters as there are elements in <b>paths</b>. The <i>i</i>-th element of <b>paths</b> (beginning with the 0-th element) will contain a '1' (all quotes are for clarity only) in position <i>j</i> if there is a path that leads directly from <i>i</i> to <i>j</i>, and a '0' if there is not a path that leads directly from <i>i</i> to <i>j</i>.
3+
4+
</td></tr><tr><td colspan="2"><h3>Definition</h3></td></tr><tr><td>&#160;&#160;&#160;&#160;</td><td><table><tr><td>Class:</td><td>TeamBuilder</td></tr><tr><td>Method:</td><td>specialLocations</td></tr><tr><td>Parameters:</td><td>vector &lt;string&gt;</td></tr><tr><td>Returns:</td><td>vector &lt;int&gt;</td></tr><tr><td>Method signature:</td><td>vector &lt;int&gt; specialLocations(vector &lt;string&gt; paths)</td></tr><tr><td colspan="2">(be sure your method is public)</td></tr></table></td></tr><tr><td colspan="2"><h3>Limits</h3></td></tr><tr><td>&#160;&#160;&#160;&#160;</td><td><table><tr><td>Time limit (s):</td><td>2.000</td></tr><tr><td>Memory limit (MB):</td><td>64</td></tr></table></td></tr><tr><td colspan="2"><h3>Constraints</h3></td></tr><tr><td align="center" valign="top">-</td><td><b>paths</b> will contain between 2 and 50 elements, inclusive.</td></tr><tr><td align="center" valign="top">-</td><td>Each element of <b>paths</b> will contain <i>N</i> characters, where <i>N</i> is the number of elements of <b>paths</b>.</td></tr><tr><td align="center" valign="top">-</td><td>Each element of <b>paths</b> will contain only the characters '0' and '1'.</td></tr><tr><td align="center" valign="top">-</td><td>The <i>i</i>-th element of <b>paths</b> will contain a zero in the <i>i</i>-th position.</td></tr><tr><td colspan="2"><h3>Examples</h3></td></tr><tr><td align="center" nowrap="true">0)</td><td></td></tr><tr><td>&#160;&#160;&#160;&#160;</td><td><table><tr><td><table><tr><td><pre>{&quot;010&quot;,&quot;000&quot;,&quot;110&quot;}</pre></td></tr></table></td></tr><tr><td><pre>Returns: { 1, 1 }</pre></td></tr><tr><td><table><tr><td colspan="2">Locations 0 and 2 can both reach location 1, and location 2 can reach both of the other locations, so we return {1,1}.</td></tr></table></td></tr></table></td></tr><tr><td align="center" nowrap="true">1)</td><td></td></tr><tr><td>&#160;&#160;&#160;&#160;</td><td><table><tr><td><table><tr><td><pre>{&quot;0010&quot;,&quot;1000&quot;,&quot;1100&quot;,&quot;1000&quot;}</pre></td></tr></table></td></tr><tr><td><pre>Returns: { 1, 3 }</pre></td></tr><tr><td><table><tr><td colspan="2">Only location 3 is able to reach all of the other locations, but it must take more than one path to reach locations 1 and 2. Locations 0, 1, and 2 are reachable by all other locations. The method returns {1,3}.</td></tr></table></td></tr></table></td></tr><tr><td align="center" nowrap="true">2)</td><td></td></tr><tr><td>&#160;&#160;&#160;&#160;</td><td><table><tr><td><table><tr><td><pre>{&quot;01000&quot;,&quot;00100&quot;,&quot;00010&quot;,&quot;00001&quot;,&quot;10000&quot;}</pre></td></tr></table></td></tr><tr><td><pre>Returns: { 5, 5 }</pre></td></tr><tr><td><table><tr><td colspan="2">Each location can reach one other, and the last one can reach the first, so all of them can reach all of the others.</td></tr></table></td></tr></table></td></tr><tr><td align="center" nowrap="true">3)</td><td></td></tr><tr><td>&#160;&#160;&#160;&#160;</td><td><table><tr><td><table><tr><td><pre>{&quot;0110000&quot;,&quot;1000100&quot;,&quot;0000001&quot;,&quot;0010000&quot;,&quot;0110000&quot;,&quot;1000010&quot;,&quot;0001000&quot;}</pre></td></tr></table></td></tr><tr><td><pre>Returns: { 1, 3 }</pre></td></tr><tr><td></td></tr></table></td></tr></table><p>This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2003, TopCoder, Inc. All rights reserved. </p></body></html>

0 commit comments

Comments
 (0)