이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define int long long
#define INF 1000000000000000
#define lchild(i) (i*2 + 1)
#define rchild(i) (i*2 + 2)
#define mid(l, u) ((l+u)/2)
#define x(p) p.first
#define y(p) p.second
#define MOD 998244353
using namespace std;
int cost = 0;
int calcost(int i, int par, vector<int> adj[]){
int sum = 0;
for(int j: adj[i]){
if(j==par) continue;
sum+=calcost(j, i, adj);
}
//cout<<"node "<<i<<" value "<<(sum+1)<<"\n";
cost += (sum + 1);
return (sum + 1);
}
signed main(){
int n;
cin>>n;
vector<int> children[n+1];
for(int i =0 ;i<n;i++){
int k;
cin>>k;
for(int j =0 ;j<k;j++){
int par;
cin>>par;
children[par].push_back(i+1);
}
}
int ans = INF;
for(int i = 1;i<=n;i++){
vector<int> adj[n+1];
bool chosen[n+1];
for(int j = 0;j<=n;j++) chosen[j] = false;
chosen[i] = true;
queue<int> q;
q.push(i);
while(!q.empty()){
int curr = q.front();
q.pop();
for(int j: children[curr]){
if(!chosen[j]){
//cout<<curr<<" is parent of "<<j<<"\n";
chosen[j] = true;
q.push(j);
adj[curr].push_back(j);
}
}
}
bool nc = false;
for(int j = 1;j<=n;j++) if(!chosen[j]) nc = true;
if(nc) continue;
cost = 0;
calcost(i, -1, adj);
//cout<<i<<" "<<cost<<"\n";
ans = min(ans, cost);
}
cout<<cost<<"\n";
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |