Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit b14356a

Browse files
committed
Single Round Match 666
1 parent cd7cf91 commit b14356a

File tree

1 file changed

+159
-0
lines changed

1 file changed

+159
-0
lines changed
Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
const int MAXN = 102;
5+
6+
int dp[MAXN][MAXN][2];
7+
vector<int> G[MAXN];
8+
int L;
9+
10+
void dfs(int u) {
11+
dp[u][0][0] = dp[u][0][1] = 1;
12+
for (int v : G[u]) {
13+
dfs(v);
14+
for (int i = L; i > 0; --i) {
15+
for (int j = 0; j <= i; ++j) {
16+
if (i - j - 2 >= 0) {
17+
dp[u][i][1] = max(dp[u][i][1], dp[u][j][1] + dp[v][i - j - 2][1]);
18+
dp[u][i][0] = max(dp[u][i][0], dp[u][j][0] + dp[v][i - j - 2][1]);
19+
}
20+
dp[u][i][0] = max(dp[u][i][0], dp[u][i][1]);
21+
if (i -j - 1 >= 0)
22+
dp[u][i][0] = max(dp[u][i][0], dp[u][j][1] + dp[v][i - j - 1][0]);
23+
}
24+
}
25+
}
26+
}
27+
28+
struct WalkOverATree
29+
{
30+
int maxNodesVisited(vector<int> parent, int L)
31+
{
32+
::L = L;
33+
memset(dp, 0, sizeof(dp));
34+
35+
for (int i = 0; i < MAXN; ++i)
36+
G[i].clear();
37+
38+
for (int i = 0; i < (int)parent.size(); ++i)
39+
G[ parent[i] ].push_back(i + 1);
40+
41+
dfs(0);
42+
43+
int ans = 0;
44+
for (int i = L; i >= 0; --i)
45+
ans = max(ans, max(dp[0][i][0], dp[0][i][1]));
46+
47+
return ans;
48+
}
49+
};
50+
51+
// CUT begin
52+
ifstream data("WalkOverATree.sample");
53+
54+
string next_line() {
55+
string s;
56+
getline(data, s);
57+
return s;
58+
}
59+
60+
template <typename T> void from_stream(T &t) {
61+
stringstream ss(next_line());
62+
ss >> t;
63+
}
64+
65+
void from_stream(string &s) {
66+
s = next_line();
67+
}
68+
69+
template <typename T> void from_stream(vector<T> &ts) {
70+
int len;
71+
from_stream(len);
72+
ts.clear();
73+
for (int i = 0; i < len; ++i) {
74+
T t;
75+
from_stream(t);
76+
ts.push_back(t);
77+
}
78+
}
79+
80+
template <typename T>
81+
string to_string(T t) {
82+
stringstream s;
83+
s << t;
84+
return s.str();
85+
}
86+
87+
string to_string(string t) {
88+
return "\"" + t + "\"";
89+
}
90+
91+
bool do_test(vector<int> parent, int L, int __expected) {
92+
time_t startClock = clock();
93+
WalkOverATree *instance = new WalkOverATree();
94+
int __result = instance->maxNodesVisited(parent, L);
95+
double elapsed = (double)(clock() - startClock) / CLOCKS_PER_SEC;
96+
delete instance;
97+
98+
if (__result == __expected) {
99+
cout << "PASSED!" << " (" << elapsed << " seconds)" << endl;
100+
return true;
101+
}
102+
else {
103+
cout << "FAILED!" << " (" << elapsed << " seconds)" << endl;
104+
cout << " Expected: " << to_string(__expected) << endl;
105+
cout << " Received: " << to_string(__result) << endl;
106+
return false;
107+
}
108+
}
109+
110+
int run_test(bool mainProcess, const set<int> &case_set, const string command) {
111+
int cases = 0, passed = 0;
112+
while (true) {
113+
if (next_line().find("--") != 0)
114+
break;
115+
vector<int> parent;
116+
from_stream(parent);
117+
int L;
118+
from_stream(L);
119+
next_line();
120+
int __answer;
121+
from_stream(__answer);
122+
123+
cases++;
124+
if (case_set.size() > 0 && case_set.find(cases - 1) == case_set.end())
125+
continue;
126+
127+
cout << " Testcase #" << cases - 1 << " ... ";
128+
if ( do_test(parent, L, __answer)) {
129+
passed++;
130+
}
131+
}
132+
if (mainProcess) {
133+
cout << endl << "Passed : " << passed << "/" << cases << " cases" << endl;
134+
int T = time(NULL) - 1440553464;
135+
double PT = T / 60.0, TT = 75.0;
136+
cout << "Time : " << T / 60 << " minutes " << T % 60 << " secs" << endl;
137+
cout << "Score : " << 222 * (0.3 + (0.7 * TT * TT) / (10.0 * PT * PT + TT * TT)) << " points" << endl;
138+
}
139+
return 0;
140+
}
141+
142+
int main(int argc, char *argv[]) {
143+
cout.setf(ios::fixed, ios::floatfield);
144+
cout.precision(2);
145+
set<int> cases;
146+
bool mainProcess = true;
147+
for (int i = 1; i < argc; ++i) {
148+
if ( string(argv[i]) == "-") {
149+
mainProcess = false;
150+
} else {
151+
cases.insert(atoi(argv[i]));
152+
}
153+
}
154+
if (mainProcess) {
155+
cout << "WalkOverATree (222 Points)" << endl << endl;
156+
}
157+
return run_test(mainProcess, cases, argv[0]);
158+
}
159+
// CUT end

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /