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;
int main() {
int N; cin >> N;
vector<vector<int>> workers(N + 1);
for(int i = 1; i <= N; i++){
int K; cin >> K;
while(K--){
int x; cin >> x;
workers[x].push_back(i);
}
}
long long final_ans = LONG_LONG_MAX;
for(int i = 1; i <= N; i++){
queue<pair<int, int>> q;
q.push({i, 1});
vector<bool> vis(N + 1, false);
vis[i] = 1;
int visited = 1;
long long ans = 0;
while(!q.empty()){
pair<int, int> cur = q.front();
q.pop();
for(int j : workers[cur.first]){
if(vis[j]) continue;
vis[j] = 1;
visited++;
ans += (cur.second + 1);
q.push({j, cur.second + 1});
}
}
/*cout << visited << "\n";
for(int j = 1; j <= N; j++){
cout << j << " " << Tree[j].parent << " " << Tree[j].depth << "\nchildren are : ";
for(int k : Tree[j].children) cout << k << " ";
cout << "\n\n";
}
cout << "\n\n==============================\n";
cout << i << " ";*/
if(visited == N){
final_ans = min(final_ans, ans);
}
else{
//cout << "Not valid\n\n\n";
}
}
cout << final_ans + 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... |