이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define pb(e) push_back(e)
#define sv(a) sort(a.begin(),a.end())
#define sa(a,n) sort(a,a+n)
#define mp(a,b) make_pair(a,b)
#define vf first
#define vs second
#define ar array
#define all(x) x.begin(),x.end()
const int inf = 0x3f3f3f3f;
const int mod = 1000000007;
const double PI=3.14159265358979323846264338327950288419716939937510582097494459230;
bool remender(ll a , ll b){return a%b;}
void solve(){
int n;
cin >> n;
vector<int> adj[n + 1];
for(int i = 1; i <= n; i++){
int k;
cin >> k;
for(int j = 0; j < k; j++){
int x;
cin >> x;
adj[x].pb(i);
}
}
ll ans = 1e18;
int par[n + 1];
for(int i = 1; i <= n; i++){
memset(par , 0 , sizeof par);
queue<int> q;
q.push(i);
par[i] = -1;
ll siz[n + 1];
memset(siz,0,sizeof siz);
int cnt[n + 1];
queue<int> q1;
while(q.size()){
int node = q.front();
q.pop();
int c = 0;
for(int j : adj[node]){
if(par[j] == 0){
par[j] = node;
q.push(j);
c++;
}
}
if(c == 0){
q1.push(node);
}
cnt[node] = c;
}
bool fine = 1;
for(int j = 1; j <= n; j++){
if(par[j] == 0)fine = 0;
}
if(fine){
ll res = 0;
while(q1.size()){
int node = q1.front();
q1.pop();
siz[node]++;
res += siz[node];
if(par[node] == -1)break;
siz[par[node]] += siz[node];
cnt[par[node]]--;
if(cnt[par[node]] == 0)q1.push(par[node]);
}
ans = min(ans , res);
}
}
cout << ans << '\n';
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
//int t;cin >> t;while(t--)
solve();
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... |