Skip to content

Commit c905efc

Browse files
committed
srm 648 div 2 500
1 parent 4d09120 commit c905efc

File tree

2 files changed

+116
-0
lines changed

2 files changed

+116
-0
lines changed

Fragile2.cpp

+92
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
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 set<int> si;
14+
typedef vector<int> vi;
15+
typedef vector<vi> vvi;
16+
typedef vector<string> vs;
17+
int n;
18+
vs G;
19+
vi visited;
20+
set<int> articulation;
21+
void dfs(int i){
22+
for(int j = 0; j < n; j++){
23+
if(articulation.count(j)) continue;
24+
if(visited[j] == 0 && G[i][j] == 'Y'){
25+
visited[j] = 1;
26+
dfs(j);
27+
}
28+
}
29+
}
30+
31+
int dfs(){
32+
int res = 0;
33+
for(int i = 0; i < n; i++){
34+
if(articulation.count(i)) continue;
35+
if(visited[i] == 0){
36+
visited[i] = 1;
37+
res++;
38+
dfs(i);
39+
}
40+
}
41+
return res;
42+
}
43+
44+
class Fragile2{
45+
public:
46+
int countPairs(vector <string> graph) {
47+
n = graph.size();
48+
int total = 0;
49+
G = graph;
50+
articulation.clear();
51+
visited = vi(n, 0);
52+
int ans = dfs();
53+
54+
for(int i = 0; i < n; i++)
55+
for(int j = i+1; j < n; j++){
56+
G = graph;
57+
articulation.clear();
58+
visited = vi(n, 0);
59+
articulation.insert(i);
60+
articulation.insert(j);
61+
if(dfs() > ans) total++;
62+
}
63+
return total;
64+
}
65+
66+
// BEGIN CUT HERE
67+
public:
68+
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(); }
69+
private:
70+
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(); }
71+
void verify_case(int Case, const int &Expected, const int &Received) { cerr << "Test Case #" << Case << "..."; if (Expected == Received) cerr << "PASSED" << endl; else { cerr << "FAILED" << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } }
72+
void test_case_0() { string Arr0[] = {"NYNN", "YNYN", "NYNY", "NNYN"}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 3; verify_case(0, Arg1, countPairs(Arg0)); }
73+
void test_case_1() { string Arr0[] = {"NYNNNN", "YNYNNN", "NYNNNN", "NNNNYN", "NNNYNY", "NNNNYN"}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 5; verify_case(1, Arg1, countPairs(Arg0)); }
74+
void test_case_2() { string Arr0[] = {"NNN", "NNN", "NNN"}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 0; verify_case(2, Arg1, countPairs(Arg0)); }
75+
void test_case_3() { string Arr0[] = {"NYNYNNYYNN", "YNNNYNYYNN", "NNNNYNNNYN", "YNNNYYNNNN", "NYYYNNNNYN",
76+
"NNNYNNNNYN", "YYNNNNNNNN", "YYNNNNNNYN", "NNYNYYNYNY", "NNNNNNNNYN"}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 9; verify_case(3, Arg1, countPairs(Arg0)); }
77+
void test_case_4() { string Arr0[] = {"NNNYNNYNNNNNNNYYNNNY", "NNNNNNNNYNNNNNNNNNNN", "NNNNNNNNNNNNNNNNNNNN", "YNNNNNNNNNYNNNNNNNNN", "NNNNNNNYNNNNNYNNNNYN",
78+
"NNNNNNNNNNNNNNNNYNNY", "YNNNNNNNNNNNNYYYNYNN", "NNNNYNNNNNNNNYYNNNNN", "NYNNNNNNNYNNNNNNNNNN", "NNNNNNNNYNNNYNNNNNYN",
79+
"NNNYNNNNNNNNNNYNNNNN", "NNNNNNNNNNNNNNNNNNNN", "NNNNNNNNNYNNNNNNNYNN", "NNNNYNYYNNNNNNNNNNNN", "YNNNNNYYNNYNNNNNNNNN",
80+
"YNNNNNYNNNNNNNNNYNNN", "NNNNNYNNNNNNNNNYNYNN", "NNNNNNYNNNNNYNNNYNNN", "NNNNYNNNNYNNNNNNNNNN", "YNNNNYNNNNNNNNNNNNNN"}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 42; verify_case(4, Arg1, countPairs(Arg0)); }
81+
82+
// END CUT HERE
83+
84+
};
85+
86+
// BEGIN CUT HERE
87+
int main(){
88+
89+
Fragile2 ___test;
90+
___test.run_test(-1);
91+
}
92+
// END CUT HERE

Fragile2.html

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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><p>Lun the dog has found an undirected graph in Shuseki Forest. The graph consisted of <i>N</i> vertices and some edges. The vertices of the graph were numbered 0 through <i>N</i>-1. Each edge connected a different pair of vertices.</p>
2+
<p></p>
3+
<p>You are given the description of the graph in a vector &lt;string&gt; <b>graph</b> with <i>N</i> elements, each containing <i>N</i> characters. For each <i>i</i> and <i>j</i>, <b>graph</b>[<i>i</i>][<i>j</i>] will be 'Y' if vertex <i>i</i> and vertex <i>j</i> are connected by an edge, and 'N' otherwise. (Note that for each <i>i</i>, <b>graph</b>[<i>i</i>][<i>i</i>] will be 'N': there are no self-loops.)</p>
4+
<p></p>
5+
<p>Lun is interested in <i>articulation pairs</i> in this graph. An <i>articulation pair</i> is an unordered pair of two different vertices whose deletion increases the number of connected components in the graph. (The deletion of a vertex also removes all edges incident with that vertex.)</p>
6+
<p></p>
7+
<p>Return the number of the articulation pairs in Lun's graph.</p></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>Fragile2</td></tr><tr><td>Method:</td><td>countPairs</td></tr><tr><td>Parameters:</td><td>vector &lt;string&gt;</td></tr><tr><td>Returns:</td><td>int</td></tr><tr><td>Method signature:</td><td>int countPairs(vector &lt;string&gt; graph)</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>256</td></tr><tr><td>Stack limit (MB):</td><td>256</td></tr></table></td></tr><tr><td colspan="2"><h3>Notes</h3></td></tr><tr><td align="center" valign="top">-</td><td>You are not given the value of <i>N</i> explicitly, but you can determine it as the number of elements in <b>graph</b>.</td></tr><tr><td align="center" valign="top">-</td><td>Two vertices belong to the same connected component if and only if we can reach one of them from the other by following a sequence of zero or more edges.</td></tr><tr><td colspan="2"><h3>Constraints</h3></td></tr><tr><td align="center" valign="top">-</td><td><b>graph</b> will contain between 3 and 20 elements, inclusive.</td></tr><tr><td align="center" valign="top">-</td><td>Each element of <b>graph</b> will contain <i>N</i> characters, where <i>N</i> is the number of the elements in <b>graph</b>.</td></tr><tr><td align="center" valign="top">-</td><td>Each character of each element of <b>graph</b> will be either 'Y' or 'N'.</td></tr><tr><td align="center" valign="top">-</td><td>For each valid <i>i</i> and <i>j</i>, <b>graph</b>[<i>i</i>][<i>j</i>] will be equal to <b>graph</b>[<i>j</i>][<i>i</i>].</td></tr><tr><td align="center" valign="top">-</td><td>For each valid <i>i</i>, <b>graph</b>[<i>i</i>][<i>i</i>] will be 'N'.</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;NYNN&quot;, &quot;YNYN&quot;, &quot;NYNY&quot;, &quot;NNYN&quot;}</pre></td></tr></table></td></tr><tr><td><pre>Returns: 3</pre></td></tr><tr><td><table><tr><td colspan="2"><p>The graph looks as follows:</p>
8+
<p></p>
9+
<img src="http://s6.postimg.org/dguz2x9mp/2b0.png"></img>
10+
<p></p>
11+
<p>The articulation pairs are (0, 2), (1, 2) and (1, 3).</p>
12+
<p>For example, here is why (0, 2) is an articulation pair:
13+
Currently there is one connected component.
14+
(It contains all four vertices.)
15+
If we remove the vertices 0 and 2, and all edges incident to these vertices, we will be left with two isolated vertices: 1 and 3.
16+
Each of these vertices now forms a different connected component, so the number of connected components increased from 1 to 2.</p></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;NYNNNN&quot;, &quot;YNYNNN&quot;, &quot;NYNNNN&quot;, &quot;NNNNYN&quot;, &quot;NNNYNY&quot;, &quot;NNNNYN&quot;}</pre></td></tr></table></td></tr><tr><td><pre>Returns: 5</pre></td></tr><tr><td><table><tr><td colspan="2"><img src="http://s6.postimg.org/ew1lorh1d/2b1.png"></img>
17+
<p></p>
18+
<p>The articulation pairs are (0, 4), (1, 3), (1, 4), (1, 5) and (2, 4).</p></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;NNN&quot;, &quot;NNN&quot;, &quot;NNN&quot;}</pre></td></tr></table></td></tr><tr><td><pre>Returns: 0</pre></td></tr><tr><td><table><tr><td colspan="2"><img src="http://s6.postimg.org/6rthk0um9/2b2.png"></img>
19+
<p></p>
20+
<p>There are no articulation pairs.</p></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;NYNYNNYYNN&quot;, &quot;YNNNYNYYNN&quot;, &quot;NNNNYNNNYN&quot;, &quot;YNNNYYNNNN&quot;, &quot;NYYYNNNNYN&quot;,
21+
&quot;NNNYNNNNYN&quot;, &quot;YYNNNNNNNN&quot;, &quot;YYNNNNNNYN&quot;, &quot;NNYNYYNYNY&quot;, &quot;NNNNNNNNYN&quot;}</pre></td></tr></table></td></tr><tr><td><pre>Returns: 9</pre></td></tr><tr><td><table><tr><td colspan="2"></td></tr></table></td></tr></table></td></tr><tr><td align="center" nowrap="true">4)</td><td></td></tr><tr><td>&#160;&#160;&#160;&#160;</td><td><table><tr><td><table><tr><td><pre>{&quot;NNNYNNYNNNNNNNYYNNNY&quot;, &quot;NNNNNNNNYNNNNNNNNNNN&quot;, &quot;NNNNNNNNNNNNNNNNNNNN&quot;, &quot;YNNNNNNNNNYNNNNNNNNN&quot;, &quot;NNNNNNNYNNNNNYNNNNYN&quot;,
22+
&quot;NNNNNNNNNNNNNNNNYNNY&quot;, &quot;YNNNNNNNNNNNNYYYNYNN&quot;, &quot;NNNNYNNNNNNNNYYNNNNN&quot;, &quot;NYNNNNNNNYNNNNNNNNNN&quot;, &quot;NNNNNNNNYNNNYNNNNNYN&quot;,
23+
&quot;NNNYNNNNNNNNNNYNNNNN&quot;, &quot;NNNNNNNNNNNNNNNNNNNN&quot;, &quot;NNNNNNNNNYNNNNNNNYNN&quot;, &quot;NNNNYNYYNNNNNNNNNNNN&quot;, &quot;YNNNNNYYNNYNNNNNNNNN&quot;,
24+
&quot;YNNNNNYNNNNNNNNNYNNN&quot;, &quot;NNNNNYNNNNNNNNNYNYNN&quot;, &quot;NNNNNNYNNNNNYNNNYNNN&quot;, &quot;NNNNYNNNNYNNNNNNNNNN&quot;, &quot;YNNNNYNNNNNNNNNNNNNN&quot;}</pre></td></tr></table></td></tr><tr><td><pre>Returns: 42</pre></td></tr><tr><td><table><tr><td colspan="2"></td></tr></table></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)