이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/extc++.h>
#define all(v) v.begin(), v.end()
#define zip(v) sort(all(v)), v.erase(unique(all(v)), v.end())
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
int n, ans=1e9, p, R;
queue<int> v[5050], hubo[5050];
vector<int> adj[5050];
int dfs(int u) {
int ret=1;
for(int& v:adj[u]) ret+=dfs(v);
//cout<<u<<' '<<ret<<endl;
if(u!=R) p+=ret;
return ret;
}
int f(int r) {
R=r;
for(int i=1;i<=n;i++) adj[i].clear(), hubo[i]=v[i];
vector<bool> visited(n+1,0);
queue<int> q;
q.push(r);
visited[r]=1;
int cnt=0;
p=0;
while(!q.empty()) {
int u=q.front(); q.pop();
cnt++;
while(!hubo[u].empty()) {
int v=hubo[u].front(); hubo[u].pop();
if(visited[v]) continue;
visited[v]=1;
adj[u].push_back(v);
q.push(v);
}
}
//cout<<cnt<<endl;
if(cnt<n) return 1e9;
return dfs(r)+p;
}
void solve() {
cin>>n;
for(int i=1;i<=n;i++) {
int k; cin>>k;
while(k --> 0) {
ll x; cin>>x;
v[x].push(i);
}
}
for(int r=1;r<=n;r++) {
ans=min(ans,f(r));
//cout<<f(r)<<endl;
}
cout<<ans;
}
int main() {
cin.tie(0)->sync_with_stdio(0);
int tc=1; //cin>>tc;
while(tc --> 0) solve();
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |