이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#undef _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
#define fast ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define int long long
#define pb push_back
#define fi first
#define si second
#define ar array
#define dd double
typedef pair<int,int> pi;
typedef tuple<int,int,int> ti;
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {cerr << " " << to_string(H);debug_out(T...);}
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
int N, dist[1010], vis[1010], ans = 1e14, dp[1010];
vector<int> adj[1010], child[1010], order;
queue<int> q;
void dfs(int x) {
for (auto i: adj[x]) {
if (vis[i]) continue;
vis[i] = 1;
dfs(i);
}
order.pb(x);
}
signed main() {
fast;
cin >> N;
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);
}
}
for (int person = 1; person <= N; ++person) {
memset(dist, -1, sizeof dist);
dist[person] = 0;
q.push(person);
while (q.size()) {
int x = q.front(); q.pop();
for (auto i: adj[x]) {
if (dist[i] != -1) continue;
dist[i] = dist[x] + 1;
child[x].pb(i);
q.push(i);
}
}
bool skip = false;
for (int i = 1; i <= N; ++i) {
if (dist[i] == -1) skip = true;
}
if (skip) continue;
memset(vis, 0, sizeof vis);
vis[person] = 1;
dfs(person);
int cur = 0;
for (auto i: order) {
for (auto j: child[i]) dp[i] += dp[j];
++dp[i];
cur += dp[i];
}
ans = min(ans, cur);
}
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... |