이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#ifdef DEBUG
#include "../Library/debug.h"
#else
#define dbg(x...)
#endif
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pi> vpi;
typedef vector<pl> vpl;
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define F0R(i, a) for (int i = 0; i < (a); ++i)
#define FORd(i, a, b) for (int i = (b) - 1; i >= (a); --i)
#define F0Rd(i, a) for (int i = (a) - 1; i >= 0; --i)
#define trav(a, x) for (auto& a : x)
#define f first
#define s second
#define pb push_back
#define sz(x) (int)(x).size()
#define all(x) x.begin(), x.end()
const char nl = '\n';
const int INF = 1e9;
const int MOD = 1e9 + 7;
vector<vi> adj, p;
vector<bool> vis;
vl a;
int n;
vector<vi> childs;
void dfs(int u){
vis[u] = true;
trav(v, childs[u]){
dfs(v);
}
}
bool bfs(vi cost){
childs.clear();
childs.resize(n);
vi par(n);
int cnt = 0;
F0R(i, n){
par[i] = i;
trav(u, p[i]){
if(cost[u] < cost[par[i]])
par[i] = u;
}
if(par[i] == i)
cnt++;
}
if(cnt != 1)
return false;
F0R(i, n){
if(par[i] == i)
continue;
childs[par[i]].pb(i);
}
F0R(i, n){
if(par[i] == i){
dfs(i);
break;
}
}
if(accumulate(all(vis), 0) != n)
return false;
vis.assign(n, false);
queue<int> q;
F0R(i, n){
if(childs[i].empty()){
dbg(i)
q.push(i), vis[i] = true;
}
}
while(!q.empty()){
int u = q.front();
q.pop();
if(par[u] == u)
continue;
a[par[u]] += a[u];
if(!vis[par[u]])
q.push(par[u]), vis[par[u]] = true;
}
dbg(par)
return true;
}
void solve(){
cin >> n;
adj.resize(n);
p.resize(n);
vis.resize(n);
a.resize(n);
F0R(i, n){
int x; cin >> x;
F0R(j, x){
int a;
cin >> a;
a--;
adj[a].pb(i);
p[i].pb(a);
}
}
ll mn = 1e18;
vi cost(n);
F0R(i, n) cost[i] = i;
do{
vis.assign(n, false);
a.assign(n, 1);
if(!bfs(cost)) continue;
dbg(a);
dbg(childs)
mn = min(mn, accumulate(all(a), 0LL));
}while(next_permutation(all(cost)));
cout << mn << nl;
}
int32_t main(){
ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
int TC = 1;
// cin >> TC;
while(TC--){
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... |