# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
531825 | devariaota | Bosses (BOI16_bosses) | C++17 | 0 ms | 204 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define int ll
signed main(){
cin.tie(0) -> ios_base::sync_with_stdio(0);
int n;
cin >> n;
vector<vector<int>> adj(n, vector<int>());
int r = -1;
for(int i=0;i<n;i++)
{
int k;
cin >> k;
if(k == 0) r = i;
for(int j=0;j<k;j++)
{
int x;
cin >> x; x--;
adj[x].push_back(i);
}
}
if(r == -1)
{
int mx = 0;
for(int i=0;i<n;i++)
{
if(adj[i].size() > mx)
{
mx = adj[i].size();
r = i;
}
}
}
queue<int> q;
q.push(r);
// cout << "root : " << r << '\n';
vector<int> g[n]; // directed tree
vector<int> dist(n, -1), child(n);
dist[r] = 0;
while(!q.empty()){
int u = q.front(); q.pop();
for(auto v : adj[u]){
if(dist[v] == -1)
{
g[u].push_back(v);
// child[u]++;
dist[v] = dist[u] + 1;
q.push(v);
}
}
}
// find shortest path for every leaf node to root
// vector<int> d(n, 1);
// for(int i=0;i<n;i++)
// {
// if(!child[i])
// {
// queue<int> q2;
// vector<int> p(n, -1);
// q2.push(i);
// while(!q2.empty()){
// int x = q2.front(); q2.pop();
// if(x == r) break;
// for(auto i : g[x])
// {
// cout << x + 1 << " -> " << i + 1 << '\n';
// if(p[i] == -1)
// {
// p[i] = x;
// q2.push(i);
// }
// }
// }
// vector<int> path;
// for(int v = r; v != -1; v = p[v])
// {
// path.push_back(v);
// }
// reverse(path.begin(), path.end());
// for(int i=1;i<path.size();i++)
// {
// d[i] += d[i - 1];
// }
// }
// }
vector<int> ans(n);
function<int(int)> Dfs = [&](int v){
int ret = 1;
for(auto i : g[v])
{
ret += Dfs(i);
}
// cout << v << " : " << ret << '\n';
return ans[v] = ret;
};
Dfs(r);
int sm = 0;
for(int i=0;i<n;i++)
{
// cout << ans[i] << " ";
sm += ans[i];
}
cout << sm << '\n';
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |