We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
- Journey to the Moon
- Discussions
Journey to the Moon
Journey to the Moon
Sort by
recency
|
26 Discussions
|
Please Login in order to post a comment
include
using namespace std;
string ltrim(const string &); string rtrim(const string &); vector split(const string &);
long long journeyToMoon(int n, vector> astronaut) { vector> adj(n); for (auto &e : astronaut) { int u = e[0], v = e[1]; adj[u].push_back(v); adj[v].push_back(u); } vector vis(n, 0); vector comp; comp.reserve(n); for (int i = 0; i < n; ++i) { if (vis[i]) continue; int sz = 0; stack st; st.push(i); vis[i] = 1; while (!st.empty()) { int u = st.top(); st.pop(); ++sz; for (int v : adj[u]) { if (!vis[v]) { vis[v] = 1; st.push(v); } } } comp.push_back(sz); } long long total = 0; long long prefix = 0; for (int s : comp) { total += prefix * (long long)s; prefix += s; } return total; }
int main() { ofstream fout(getenv("OUTPUT_PATH"));
}
string ltrim(const string &str) { string s(str); s.erase( s.begin(), find_if(s.begin(), s.end(), not1(ptr_fun(isspace))) ); return s; }
string rtrim(const string &str) { string s(str); s.erase( find_if(s.rbegin(), s.rend(), not1(ptr_fun(isspace))).base(), s.end() ); return s; }
vector split(const string &str) { vector tokens; string::size_type start = 0; string::size_type end = 0; while ((end = str.find(" ", start)) != string::npos) { tokens.push_back(str.substr(start, end - start)); start = end + 1; } tokens.push_back(str.substr(start)); return tokens; }
Solution in Python 3
Python 3
The answer seems wrong.
after finding components and number on each component, with the same syntax used in the editorial soln, the final calculation should be:
This is basically keeping the property of the_count: it is the number of possible interactions until ith component. Addition of each new component creates itself times the total number of nodes; as an addition to previous possibilities.
This changes half of the test cases.
The C# boiler plate is broken.
You need to change the return type from int to long.