이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
#define tizoboz ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define ll long long
#define ld long double
#define pb push_back
#define int long long
#define itn int
#define ss set <int>
#define prq priority_queue <int>
#define endl '\n'
const ll MOD = 1e9 + 7;
#define md(x) (((x%MOD)+MOD)%MOD)
#define vi vector <int>
#define vl vector<ll>
#define str string
#define mp make_pair
#define mata int32_t
#define sz size
#define lc id *2
#define rc lc +1
#define SZ(x) (int)x.size()
#define mid (l+r)/2
#define cn cin
#define ct cout
#define sep " "
#define F first
#define X first
#define S second
#define Y second
using namespace std;
typedef pair <int , int> pii;
const ll maxn = 5e2 + 5 ;
const int Lg = 23;
const ll inf = 1e9 ;
vi g[maxn], c[maxn];
bool use[maxn];
int cu = 0;
int dfs(int v , int p = -1){
int x = 0;
for (int to : c[v]){
if (to == p) continue;
x += dfs(to , v);
}
cu += x+1;
return x+1;
}
void solve (){
int n;
cn >> n;
pii mb = {-inf , -inf};
for (int i = 1 ; i <= n ; i ++){
int k; cn >> k;
for (int j = 0 ; j < k ; j ++){
int p;
cn >> p;
g[p].pb(i);
mb = max (mb , {(int)g[p].sz() , p});
}
}
int ans = inf;
for (int i = 1 ; i <= n ; i ++){
if (g[i].sz() == 0) continue;
if (abs((int)g[i].sz() - mb.F) > 1) continue;
for (int j = 1 ; j <= n ; j++) c[j].resize(0);
queue <int> q;
q.push(i);
memset(use, 0 , sizeof use);
use[i] = 1;
int h = 1;
while (SZ(q)){
int v = q.front(); q.pop();
for (auto to : g[v]){
if (use[to] == 1) continue;
c[v].pb(to);
q.push(to);
use[to] = 1;
h++;
}
}
if ( h == n){
cu = 0;
dfs(i);
ans = min (ans , cu);
}
}
ct << ans << endl;
}
mata main(){
tizoboz;
int tt = 1;
// cn >> tt;
while (tt--){
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... |