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>
#define ll long long
#define pb push_back
#define mp make_pair
#define ull unsigned long long
using namespace std;
const int maxn = 5100;
vector<int>g[maxn], gc[maxn];
bool visited[maxn];
ll result, temp_result;
int n;
ll dfs(int node, int parent) {
ll tsum = 0LL;
for(int i:gc[node]) {
if(i!=parent) {
tsum += dfs(i, node);
}
}
temp_result += tsum + 1LL;
return tsum + 1LL;
}
void bfs(int st) {
for(int i=1;i<=n;i++) gc[i].clear();
memset(visited, false ,sizeof(visited));
queue<int>q;
q.push(st);
visited[st] = true;
int cntr = 1;
while(!q.empty()) {
int curr = q.front();
q.pop();
for(int i:g[curr]) {
if(!visited[i]) {
visited[i] = true;
cntr++;
gc[curr].pb(i);
q.push(i);
}
}
}
if(cntr == n) {
temp_result = 0LL;
ll temp = dfs(st, -1);
result = min(result, temp_result);
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cin>>n;
int t, u;
for(int i=1;i<=n;i++) {
cin>>t;
for(int j=0;j<t;j++) {
cin>>u;
g[u].pb(i);
}
}
result = LLONG_MAX;
for(int i=1;i<=n;i++) {
bfs(i);
}
cout<<result;
return 0;
}
Compilation message (stderr)
bosses.cpp: In function 'void bfs(int)':
bosses.cpp:48:12: warning: unused variable 'temp' [-Wunused-variable]
ll temp = dfs(st, -1);
^~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |