이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
// That's what she said !
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define int long long
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
const int maxn = 5e3 + 9 , modn = 1e9 + 7;
int n, v, k, ans = 1e18, cnt[maxn], h[maxn];
bool mark[maxn];
vector <int> adj[maxn], adj2[maxn];
int bfs(int v)
{
queue <int> q;
int H = 0, sum = 0, g = 0;
mark[v] = 1;
cnt[0] = 1;
q.push(v);
while(q.size()){
v = q.front();
q.pop();
for(auto i : adj[v]){
if(mark[i])
continue;
h[i] = h[v] + 1;
cnt[h[i]]++;
H = max(H , h[i]);
mark[i] = 1;
q.push(i);
}
}
for(int i = H; i > -1; i--){
g += cnt[i];
sum += g;
}
for(int i = 1; i <= n; i++)
if(!mark[i])
return -1;
return sum;
}
int32_t main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> n;
for(int i = 1; i <= n; i++){
cin >> k;
for(int j = 0; j < k; j++){
cin >> v;
adj[v].push_back(i);
adj2[v].push_back(i);
adj[i].push_back(v);
}
}
for(int i = 1; i <= n; i++){
swap(adj[i], adj2[i]);
int p = bfs(i);
swap(adj[i], adj2[i]);
if(p != -1)
ans = min(ans , p);
for(int i = 0; i <= n; i++){
cnt[i] = h[i] = 0;
mark[i] = 0;
}
}
cout << ans;
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |